zoukankan      html  css  js  c++  java
  • springboot整合 mybatis.generator

    一、先记录正确配置

    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="E:mysql-connector-java-8.0.23.jar" />
    
        <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
            <property name="javaFileEncoding" value="UTF-8"/>
            <property name="beginningDelimiter" value="`"/>
            <property name="endingDelimiter" value="`"/>
            <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
                <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
                <property name="caseSensitive" value="true"/>
            </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"/>
            <table tableName="sys_user">
                <generatedKey column="id_" sqlStatement="JDBC"/>
            </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、多看官方文档、官方文档很多时候给了解决方案了。

  • 相关阅读:
    二维坐标离散化
    unique()函数使用
    如果你的unordered_map头文件报错请看这里
    string删除与查找erase,find
    2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest K
    2017-2018 ACM-ICPC Pacific Northwest Regional Contest (Div. 2) P-Fear Factoring 区间内数的所有因数的和(除法分块)
    .NETCore使用EntityFrameworkCore连接数据库生成实体
    sql server 触发器开发
    Windows 配置定时任务 和任务无法执行原因分析
    sql 的存储过程和事务
  • 原文地址:https://www.cnblogs.com/xiufengd/p/14417139.html
Copyright © 2011-2022 走看看