Spring Boot踩坑笔记一:Spring Boot整合mybatis和通用Mapper遇到的坑

一、整合步骤

1、添加启动依赖

<!-- mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- 通用Mapper -->
    <dependency>
	    <groupId>tk.mybatis</groupId>
	    <artifactId>mapper-spring-boot-starter</artifactId>
	    <version>2.1.5</version>
	</dependency>

2、配置Mybatis

#配置mybatis
mybatis:
  #实体类包名路径
  type-aliases-package: com.bh.pojo
  #映射文件路径
#  mapper-locations: classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3、给启动类配置MapperScan扫描注解

package com.bh;

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

import tk.mybatis.spring.annotation.MapperScan;

/**
 * 启动类 
 */
@SpringBootApplication
//整合通用Mapper,修改扫描注解tk.mybatis.spring.annotation.MapperScan
@MapperScan("com.bh.mapper")
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

4、编写各层代码

pojo实体类

package com.bh.pojo;

import javax.persistence.Id;
import javax.persistence.Table;

import tk.mybatis.mapper.annotation.KeySql;

@Table(name = "user")
public class User {
	@Id
	@KeySql(useGeneratedKeys = true)//主键回填
	private int id;
	private String username;
	private String password;
	private String email;
        ……(geter、setter)
}

mapper层

package com.bh.mapper;

import com.bh.pojo.User;

import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {

}

service层

package com.bh.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bh.mapper.StudentMapper;
import com.bh.mapper.UserMapper;
import com.bh.pojo.User;

@Service
public class UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	//根据id查询
	public User findUserById(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}
	
	public List<User> find(){
		return userMapper.selectAll();
	}
}

controller层

package com.bh.controller;

import java.util.List;

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

import com.bh.pojo.User;
import com.bh.service.UserService;

@RestController
public class HelloController {
	
	@Autowired
	private UserService userService;
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@GetMapping("/user/{id}")
	public User getUser(@PathVariable Integer id) {
		return userService.findUserById(id);
	}
	/**
	 * 查询所有用户
	 * @return
	 */
	@GetMapping("/user")
	public List<User> get(){
		return userService.find();
	}
}

二、出现的问题

1、问题

代码的大致结构基本写完,然后看似也没什么问题,把程序运行起来,去访问,就会发现

但是呢我数据库里是有数据的,接下来再访问一下查询全部用户的接口

现在问题就是我这的id全是0,这肯定是不对的,看控制台打印的sql语句会发现没有id那个字段

2、原因及解决方法

经过排查,发现问题出现的原因是我实体类中定义的id类型是基本数据类型int
解决方法把int类型改为它的包装类Integer类型,问题就解决了
修改User类中的类型

运行结果

三、总结

【注意】在开发过程中,实体类的属性类型尽量不要使用基本类型,用相应的包装类来代替,避免出现一些奇怪的问题

posted @ 2020-01-13 11:17  是程序媛吖  阅读(6195)  评论(0编辑  收藏  举报