springboot 2.2.1+mybatis使用逆向工程生成代码(可不包含example)

之前一直感觉mybatis的逆向工程非常的鸡肋,但是今天试了一下,确实比平常要方便一点点,现在就复盘一下啰,保存一下配置的xml,自己之后使用的时候可以直接来复制使用。

项目结构如下所示(就是resource里面的结构)
springboot 2.2.1+mybatis使用逆向工程生成代码(可不包含example)_第1张图片
首先是在application.properties里面对mybatis的配置,以及pom文件的依赖。

#告知springboot去哪个包下面扫描xml的文件
mybatis.mapper-locations=classpath:mapper/*.xml
#开启驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true
#实体类的别名配置
mybatis.type-aliases-package=com.xxx.xxx.entity
<!--    逆向工程依赖    -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>

然后在resource下面建一个用于逆向工程对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="DB2Tables" targetRuntime="MyBatis3">
        <!--去掉注释-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--需要配置数据库连接-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://xxx:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true"
                        userId="xxx"
                        password="xxx">
        </jdbcConnection>
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--指定entity生成的位置-->
        <javaModelGenerator targetPackage="com.xxx.xxx.entity" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--sql映射文件生成的位置 mapper.xml-->
        <sqlMapGenerator targetPackage="mapper"  targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--指定DaoMapper生成的位置 mapper.java-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.xxx.dao" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--这种方式不会有example,建议选择这种方式-->
        <table tableName="customer_service_config" domainObjectName="CustomerServiceConfig" enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
               <!--这种方式就会有example-->
        <table tableName="customer_service_config" domainObjectName="CustomerServiceConfig" ></table>

    </context>
</generatorConfiguration>

然后如果装了插件其实可以直接就右键然后run,如果没有,其实可以直接写一个测试方法跑一遍下面的代码

@Test
    public void test() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
       List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("./src/main/resources/generatorConfig.xml");//直接放在文件下面,与pom.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);
    }

OK,跑一下就OK了

你可能感兴趣的:(试验)