MyBatis Generator的使用

问题

  • targetProject路径不存在
描述

配置的targetProject=supermarket-common/src/main/java在运行时错误:[WARNING] The specified target project directory supermarket-common/src/main/java does not exist

解决方法

targetProject是相对路径targetProject=src/main/java。不清楚具体原理,仅在maven环境中通过

  • 无法找到rootClass
描述

在xml中配置model类的超类时

        <javaModelGenerator targetPackage="${basicPackageTest}.model"
            targetProject="${targetProject}">
            <property name="rootClass" value="${basicPackage}.base.BaseDO"/>
        </javaModelGenerator>

运行时提示[WARNING] Root class cn.navyd.app.supermarket.base.BaseDO cannot be loaded, checking for member overrides is disabled for this class,将会导致超类字段仍然存在model类中

public class productDO extends BaseDO {
    // BaseDO.id
    private Integer id;
    private String name;
解决方法

将对应的root class加载到class path中,使用元素classPathEntry

<generatorConfiguration>
    <properties resource="generatorConfig.properties"/>
    <!-- 加载到classpath -->
    <classPathEntry location="/home/navyd/Workspace/eclipse/project-workspace/supermarket/supermarket-common/target/supermarket.jar.original" />

此时的model类已经成功的包含了超类字段

public class productDO extends BaseDO {
    private String name;
注意

如果是使用Spring Boot 打包的jar,会导致generator无法识别,应该使用原始的jar

  • mysql多个数据库的同名表的选择
描述

在mysql中存在supermarket_background.product, store.product两个同名表的数据库,即使在jdbc url: db.url=jdbc:mysql://localhost:3306/supermarket_background?useSSL=false中指定数据库,generator不会选择该数据库,仍然会搜索所有数据库中的表,导致不断覆盖或生成不同的文件

如果在<table中指定catalog,catalog为数据库名称,会导致generator生成奇怪的语法database..tableName(该行为应该产生在mysql Connector/J version>=8.0)。

        <table tableName="product"
            catalog="${db.catalog}"
            domainObjectName="productDO"
            mapperName="ProductDao"
            sqlProviderName="ProductSqlProvider">

在所有的sql中使用表时都会存在的语法:database..tableName ==> supermarket_background..product

    @Delete({
        "delete from supermarket_background..product",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);
解决方法

由于mysql不存在schema与catalog的概念,应该禁用使用schema与catalog,不会再导致选择多个数据库表的问题<property name="nullCatalogMeansCurrent" value="true" />

注意应该不再使用<table catalog="catalog"属性,否则仍然会产生奇怪的语法database..tableName

        <jdbcConnection
            driverClass="${db.driverClass}"
            connectionURL="${db.url}" 
            userId="${db.username}" 
            password="${db.password}">
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>
<!-- ... -->
        <table tableName="product"
            domainObjectName="productDO"
            mapperName="ProductDao"
            sqlProviderName="ProductSqlProvider">
            <property name="useActualColumnNames" value="false" />
参考

MySql Usage Notes Unsigned Fields

Mybatis Generator: How to generate all tables for a specified schema

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容