Spring boot 整合 TK Mybatis(含代码生成器)

Spring boot 整合 TK Mybatis

前言

通用 Mapper4 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及Example 相关的单表操作。通用 Mapper 是为了解决 MyBatis 使用中 90% 的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。

Github 传送门

使用说明文档传送门

TK 代码生成器

通用 Mapper 专用代码生成器生成的 Model 会在原有基础上增加 @Table,@Id,@Column 等注解,方便自动会数据库字段进行映射。

使用 TK 代码生成插件

在 pom.xml 中添加插件

<!--        Mapper 代码生成插件   -->
<plugin>
    <!--  mybaits 代码生成插件  -->
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.6</version>

    <configuration>
        <!--  代码自动生成配置文件路径  -->
        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
        <!--  是否覆盖   -->
        <overwrite>true</overwrite>
        <!--  允许移动生成的文件  -->
        <verbose>true</verbose>
    </configuration>

    <dependencies>
        <!--  依赖库,生成代码时需要数据库驱动  -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>

        <!--  mapper 依赖库  -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.0.3</version>
        </dependency>
    </dependencies>
</plugin>
代码自动生成配置文件

创建代码自动生成配置文件 generatorConfig.xml

<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

    <!--  引用资源配置文件  -->
    <properties resource="application.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">

        <property name="javaFileEncoding" value="UTF-8"/>
        <property name="useMapperCommentGenerator" value="false"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <!-- 配置后生成的 Mapper 接口都会自动继承接口 value: 需要继承的接口, 该接口不可以在 MapperScan 扫描范围中-->
            <property name="mappers" value="com.example.demo.demo.IBaseCommMapper"/>
            <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
            <property name="caseSensitive" value="true"/>
            <property name="forceAnnotation" value="true"/>
        </plugin>


        <!-- 数据库连接属性: -->
        <jdbcConnection
                driverClass="${spring.datasource.driver-class-name}"
                connectionURL="${spring.datasource.url}"
                userId="${spring.datasource.username}"
                password="${spring.datasource.password}"/>

        <!-- MyBatis 生成器生成 Model -->
        <javaModelGenerator targetPackage="com.example.demo.demo.po"
                            targetProject="src/main/java"/>

        <!-- MyBatis 生成器生成 Mapper XML -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources"/>

        <!-- MyBatis 生成器生成 Mapper class -->
        <javaClientGenerator targetPackage="com.example.demo.demo.mapper"
                             targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <!--   需要生成的表名,  % 为通配符, -->
        <table tableName="%">
            <generatedKey column="id"
                          sqlStatement="select SEQ_{1}.nextval from %"
                          identity="false" type="pre"/>
        </table>
    </context>
</generatorConfiguration>
资源配置文件,添加数据库连接属性

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
代码生成

点击 mybatis-generator:generate 生成代码

在这里插入图片描述

使用 TK Mybatis

使用 TK Mybaits 不需要在 XML 中写大量的 SQL 语句,可以直接使用TK Mybaits 提供的方法进行数据操作.

TK Mybatis 依赖库
<!--   tk mybaits 依赖,包含 mybatis 依赖     -->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>

<!--   数据库驱动     -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
</dependency>

新建 IBaseCommMapper.class

  • 继承 IdsMapper 接口,实体类中必须只有一个带有 @Id 注解的字段
  • IBaseCommMapper 不可以在 MapperScan 扫描范围中
public interface IBaseCommMapper<T> extends Mapper<T>, ConditionMapper<T>, IdsMapper<T>, InsertListMapper<T> {
}
添加 mapper 扫描注解

在 Application 中使用 MapperScan 注解扫描 mapper 文件

**注意: MapperScan 使用的是tk.mybatis.spring.annotation.MapperScan 类,而不是 mybatis 的 MapperScan **

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
使用 TK Mybatis
@RestController
@RequestMapping("/user")
public class TestMapperController {

    @Autowired
    private UserMapper userMapper;


    @GetMapping("/select")
    @ResponseBody
    public Object select(){
        User user = new User();
        user.setId(1);
        return userMapper.select(policy);
    }
}
Example.Criteria 类说明

条件查询

方法说明
setOrderByClause添加升序排列条件,DESC为降序
setDistinct去除重复,boolean型,true为选择不重复的记录。
andIsNull添加字段xxx为null的条件
andIsNotNull添加字段xxx不为null的条件
andEqualTo添加xxx字段等于value条件
andNotEqualTo添加xxx字段不等于value条件
andGreaterThan添加xxx字段大于value条件
andGreaterThanOrEqualTo添加xxx字段大于等于value条件
andLessThan添加xxx字段小于value条件
andLessThanOrEqualTo添加xxx字段小于等于value条件
andIn添加xxx字段值在List<?>条件
andNotIn添加xxx字段值不在List<?>条件
andLike添加xxx字段值为value的模糊查询条件
andNotLike添加xxx字段值不为value的模糊查询条件
andBetween添加xxx字段值在value1和value2之间条件
andNotBetween添加xxx字段值不在value1和value2之间条件

持续学习更新中, 欢迎指教…

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值