一、tkMapper
实体类与数据表存在对应关系,并且是有规律的一一只要知道了数据表的结构,就能够生成实体类;
所有实体的DAO接口中定义的方法也是有规律的,不同点就是实体类型不同
- UserDAO
public interface UserDAO extends GeneralDAO<User>{
}
- GoodsDAO
public interface GoodsDAO extends GeneralDAO<Goods> {
}
- GeneralDAO
public interface GeneralDAO<T>{
//通用方法
public int insert(T t);
public T queryOneByPrimarykey(int i);
)
- 对于GeneralDAO接口定义的数据库操作方法因为使用了泛型,无需映射文件;对于UserDAO和GoodsDAO需要
映射文件,所有DAO的相同操作的映射文件也是有规律可循的 - UserMapper
<insert id="insert">
insert into users(user_id,username) values(#{userId},#{username})
</insert>
@Table("users")
public class User{
@Id
@Column("user_id")
private int userId;
@Column("username")
private String username;
}
- GoodsMapper
<insert id="insert">
insert into goods(goods_id,goodsrname) values(#{goodsId},#{goodsname})
</insert>
@Table("product")
public class User{
@Id
@Column("goods_id")
private int userId;
@Column("goodsname")
private String username;
}
二、tkMapper
基于MyBatis提供了很多第三方插件,这些插件通常可以完成数据操作方法的封装(GeneralDAO)、数据库逆向工程工作(根据数据表生成实体类、生成映射文件)
- MyBatis-plus
- tkMapper
tkMapper就是一个MyBatis插件,是在MyBatis的基础上提供了很多工具,让开发变得简单,提高开发效率。
- 提供了针对单表通用的数据库操作方法
- 逆向工程(根据数据表生成实体类、dao接口、映射文件)
三、tkMapper整合
3.1 基于SpringBoot完成MyBatis的整合
3.2整合tkMapper
3.2.1添加tkMapper的依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
3.2.2修改启动类的@MapperScan 注解的包
- 为 tk .mybatis .spring.annotation.MapperScan
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.qfedu.tkmapperdemo.dao")
public class TkmapperDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TkmapperDemoApplication.class, args);
}
}
四、tkMapper使用
4.1 创建DAO接口
tkMapper已经完成了对单表的通用操作的封装,封装在Mapper接口和MySqlMapper接口;因此如果我们要完成对单表的操作,只需自定义DAO接口继承Mapper接口和MySqIMapper接口
public interface UserDAO extends Mapper<User>, MySqlMapper<User> {
}
4.2 tkMapper提供的方法
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest {
@Autowired
private CategoryDAO categoryDAO;
@Test
public vold testInsert(){
Category category = new Category(日,"测试类别3",1,0,"03.png","xixi","aaa.jpg","black");
//int i = categoryDAO.insert(category);
int i = categoryDAO.insertUseGeneratedKeys(category);
System.out.println(category.getCategoryId());
assertEquals(1,i);
}
@Test
public void testUpdate(){
Category category = new Category(48,"测试类别4",1,0,"04.png","heihei","aaa.jpg","black");
int i = categoryDAO.updateByPrimaryKey(category);
//根据自定义条件修改,Example example就是封装条件的
// int i1 = categoryDAO.updateByExample( Example example);
assertEquals(1,i);
}
@Test
public void testDelete(){
int i = categoryDAO.deleteByPrimaryKey(48);
// 根据条件删除
//int i1 = categoryDAO.deleteByExample(Example example);
assertEquals(1,i);
}
@Test
public void testSelect1(){
//查询所有
List<Category> categories = categoryDAO.selectAll();
for (Category category: categories) {
System.out.println(category);
}
}
@Test
public void testSelect2(){
//根据主键查询
Category category = categoryDAO.selectByPrimaryKey(47);
System.out.println(category);
}
@Test
public void testSelect4(){
//分页查询
int pageNum = 2;
int pageSize = 10;
int start = (pageNum-1)*pageSize;
RowBounds rowBounds = new RowBounds(start,pageSize);
List<Category> categories = categoryDAO.selectByRowBounds(new Category(), rowBounds);
for (Category category: categories) {
System.out.println(category);
}
//查询总记录数
int i = categoryDAO.selectCount(new Category());
System.out.println(i);
}
@Test
public void testSelect5(){
//带条件分页
//条件
Example example = new Example(Category.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("categoryLevel",1);
//分页
int pageNum = 2;
int pageSize = 3;
int start = (pageNum-1)*pageSize;
RowBounds rowBounds = new RowBounds(start,pageSize);
List<Category> categories = categoryDAO.selectByExampleAndRowBounds(example,rowBounds);
for (Category category: categories) {
System.out.println(category);
}
//查询总记录数(满足条件)
int i = categoryDAO.selectCountByExample(example);
System.out.println(i);
}
}
4.3 多表关联查询
public void testSelect(){
//查询用户同时查询订单
Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo( property."username", value:"zhangsan");
//根据用户名查询用户
//1.先根据用户名查询用户信息
List<User> users = userDAO.selectByExample(example);
User user = users.get(0);
//2.再根据用户id到订单表查询订单
Example example1 = new Example(Orders.class);
Example.Criteria criteria1 = example1.createCriteria();
criteria1.andEqualTo(lproperty:"userId",user.getUserId());
List<Orders> ordersList = orderDAO.selectByExample(example1);
//3.将查询到订单集合设置到user
user.setordersList(ordersList);
System.out.println(user);
}
- 也可以自定义sql语句进行关联查询
4.4 逆向工程——类似代码生成器
- 引入插件和依赖
- 在resource/gnerator目录下创建generatorConfig.xml
<!-- mysql的驱动版本要和pom文件中的mysql驱动版本一致 -->
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<?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>
<!-- 引入数据库连接配置 -->
<!-- <properties resource="jdbc.properties "/>-->
<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.ly.lymall.general.GeneralDAO"/>
</plugin>
<!-- 配置数据库连接-->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mall"
userId="root"
password="123456">
<!-- connectionURL属性,防止乱码 -->
<property name="useUnicode" value="true"/>
<!-- connectionURL属性,防止乱码 -->
<property name="characterEncoding" value="utf-8"/>
<!-- connectionURL属性,防止报时间错误 -->
<property name="serverTimezone" value="UTC"/>
<!-- connectionURL属性,防止生成不同数据库同名表的代码 -->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 配置实体类存放路径 -->
<javaModelGenerator targetPackage="com.ly.lymall.entity" targetProject="src/main/java"/>
<!-- 配置 XML 存放路径 -->
<sqlMapGenerator targetPackage="/" targetProject="src/main/resources/mappers"/>
<!-- 配置 DAO 存放路径 -->
<javaClientGenerator targetPackage="com.ly.lymall.dao" targetProject="src/main/java" type="XMLMAPPER"/>
<!-- %代表要生成所有表 -->
<table tableName="%"></table>
</context>
</generatorConfiguration>
- 将配置文件设置到逆向工程的maven插件
- 启动generator