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

Stella981
• 阅读 529

###一、整合步骤 ####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、问题 代码的大致结构基本写完,然后看似也没什么问题,把程序运行起来,去访问,就会发现 Spring Boot踩坑笔记一:Spring Boot整合mybatis和通用Mapper遇到的坑

但是呢我数据库里是有数据的,接下来再访问一下查询全部用户的接口 Spring Boot踩坑笔记一:Spring Boot整合mybatis和通用Mapper遇到的坑

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

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

####2、原因及解决方法 经过排查,发现问题出现的原因是我实体类中定义的id类型是基本数据类型int 解决方法把int类型改为它的包装类Integer类型,问题就解决了 修改User类中的类型 Spring Boot踩坑笔记一:Spring Boot整合mybatis和通用Mapper遇到的坑

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

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

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot集成Mybatis连接数据库防止后面忘记1.添加Mybatis和Mysql依赖      <dependency    <groupIdorg.mybatis.spring.boot</groupId    <artifactIdmybatisspringbootst
Easter79 Easter79
2年前
SpringBoot权限管理开发实战2
1.添加依赖<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarter</artifactId<version2.1.1</version</dependency<dependency<groupI
Stella981 Stella981
2年前
SpringBoot持久层技术
一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId
Easter79 Easter79
2年前
SpringBoot持久层技术
一、Springboot整合mybatismaven中添加对数据库与mybatis的依赖1<dependencies2<dependency3<groupIdorg.springframework.boot</groupId4<artifactId
Stella981 Stella981
2年前
SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis
如何整合MyBatis1、pom依赖<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarte
Stella981 Stella981
2年前
Spring Boot整合redis
一、添加依赖<!SpringBoot整合redis的依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarter
Easter79 Easter79
2年前
Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常
Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常参考文章:(1)Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常(https://www.oschina.net/actio
Easter79 Easter79
2年前
SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis
如何整合MyBatis1、pom依赖<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarte
Stella981 Stella981
2年前
Spring Boot整合第三方框架
1、整合jdbc1.1导入maven依赖<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringbootstarterjdbc</artifactId</
Stella981 Stella981
2年前
SpringBoot权限管理开发实战2
1.添加依赖<dependency<groupIdorg.mybatis.spring.boot</groupId<artifactIdmybatisspringbootstarter</artifactId<version2.1.1</version</dependency<dependency<groupI