###一、整合步骤 ####1、添加启动依赖
1<!-- mybatis --> 2 <dependency> 3 <groupId>org.mybatis.spring.boot</groupId> 4 <artifactId>mybatis-spring-boot-starter</artifactId> 5 <version>1.3.2</version> 6 </dependency> 7 <!-- 通用Mapper --> 8 <dependency> 9 <groupId>tk.mybatis</groupId> 10 <artifactId>mapper-spring-boot-starter</artifactId> 11 <version>2.1.5</version> 12 </dependency>
####2、配置Mybatis
1#配置mybatis 2mybatis: 3 #实体类包名路径 4 type-aliases-package: com.bh.pojo 5 #映射文件路径 6# mapper-locations: classpath:mapper/*.xml 7 configuration: 8 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
####3、给启动类配置MapperScan扫描注解
1package com.bh; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6import tk.mybatis.spring.annotation.MapperScan; 7 8/** 9 * 启动类 10 */ 11@SpringBootApplication 12//整合通用Mapper,修改扫描注解tk.mybatis.spring.annotation.MapperScan 13@MapperScan("com.bh.mapper") 14public class Application { 15 public static void main(String[] args) { 16 SpringApplication.run(Application.class, args); 17 } 18}
####4、编写各层代码 pojo实体类
1package com.bh.pojo; 2 3import javax.persistence.Id; 4import javax.persistence.Table; 5 6import tk.mybatis.mapper.annotation.KeySql; 7 8@Table(name = "user") 9public class User { 10 @Id 11 @KeySql(useGeneratedKeys = true)//主键回填 12 private int id; 13 private String username; 14 private String password; 15 private String email; 16 ……(geter、setter) 17}
mapper层
1package com.bh.mapper; 2 3import com.bh.pojo.User; 4 5import tk.mybatis.mapper.common.Mapper; 6 7public interface UserMapper extends Mapper<User> { 8 9}
service层
1package com.bh.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7import org.springframework.transaction.annotation.Transactional; 8 9import com.bh.mapper.StudentMapper; 10import com.bh.mapper.UserMapper; 11import com.bh.pojo.User; 12 13@Service 14public class UserService { 15 16 @Autowired 17 private UserMapper userMapper; 18 19 //根据id查询 20 public User findUserById(Integer id) { 21 return userMapper.selectByPrimaryKey(id); 22 } 23 24 public List<User> find(){ 25 return userMapper.selectAll(); 26 } 27}
controller层
1package com.bh.controller; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.web.bind.annotation.GetMapping; 7import org.springframework.web.bind.annotation.PathVariable; 8import org.springframework.web.bind.annotation.RestController; 9 10import com.bh.pojo.User; 11import com.bh.service.UserService; 12 13@RestController 14public class HelloController { 15 16 @Autowired 17 private UserService userService; 18 /** 19 * 根据id查询 20 * @param id 21 * @return 22 */ 23 @GetMapping("/user/{id}") 24 public User getUser(@PathVariable Integer id) { 25 return userService.findUserById(id); 26 } 27 /** 28 * 查询所有用户 29 * @return 30 */ 31 @GetMapping("/user") 32 public List<User> get(){ 33 return userService.find(); 34 } 35}
###二、出现的问题 ####1、问题 代码的大致结构基本写完,然后看似也没什么问题,把程序运行起来,去访问,就会发现 
但是呢我数据库里是有数据的,接下来再访问一下查询全部用户的接口 
现在问题就是我这的id全是0,这肯定是不对的,看控制台打印的sql语句会发现没有id那个字段

####2、原因及解决方法 经过排查,发现问题出现的原因是我实体类中定义的id类型是基本数据类型int 解决方法把int类型改为它的包装类Integer类型,问题就解决了 修改User类中的类型 
运行结果 
###三、总结 【注意】在开发过程中,实体类的属性类型尽量不要使用基本类型,用相应的包装类来代替,避免出现一些奇怪的问题