嘘~ 正在从服务器偷取页面 . . .

SpringBoot TK.Mybatis-generator


TK.Mybatis-generator 代码生成器

一、代码生成工具

Mybatis 生成代码工具有效提交了开发效率。 MyBatis 代码生成器简称 MBG

常见的三种工具:

  • Mybatis-Generator
  • TK.Mybatis-Generator
  • Mybatis-Plus-generator

本文要整理的是 TK.Mybatis-Generator 。

早起写的通用 Mapper,后面改了包名,叫TK.Mybatis。

当然除了这三种之外,也有其他第三方工具。

二、介绍

官网:https://mybatis.io/

Git仓库:https://github.com/abel533/Mapper/wiki/1.integration

通用Mapper 的 Github 文档:https://github.com/abel533/Mapper/wiki

Gitee仓库:https://gitee.com/free/Mapper

通用Mapper 的 Gitee 文档:https://gitee.com/free/Mapper/wikis/Home

三、代码生成

新建springboot工程。

1、引入依赖:

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.5</version>
        </dependency>

为了方便看,记录一下完整依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xiaocai</groupId>
    <artifactId>mybatis-tk-generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis-tk-generator</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.5</version>
        </dependency>

        <!-- Objenesis -->
        <dependency>
            <groupId>org.objenesis</groupId>
            <artifactId>objenesis</artifactId>
            <version>2.1</version>
        </dependency>

        <!--高性能反射工具-->
        <dependency>
            <groupId>com.esotericsoftware</groupId>
            <artifactId>reflectasm</artifactId>
            <version>1.11.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.22</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>

</project>

2、MySQL 准备

建库建表


create schema if not exists test collate utf8_general_ci;

create table if not exists user
(
    id bigint not null comment '主键ID'
        primary key,
    name varchar(30) null comment '姓名',
    age int null comment '年龄',
    email varchar(50) null comment '邮箱'
);


insert into test.user (id, name, age, email) values (1, 'Jone', 18, 'test1@gmail.com');
insert into test.user (id, name, age, email) values (2, 'Jack', 20, 'test2@qq.com');
insert into test.user (id, name, age, email) values (3, 'Tom', 28, 'test3@126.com');
insert into test.user (id, name, age, email) values (4, 'Sandy', 21, 'test4@163.com');
insert into test.user (id, name, age, email) values (5, 'Billie', 24, 'test5@qq.com');

3、增加配置

server.port=8804

# DataSource Config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8&useSSL=true&Unicode=true&characterEncoding=utf8&autoReconnectForPools=true&allowMultiQueries=true&rewriteBatchedStatements=true


mybatis.mapper-Locations=classpath:/mapper/*.xml,classpath:/mapper/**/*.xml"
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

4、添加XML

创建生成代码的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!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">
        <!--处理sql中的`符号-->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!--添加自定义的继承接口-->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.xiaocai.mybatistk.common.base.BaseMapper"/>
        </plugin>

        <!--数据源配置-->
        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
            <!-- mysql8 禁止生成器试图为MySql信息模式(sys,information_schema,performance_schema等)中的表生成代码 -->
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>

        <!--model包路径-->
        <javaModelGenerator targetPackage="com.xiaocai.mybatistk.model" targetProject="src/main/java"/>
        <!--mapper.xml包路径-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
        <!--mapper包路径-->
        <javaClientGenerator targetPackage="com.xiaocai.mybatistk.mapper" targetProject="src/main/java"
                             type="XMLMAPPER"/>
        <!--表配置,tableName支持%,表示全部生成-->
        <table tableName="user" domainObjectName="User">
            <!--mysql 配置-->
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
        </table>
    </context>
</generatorConfiguration>

5、生成代码

执行:

mvn mybatis-generator:generate

四、示例代码

mybatis-tk-generator



版权声明: 本博客所有文章除特別声明外,均采用 CC BY-SA 4.0 许可协议。转载请注明来源 Small-Rose / 张小菜 !
评论
  目录