1、添加依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2、启动类换注解
MapperScan使用tkmapper的
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("xxxx.dao")
3、创建generalDao,此dao会配置在generatorConfig.xml中
package com.yogo.general;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface GeneralDao<T> extends Mapper<T>, MySqlMapper<T> {
}
4、创建配置文件generatorConfig.xml
我用的springboot2.7会报错,逆向生成了很多系统表,在jdbcConnection标签中添加以下代码: <property name="nullCatalogMeansCurrent" value="true"/>
以下为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>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value=""/> <property name="endingDelimiter" value="
"/>
<!-- 配置 GeneralDAO -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.yogo.general.GeneralDao"/>
</plugin>
<!-- 配置数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/fmmall"
userId="root" password="root1111">
<!-- 解决逆向结果重复问题-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 配置实体类存放路径 -->
<javaModelGenerator targetPackage="com.yogo.beans" targetProject="src/main/java"/>
<!-- 配置 XML 存放路径 -->
<sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mappers"/>
<!-- 配置 DAO 存放路径 -->
<javaClientGenerator targetPackage="com.yogo.dao" targetProject="src/main/java" type="XMLMAPPER"/>
<!-- 配置需要指定生成的数据库和表,% 代表所有表 -->
<table tableName="%"></table>
</context>
</generatorConfiguration>
5、添加逆向插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
<!--逆向需要的tkmapper依赖--> <dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
6、使用tkmapper
以下为tkmapper部分代码
Example example = new Example(Users.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("username",username);
List<Users> users = userDao.selectByExample(example);
以注册为例
@Transactional
@Override
public ResultVO register(String username, String password) {
synchronized (this){
Example example = new Example(Users.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("username",username);
List<Users> users = userDao.selectByExample(example);
if (users.size() == 0){
Users user = new Users();
user.setUsername(username);
user.setPassword(password);
user.setUserImg("img/defaultImg.png");
Date currentDate = new Date();
user.setUserRegtime(currentDate);
user.setUserModtime(currentDate);
int i = userDao.insertUseGeneratedKeys(user);
if (i>0){
return new ResultVO(10000,"注册成功",user);
}else {
return new ResultVO(10002,"注册失败",user);
}
}else {
return new ResultVO(10001,"用户名已经存在",null);
}
}
}