Mybatis generator(复制粘贴完成)

命令行模式

1、java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml

2、Maven plugin(mybatis-generator-maven-plugin) 常用

2.1、mvn mybatis-generator:generate

2.2、${basedir}/src/main/resources/generatorConfig.xml 默认读取这个文件,所以新建项目的话,直接添加这个文件即可。

3、java 程序

4、Ant Task 少用

generatorConfiguration

context

1、jdbcConnection

2、javaModelGenerator

3、sqlMapGenerator

4、javaClientGenerator(注解方式/xml方式/混合方式),简单的用注解方式,用到一些例子的话用xml方式

5、table

生成时可以使用的插件(内置插件都在org.mybatis.generator.plugins中)

1、FluentBuilderMethodsPlugin

2、ToStringPlugin

3、SerializablePlugin

4、RowBoundsPlugin(分页有用)

...

使用生成对象

1、简单操作,直接使用生成的xxxMapper的方式

2、复杂查询,使用生成的xxxExample对象


说了那么多,具体操作可以直接做以下2个步骤即可。

1、在springboot的启动类上,添加如下代码

2、加上一个配置文件

使用了java代码的方式进行生成代码

@SpringBootApplication
public class PointReportApplication implements CommandLineRunner
{

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

    @Override
    public void run(String... strings) throws Exception {
        generateArtifacts();
    }

    private void generateArtifacts() throws Exception {
        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(
                this.getClass().getResourceAsStream("/generatorConfig.xml"));
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

}

创建一个xml文件,文件名称为:generatorConfig.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>
    <context id="H2Tables" targetRuntime="MyBatis3Simple">
        <plugin type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin" />
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://119.23.209.247/test?truecharacterEncoding=UTF-8"
                        userId="root"
                        password="asd222030">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.ky.pointreport.dto"
                            targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mybatis.mapper"
                         targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.ky.pointreport.dao"
                             targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
     
        <table tableName="jd_ky_consume_detail" domainObjectName="jdKyConsumeDetail" > 
            <generatedKey column="id" sqlStatement="CALL IDENTITY()" identity="true" />
        </table>
    </context>
</generatorConfiguration>
原文地址:https://www.cnblogs.com/vingLiu/p/11079767.html