1、添加依賴
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2靠粪、啟動(dòng)類換注解
MapperScan使用tkmapper的
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("xxxx.dao")
3拳昌、創(chuàng)建generalDao间驮,此dao會(huì)配置在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漱病、創(chuàng)建配置文件generatorConfig.xml
我用的springboot2.7會(huì)報(bào)錯(cuò)浊洞,逆向生成了很多系統(tǒng)表惜姐,在jdbcConnection標(biāo)簽中添加以下代碼: <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>
<!-- 配置數(shù)據(jù)庫(kù)連接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/fmmall"
userId="root" password="root1111">
<!-- 解決逆向結(jié)果重復(fù)問(wèn)題-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 配置實(shí)體類存放路徑 -->
<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"/>
<!-- 配置需要指定生成的數(shù)據(jù)庫(kù)和表犁跪,% 代表所有表 -->
<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);
以注冊(cè)為例
@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,"注冊(cè)成功",user);
}else {
return new ResultVO(10002,"注冊(cè)失敗",user);
}
}else {
return new ResultVO(10001,"用戶名已經(jīng)存在",null);
}
}
}