MyBatis框架 - 逆向工程

  • 没有MyBatis的逆向工程:实体类 --> 持久层接口类 --> 业务层接口类、实现类 --> resources下的*Mapper配置文件,需要自己手动写。
  • 有MyBatis的逆向工程,这个过程可以省略,使用逆向工程直接生成。

逆向工程的配置文件

图中红框的位置要与事件的项目匹配正确:

xml 复制代码
<context id="mybatisGenerator" >
    <commentGenerator>
        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <property name="suppressAllComments" value="true" />
    </commentGenerator>
    <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    <!--与spring的配置文件中的数据源无关-->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                    connectionURL="jdbc:mysql://localhost:3306/mybatis_db?serverTimezone=CTT&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true" userId="root"
                    password="root">
    </jdbcConnection>

    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
        NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver>
        <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!-- targetProject:生成PO类的位置 -->
    <javaModelGenerator targetPackage="com.tx.entity"
                        targetProject=".\mybatisJava05\src\main\java">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
        <!-- 从数据库返回的值被清理前后的空格 -->
        <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- targetProject:mapper映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="mapper"
                     targetProject=".\mybatisJava05\src\main\resources">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </sqlMapGenerator>
    <!-- targetPackage:mapper接口生成的位置 -->
    <javaClientGenerator type="XMLMAPPER"
                         targetPackage="com.tx.dao"
                         targetProject=".\mybatisJava05\src\main\java">
        <!-- enableSubPackages:是否让schema作为包的后缀 -->
        <property name="enableSubPackages" value="false" />
    </javaClientGenerator>
    <!-- 指定数据库表 -->
    <table tableName="user" enableCountByExample="false"
           enableUpdateByExample="false" enableDeleteByExample="false"
           enableSelectByExample="false" selectByExampleQueryId="false"/>


    <!-- 有些表的字段需要指定java类型
     <table schema="" tableName="">
        <columnOverride column="" javaType="" />
    </table> -->
</context>

反向类

java 复制代码
package com.tx.utils;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Generator {
    public void generator() throws Exception  {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        // 执行逆向工程配置文件
        File configFile = new File("mybatisJava05/src/main/resources/generatorConfig.xml");
        ConfigurationParser parser = new ConfigurationParser(warnings);
        // 读取配置文件
        Configuration config = parser.parseConfiguration(configFile);
        // 回调
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);
    }

    public static void main(String[] args) throws Exception {
        try {
            Generator generatorSqlmap = new Generator();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

执行逆向工程配置文件目录要匹配正确,运行反向类后,会自动生成User实体类、UserMapper接口、UserMapper.xml文件

注意:实体类中需要添加有参构造器、无参构造器、重写toString()方法,重点是让实体类继承Serializable

相关推荐
m0_748554812 小时前
golang如何实现用户订阅偏好管理_golang用户订阅偏好管理实现总结
jvm·数据库·python
lee_curry3 小时前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
早日退休!!!3 小时前
《数据结构选型指南》笔记
数据结构·数据库·oracle
xcLeigh4 小时前
KES数据库性能优化实战
数据库·sql·性能优化·sql优化·数据性能
阿正呀4 小时前
Redis怎样实现本地缓存的高效失效通知
jvm·数据库·python
yoyo_zzm4 小时前
Laravel9.x新特性全解析
数据库·mysql·nginx
九转成圣4 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
2501_901200534 小时前
mysql如何设置InnoDB引擎参数_优化innodb_buffer_pool
jvm·数据库·python
直奔標竿4 小时前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring
金銀銅鐵5 小时前
[java] 编译之后的记录类(Record Classes)长什么样子(上)
java·jvm·后端