mybatis 自动生成 mapper

时间:2022-01-19 02:51:31

目录

生成命令

java -jar generator.jar -configfile generator.xml -overwrite

oracle 的配置

<?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>
    <classPathEntry location="ojdbc14.jar" />
    <context id="context1">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@139.219.199.9:1521:coaltest" userId="fw_data_user" password="123456" />
        <javaModelGenerator targetPackage="com" targetProject="src" />
        <sqlMapGenerator targetPackage="com" targetProject="src" />
        <javaClientGenerator type="XMLMAPPER" targetPackage="com" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="T_PF_CHECK_RESULT" domainObjectName="PfCheckResult"
            delimitIdentifiers="true" delimitAllColumns="true"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

mysql 的配置

<?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>
    <classPathEntry location="mysql-connector-java-8.0.13.jar" />
    <context id="context1">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://192.168.2.163:3306/gpftest" userId="root" password="123456" />
        <javaModelGenerator targetPackage="com" targetProject="src" />
        <sqlMapGenerator targetPackage="com" targetProject="src" />
        <javaClientGenerator type="XMLMAPPER" targetPackage="com" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="t_user" domainObjectName="User"
            delimitIdentifiers="true" delimitAllColumns="true"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

配置更新:

<?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>
    <classPathEntry location="mysql-connector-java.jar" />
    <context id="context1">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://ip:3306/dbname" userId="" password="">
            <property name="useInformationSchema" value="true"/>
        </jdbcConnection>
        <javaModelGenerator targetPackage="com" targetProject="src" />
        <sqlMapGenerator targetPackage="com" targetProject="src" />
        <javaClientGenerator type="XMLMAPPER" targetPackage="com" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <table tableName="base_meta_instance_resource"
            domainObjectName="BaseMetaInstanceResource"
            delimitIdentifiers="true"
            delimitAllColumns="true"
            enableSelectByPrimaryKey="true"
            enableInsert="true"
            enableDeleteByPrimaryKey="true"
            enableUpdateByPrimaryKey="true">
        </table>
    </context>
</generatorConfiguration>

解决问题

生成的 mapper.xml 中有重复代码

解决办法:xml 配置中,去掉下面代码

<property name="suppressAllComments" value="true"/>

无法生成 selectByPrimaryKey、deleteByPrimaryKey 等方法

将 mysql 驱动的版本降低到 5.x,尚未验证

or

添加下面的标签(有效,推荐使用)

<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="" userId="" password="">
    <property name="useInformationSchema" value="true"/>
</jdbcConnection>

参考文章

相关文章

mytatis 自动生成工具

生成器 XML 配置项