mybatis 自动生成 mapper

Posted xmsx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis 自动生成 mapper相关的知识,希望对你有一定的参考价值。

生成命令

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 配置项

以上是关于mybatis 自动生成 mapper的主要内容,如果未能解决你的问题,请参考以下文章

使用maven的mybatis-generator代码生成器插件生成实体类mapper配置文件和mapper接口(使用idea)

mybatis-plus小技能:代码生成器(增加Mapper注解和Fill填充)

mybatis-plus小技能:代码生成器(增加Mapper注解和Fill填充)

idea怎么从mapper进xml

Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类

mybatis 自动生成 mapper