spring-boot 整合mybatis 配置 可以发布到tomcat中运行

  • Post author:
  • Post category:其他


整个项目的结构


直接贴代码

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=1111

package org.shenlan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		
		SpringApplication.run(Application.class, args);
		
	}
}

package org.shenlan;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
 * 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
 */
public class SpringBootStartApplication extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    // 注意这里要指向原先用main方法执行的Application启动类
    return builder.sources(Application.class);
  }
}

package org.shenlan.web;

/**
 * Created by wangwei on 2016/9/2.
 */
public class User {
	private Integer id;
	private String name;
	private Integer age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
}
package org.shenlan.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by wangwei on 2016/9/2.
 */
@RestController
@RequestMapping({ "/home" })
public class UserController {
	@Autowired
	UserMapper userMapper;

	@RequestMapping(value = "/user/{userName}")
	@ResponseBody
	public String user(@PathVariable("userName") String userName) {
		User user = userMapper.findUserByName(userName);
		if (user == null) return userName+"不存在" ;
		else return user.getName()+"年龄:"+user.getAge() ;
	}
}


package org.shenlan.web;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * Created by Administrator on 2016/9/2.
 */
@Mapper
public interface UserMapper {
	@Select("select * from user where name = #{name}")
	User findUserByName(@Param("name") String name);
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>spring-boot</groupId>
	<artifactId>spring-boot</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.2.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		
	<!-- 测试配置
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency> 
		-->
		
		<!-- 发布时的配置start -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			 <!-- 移除嵌入式tomcat插件 -->
			  <exclusions>
			    <exclusion>
			      <groupId>org.springframework.boot</groupId>
			      <artifactId>spring-boot-starter-tomcat</artifactId>
			    </exclusion>
			  </exclusions>
		</dependency>
		
		<dependency>
		
	  <groupId>javax.servlet</groupId>
	  <artifactId>javax.servlet-api</artifactId>
	  <version>3.1.0</version>
	  <scope>provided</scope>
	</dependency>
		<!-- 发布时的配置end -->
	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis</artifactId>
	    <version>3.4.1</version>
	  </dependency>
		
	  <dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.18</version>
	  </dependency>
	 
	   
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

配置完以上步骤后就可以将项目配置到tomcat 中去 

数据库的表为

启动项目后浏览器打开   http://localhost:8080/spring-boot/home/user/张三 

 

参考地址:http://blog.csdn.net/javahighness/article/details/52515226

上面的项目下载地址:http://download.csdn.net/detail/qq_27292113/9810613