前言:
使用SpringBoot很方便,但经常因为一个业务而写很多SQL语句(很多都是CRUD),而怎样才能不写SQL语句或很少写呢?
最近发现了一款好玩的mybatis插件(通用Mapper)tk-mybatis,它解决了上面的这些问题。
介绍:
tk-mybatis由国人编写,码云地址

https://gitee.com/free

作者还写了使用文档,地址

https://gitee.com/free/Mapper/wikis/Home

优点:

  • 通用Mapper极大的方便开发人员
  • 可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法
  • 极其方便的使用MyBatis单表的增删改查
  • 自动生成常用的增删改查的SQL语句
  • 支持单表操作,不支持通用的多表联合查询。

比较:
与MPG插件(mybatis plus generator )的比较:

  • 使用MPG时:当数据库表的字段被人为修改后,需要重新使用MPG插件生成*.xml,而tk-mybatis解决了这个问题,它会根据表的字段信息动态修改,当你修改了数据库,不用操心要不要修改mapper。

一.SpringBoot集成tk-mybatis

与SpringBoot的集成,可分为以下几步:

  1. 添加tk-mybatis依赖
<dependency>
  <groupId>tk.mybatis</groupId>
  <artifactId>mapper-spring-boot-starter</artifactId>
  <version>2.1.5</version>
</dependency>

注意:引入该 starter 时,和 MyBatis 官方的 starter 没有冲突,你不用担心已经依赖的mybatis版本,只需要加入上面的依赖即可,但是官方的自动配置不会生效!

  1. 创建pojo、mapper、service、controller包,目录结构如下:
    在这里插入图片描述

pojo->User.java

//指定表名
@Table(name = "user")
public class User {
//表的主键
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    //使用包装类
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

mapper->UserMapper.java

import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

@Repository
public interface UserMapper extends Mapper<User> {
}

service->UserService.java

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public User getUserById(int id){
        //使用tk-mybatis提供的selectByPrimaryKey方法
        return userMapper.selectByPrimaryKey(id);
    }
    public List<User> getAllUser(){
        //使用tk-mybatis提供的selectAll方法
        return userMapper.selectAll();
    }
}

controller->UserController.java

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("get/{id}")
    public User getUserById(@PathVariable int id){ return this.userService.getUserById(id);}

    @GetMapping("getAll")
    public List<User> getAll(){ return this.userService.getAllUser(); }
}
  1. 在主类添加tk-mybatis的MapperScan扫描mapper
//注意是tk的MapperScan
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
//扫描mapper
@MapperScan("com.example.mapper")
public class Springbootdemo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Springbootdemo1Application.class, args);
    }
}

这样就好了,启动服务器请求数据:

http://localhost:8080/user/get/1

结果:

在这里插入图片描述

http://localhost:8080/user/getAll

结果:
在这里插入图片描述

二.tk-mybatis使用方法总结

1.实体类字段需使用包装类,不要使用基本数据类型
使用通用mapper进行查询的时候,如果对象中的属性类型不是包装类型,如int,long等,在查询的时候,没有作为条件查询,但执行的时候,仍然会拼接该字段进行查询,就会出现查不到数据的情况。因此,在建立实体类的时候,一定要用包装类型接收。
例如自增的id,要用Integer或者Long;如果用int或者long,在使用通用mapper查询时就会自动拼接id作为查询条件,从而拿不到数据。这是一个开发过程中可能遇到的坑。

2.常用方法
查询 select

方法名作用
List <T> selectAll();查询所有数据
T selectByPrimaryKey(Object key);通过主键查询单个数据
T selectOne(T record);通过实体查询单个数据
List<T> select(T record);通过实体查询多个数据
int selectCount(T record);通过实体查询实体数量
boolean existsWithPrimaryKey(Object key);通过主键查询此实体是否存在

其他常用的方法用到再列出,还可以去文档地址查看提供的方法。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐