spring boot整合mybatis
引入mybatis starter依赖
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
application.properties添加spring mybatis和datasource配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
spring.datasource.url=jdbc:mysql://localhost:33062/test?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=766774 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.config-location=classpath:/mybatis/mybatis-config.xml
resource/mybatis/mybatis-config.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <settings> 7 <!--关闭缓存--> 8 <setting name="cacheEnabled" value="false"/> 9 <!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射--> 10 <setting name="mapUnderscoreToCamelCase" value="true"/> 11 <!-- 允许 JDBC 支持自动生成主键,--> 12 <setting name="useGeneratedKeys" value="true"/> 13 <!--配置默认的执行器。SIMPLE 就是普通的执行器;REUSE 执行器会重用预处理语句(prepared statements); BATCH 执行器将重用语句并执行批量更新--> 14 <setting name="defaultExecutorType" value="REUSE"/> 15 <!--设置超时时间,它决定驱动等待数据库响应的秒数。--> 16 <setting name="defaultStatementTimeout" value="600"/> 17 18 </settings> 19 20 <!--类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余--> 21 <typeAliases> 22 <typeAlias type="com.xiaoping.house.common.model.User" alias="user"/> 23 </typeAliases> 24 <!--这些配置会告诉了 MyBatis 去哪里找映射文件--> 25 <mappers> 26 <mapper resource="mapper/user.xml"/> 27 </mappers> 28 </configuration>
实体类common/model/User
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.xiaoping.house.common.model; import lombok.Data; @Data public class User { private Long id; private String name; private String email; private String phone; }
mapper类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.xiaoping.house.mapper; import com.xiaoping.house.common.model.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { public List<User> selectUsers(); }
resource/mapper/user.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <!-- namespace为命名空间,应该是mapper接口的全称--> 7 <mapper namespace="com.xiaoping.house.mapper.UserMapper"> 8 <select id="selectUsers" resultType="user"> 9 select id,name,email,phone from user 10 </select> 11 </mapper>
控制器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.xiaoping.house.controller; 2 3 import com.xiaoping.house.common.model.User; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import java.util.List; 9 10 @RestController 11 @RequestMapping("/user") 12 public class UserController { 13 @Autowired 14 private UserService userService; 15 @RequestMapping("/users") 16 public List<User> getUsers(){ 17 return userService.getUsers(); 18 } 19 }
service
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.xiaoping.house.controller; 2 3 import com.xiaoping.house.common.model.User; 4 import com.xiaoping.house.mapper.UserMapper; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import java.util.List; 9 10 @Service 11 public class UserService { 12 @Autowired 13 private UserMapper userMapper; 14 public List<User> getUsers() { 15 return userMapper.selectUsers(); 16 } 17 }