確認maven環境,安裝maven在這裏就不贅述了.java
在idea新建maven項目mysql
打開pom文件,導入jar包web
下面是配置以後的pom文件內容spring
<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>com.xzk</groupId>
<artifactId>testboot</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>testboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>testboot</finalName>
</build>
</project>
maven刷新,確認導入包,sql
在main下新建java文件夾數據庫
新建入口類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
實體
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
控制層
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/testboot")
public class TestBootController {
@RequestMapping("getuser")
public User getUser() {
User user = new User();
user.setName("test");
return user;
}
}
啓動,
或者按alt+F12, 在路徑後邊加上 mvn spring-boot:run 回車apache
或者在cmd, 項目根目錄下鍵入 mvn spring-boot:runjson
集成mybatis瀏覽器
不用說,先配置pom文件,導入jar包,最終的pom內容:tomcat
<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>com.xu</groupId>
<artifactId>testboot</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>testboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--支持使用 JDBC 訪問數據庫-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!--Mysql / DataSource-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Json Support-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.43</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
接下來配置數據源,在resource下新建文件
application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.address=127.0.0.1
server.port=9090
#配置項目訪問路徑
#server.context-path=/test
在Application.class中配置DataSource,SqlSeesion. 最終的Application.class內容:
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
//@EnableAutoConfiguration
//@Configuration
//@ComponentScan
@SpringBootApplication
@MapperScan("com.xu.boot.mapper")
public class Application {
private static Logger logger = Logger.getLogger(Application.class);
//DataSource配置
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return new DataSource();
}
//提供SqlSeesion
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
/**
* Main Start
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.info("============= SpringBoot Start Success =============");
}
}
其中:
若是目錄結構和springboot推薦的同樣,那麼只加@SpringBootApplication這一個註解就能夠.
這個不用多說,mapper的映射路徑
這個spring.datasource是和properties文件前綴匹配.
這個是xml的映射.
最終的目錄結構:
這個yml文件是空的, 裏邊也能夠配置內容,但通常咱們都習慣在properties裏面配置.
接下來是controller,service,mapper, 也能夠用mybatis-generator自動生成
import com.alibaba.fastjson.JSONArray;
import com.xu.boot.model.User;
import com.xu.boot.service.UserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
private Logger logger = Logger.getLogger(UserController.class);
@Autowired
private UserService userService;
@RequestMapping("/getUserById")
@ResponseBody
public User getUserById(HttpServletRequest request) {
int id = Integer.parseInt(request.getParameter("id"));
User user = userService.getUserById(id);
if(user!=null){
logger.info(JSONArray.toJSON(user));
}
return user;
}
}
而後是service
import com.xu.boot.model.User;
public interface UserService {
User getUserById(int id);
}
-----------------------------------------
import com.xu.boot.mapper.UserMapper;
import com.xu.boot.model.User;
import com.xu.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper mapper;
@Override
public User getUserById(int id) {
return mapper.getUserById(id);
}
}
--------------------mapper-----------------------
import com.xu.boot.model.User;
public interface UserMapper {
User getUserById(int id);
}
------------------------XML---------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xu.boot.mapper.UserMapper">
<select id="getUserById" resultType="com.xu.boot.model.User" parameterType="java.lang.Integer">
select name, age,password from user WHERE id=#{id,jdbcType=INTEGER};
</select>
</mapper>
接下來啓動Application, 運行main方法.在瀏覽器輸入http://localhost:9090/getUserById?id=1