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

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

项目结构如下所示(就是resource里面的结构)
在这里插入图片描述
首先是在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&amp;useSSL=false&amp;serverTimezone=UTC&amp;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了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值