MyBatis Generator生成MyBatis3 Dynamic SQL风格代码

使用SpringBoot+gradle创建工程

ps:由于公司使用的gradle,之前也没有用过

项目目录如下:

 

 

build.gradle

在resources下创建 mybatis-generator.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>
    <!--mybatis targetRuntime选择 该测试需要MyBatis3DynamicSQL-->
    <context id="MySql" targetRuntime="MyBatis3DynamicSQL">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!--数据库连接-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=UTC&useSSL=false"
                        userId="root" password="root"/>
        <!--类型解析器-->
        <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
            <!--是否对整型进行分类-->
            <property name="forceBigDecimals" value="true"/>
            <!--是否试用jdk8时间类-->
            <property name="useJSR310Types" value="true"/>
        </javaTypeResolver>
        <!--java实体类生成配置-->
        <javaModelGenerator targetPackage="com.bitech.mybatisds.entity" targetProject="src\main\java">
            <!--此属性用于选择MyBatis Generator是否将根据内省表的目录和架构为对象生成不同的Java包。-->
            <property name="enableSubPackages" value="true"/>
            <!--去除字段空格-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--mapper生成配置-->
        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.bitech.mybatisds.mapper" targetProject="src\main\java">
            <!--此属性用于选择MyBatis Generator是否将根据内省表的目录和架构为对象生成不同的Java包。-->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--数据表的选择-->
        <table schema="mybatisplus" tableName="user"/>
        <table schema="mybatisplus" tableName="test"/>
    </context>
</generatorConfiguration>

  

 在util包中创建MybatisGenerator

 

 

运行main方法生成数据库中对应的实体类以及相关类

ps:mysql驱动选择8.0时,生成user表会被默认的mysql库中的user表覆盖

 

posted @ 2021-03-03 16:52  少年阿鑫  阅读(425)  评论(0)    收藏  举报