本文主要是介绍Spring Boot 整合 Mybatis (一) mybatis-spring-boot-starter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mybatis-spring-boot-autoconfigure :http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html
mybatis:http://www.mybatis.org/mybatis-3/zh/index.html
1.maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>priv.gitonlie.mybatis</groupId><artifactId>mybatis</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-mybatis</name><description>mybatis project for Spring Boot</description><properties><java.version>1.8</java.version><mybatis.springboot.version>1.3.2</mybatis.springboot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis.springboot.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></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><skipTests>true</skipTests></configuration></plugin></plugins></build></project>
2.application.yml配置
spring:application:name: mybatis-serverdatasource:driverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/flowable_demo?useUnicode=true&characterEncoding=utf-8username: rootpassword: admin123456server:port: 8081mybatis:type-aliases-package: priv.gitonlie.mybatis.entitymapper-locations:- classpath:mapper/*.xml
3.实体类
public class User {private String id;private String name;private String password;private String token;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getToken() {return token;}public void setToken(String token) {this.token = token;}}
4.Mapper接口
public interface UserMapper {List<User> queryAllUser();
}
5.mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="priv.gitonlie.mybatis.mapper.UserMapper"><select id="queryAllUser" resultType="User">select * from user</select>
</mapper>
6.service服务层
@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> queryAll(){return userMapper.queryAllUser();}
}
7.controller控制层
@RestController
public class UserController {@Autowiredprivate UserService service;@GetMapping("demo")public List<User> queryAll(){List<User> list = service.queryAll();return list;}
}
8.启动访问
这篇关于Spring Boot 整合 Mybatis (一) mybatis-spring-boot-starter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!