一、先记录正确配置

1、pom.xml添加依赖

     <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.5</version>
        </dependency>
       <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.23</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

2、维护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>
    <classPathEntry
            location="D:\WorkSpace\mysql-connector-java-8.0.23.jar" />
    <!-- 属性赋值在tk.mybatis.mapper.generator.MapperPlugin
      属性例如javaModelGenerator/javaClientGenerator在org.mybatis.generator.config
--> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="javaFileEncoding" value="UTF-8"/> <!-- 开始的分隔符,例如mysql为`,sqlserver为[ --> <property name="beginningDelimiter" value="`"/> <!-- 结束的分隔符,例如mysql为`,sqlserver为] --> <property name="endingDelimiter" value="`"/> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <!-- 继承的父类Mapper --> <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> <!-- 区分大小写 --> <property name="caseSensitive" value="true"/> <!-- 是否配置swagger 默认false,如果true,会添加@ApiModelProperty和@ApiModel --> <property name="swagger" value="true" /> <!-- value有:Data、Getter、Setter、ToString、EqualsAndHashCodeAccessors --> <property name="lombok" value="Data" /> </plugin> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/aaaa" userId="root" password="root"> <property name="nullCatalogMeansCurrent" value="true" /> </jdbcConnection> <javaModelGenerator targetPackage="com.xiufengd.domain" targetProject="src/main/java"/> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <javaClientGenerator targetPackage="com.xiufengd.mapper" targetProject="src/main/java" type="XMLMAPPER"/> <!-- 生成类名用domainObjectName定义,如果不设置,则默认用表明的驼峰 可以用 domainObjectRenamingRule 和 columnRenamingRule 对类名或者字段名进行替换重命名
    注意:domainObjectName与domainObjectRenameingRule不共用,当存在重命名时,不会对生成的类进行规则修改 如果用了domainObjectRenameingRule后,类名不再驼峰,说明版本有问题,1.3.7有bug,换1.3.6
--> <table tableName="sys_user" domainObjectName="User"> <generatedKey column="id_" sqlStatement="JDBC"/>
       <domainObjectRenamingRule searchString="Test" replaceString="" />
       <columnRenamingRule searchString="d" replaceString="ddddd" />
</table>
  </context>
</generatorConfiguration>

 

3、运行

 

 

 

 得到正确结果,目录结构如下

 

 

 二、记录问题点

  1、mybatis-generator-maven-plugin用版本1.3.7,用最新的1.4.0会出错,错误如下:

Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.4.0:generate (default-cli) on project itoken-service-admin: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.4.0:generate failed: A required class was missing while executing org.mybatis.generator:mybatis-generator-maven-plugin:1.4.0:generate: org/mybatis/generator/api/dom/xml/Element

  2、application中扫描mapper应该用 import tk.mybatis.spring.annotation.MapperScan 而不是 org.mybatis.spring.annotation.MapperScan,否则会报错,错误如下:

java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider

  3、当连接中存在好多个数据库时,加载的表是第一个数据库的同名表,而不是指定数据库的,即 generator 链接数据库不对,此时应该在<jdbcConnection></jdbcConnection>中添加配置

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

原因官方文档给出了解释 http://mybatis.org/generator/usage/mysql.html

谷歌翻译为:

 

 三、总结

  1、有些框架真不是越新了越好,如果追求稳定还是用相对稳定的版本。

  2、多看官方文档、官方文档很多时候给了解决方案了。

 

posted on 2021-02-19 17:21  程序员丁先生  阅读(518)  评论(0编辑  收藏  举报