mybatis-generator 代码生成工具

发布时间 2023-07-14 19:54:20作者: 花开如梦

官网文档:http://mybatis.org/generator/quickstart.html

 

引入依赖:

   <!-- 代码生成工具https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.2</version>
        </dependency>

 

配置文件:

<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!--如果需要使用 command 的方式生成需要配置数据库驱动的jar包路径-->
    <!--   java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite  -->
<!--    <classPathEntry location="指定数据驱动的磁盘路径"/>-->

    <!--
        context 生成上下文 配置生成规则
            id:唯一标识 随意写
            targetRuntime:生成策略
                MyBatis3DynamicSql 默认的 会生成带动态sql的CRUD
                MyBatis3Kotlin
                MyBatis3            生成通用的查询,可以指定动态where条件
                MyBatis3Simple      只生成简单的CRUD
    -->
    <context id="simple" targetRuntime="MyBatis3Simple">
        <!--数据源-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mytest"
                        userId="root"
                        password="123456"/>
        <!-- pojo JAVA实体生成的规则
            targetPackage:生成到哪个包下面
            targetProject:当前文件的哪个相对路径下
        -->
        <javaModelGenerator targetPackage="com.mybatise.test.generator.pojo" targetProject="src/main/java"/>


        <!--mapper xml映射文件 生成的规则
              targetPackage:生成到哪个包下面
            targetProject:当前文件的哪个相对路径下
        -->
        <sqlMapGenerator targetPackage="com.mybatise.test.generator.pojo" targetProject="src/main/resources"></sqlMapGenerator>
        <!--mapper 接口 生成的规则
            targetPackage:生成到哪个包下面
            targetProject:当前文件的哪个相对路径下
            type:指定生成的方式
                1.使用注解的方式
                2.使用接口绑定的方式生成 (要配置sqlMapGenerator)
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatise.test.generator.mapper" targetProject="src/main/java"/>
        <!--配置哪些表进行代码生成
                tableName:表明
                domainObjectName:指定pojo 类名 (可省略自动生成)
                mapperName:指定mapper mapper接口 和 xml映射文件 用的同一个名字 (可省略自动生成)
        -->
        <table tableName="mytable"/>
        <table tableName="person"/>
    </context>
</generatorConfiguration>

 

代码生成方式:

    @Test
    public void test01() throws XMLParserException, IOException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src/main/resources/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }