首先, pom不能引用mybatis-spring-boot-starter, 加入tk mybatis依赖:
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
第二, 在启动类application.java那里, import的MapperScan改为import tk.mybatis.spring.annotation.MapperScan;
第三, mapper改为这种泛型的风格 : public interface TaUserMapper extends Mapper<TaUser> { }
而在pom.xml里面配置插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/tk-mybatis-autogen.xml
</configurationFile>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
在resource下建立tk-mybatis-autogen.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="C:\Users\dgmislrh\.m2\repository\org\postgresql\postgresql\42.2.5\postgresql-42.2.5.jar"/>
<!--如果你希望不生成和Example查询有关的内容,那么可以按照如下进行配置: targetRuntime="MyBatis3Simple"-->
<context id="context" targetRuntime="MyBatis3Simple">
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
</plugin>
<commentGenerator>
<!--是否去除生成的自动注解(英文的) true是去除 false 不去除-->
<property name="suppressAllComments" value="true"/>
<!-- 是否去除自动生成的日期 true:是 : false:否 -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection userId="postgres" password="xxxx" driverClass="org.postgresql.Driver" connectionURL="jdbc:postgresql://xxxx:5432/xxxdb"/>
<!--
true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
false:默认,
scale>0;length>18:使用BigDecimal;
scale=0;length[10,18]:使用Long;
scale=0;length[5,9]:使用Integer;
scale=0;length<5:使用Short;
-->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- !!!! Model Configurations !!!! -->
<!--生成POJO类的位置-->
<javaModelGenerator targetPackage="com.xx.common.modules.system.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!! Mapper XML Configurations !!!! -->
<!--生成映射文件xml的位置-->
<sqlMapGenerator targetPackage="mappings.modules.ta" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- !!!! Mapper Interface Configurations !!!! -->
<!--mapper.xml 接口生成的位置-->
<javaClientGenerator targetPackage="com.xxx.common.modules.system.dao" type="XMLMAPPER" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--
schema即为数据库名, tableName为数据库中的对应的数据库表名或视图名, domainObjectName是要生成的实体类名,
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,
就不会生成对应的Example类了.
table其他属性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
如果table里边不配置property,默认字段都生成为类属性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。
-->
<!-- !!!! Table Configurations !!!! -->
<!-- 下面这个是生成该数据库下的所有的数据表的 -->
<table schema="public" tableName="tc_user_access" enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
展开maven, 运行generator.