Mybatis代码生成器配置
相关依赖
<properties>
<java.version>1.8</java.version>
<mysql.version>5.1.47</mysql.version>
</properties>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<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>src/main/resources/mybatis/generator-config.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
配置文件 application.yml
server:
port: 8180
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
username: root
password: "0000"
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.li.back.domain
mapper-locations: com.li.back.mapper.*
configuration:
map-underscore-to-camel-case: true
代码生成配置文件 generator-config.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>
<context id="MySQLTables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/shop?useUnicode=yes&characterEncoding=UTF-8&useSSL=false"
userId="root"
password="0000">
</jdbcConnection>
<javaModelGenerator targetPackage="com.li.back.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.li.back.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.li.back.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table schema="shop" tableName="t_user" domainObjectName="User"></table>
<table schema="shop" tableName="t_menu" domainObjectName="Menu"></table>
<table schema="shop" tableName="t_role" domainObjectName="Role"></table>
<table schema="shop" tableName="t_menu_role" domainObjectName="Menurole"></table>
</context>
</generatorConfiguration>
点击插件生成相应的代码

Mybatis 通用Mapper
- 使用
Mybatis
代码生成器具有一定的优势,但是当表中添加字段后,就得要重新生成。使用Mybatis
通用Mapper
可以满足对单表的通用增删改查操作。
引入依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
实体类
@Table(name = "testone")
public class TestOne {
private Integer id;
private String name;
private Integer age;
private String sno;
}
Mapper 定义
public interface TestOneMapper extends Mapper<TestOne> {
}
测试代码
@Autowired
private TestOneMapper testOneMapper;
@Override
public void testOne() {
TestOne testOne = new TestOne();
testOne.setId(1);
List<TestOne> testOne1 = testOneMapper.select(testOne);
logger.info(JSON.toJSONString(testOne1));
}
出现报错
2021-04-22 17:07:26.617 ERROR 20884 --- [nio-8088-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method 'public java.lang.String tk.mybatis.mapper.mapperhelper.MapperTemplate.dynamicSQL(java.lang.Object)' with specify parameter 'class com.li.entity.test.TestOne'. Cause: java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider.<init>()] with root cause
java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_181]
- 解决方法:将
SpringBoot
启动类上mapper的注解包换为import tk.mybatis.spring.annotation.MapperScan;
问题解决。
可以用的方法有:有点Jpa的味道
"delete","insert","selectByExample","deleteByExample","updateByExample","selectCount","insertSelective","selectByPrimaryKey","updateByPrimaryKey","deleteByPrimaryKey","existsWithPrimaryKey","selectOneByExample","selectCountByExample","updateByPrimaryKeySelective","selectByRowBounds","selectByExampleAndRowBounds","updateByExampleSelective","selectOne","selectAll","select"