让你远离sql语句的Mybatis工具==>去除mapper.xml
只需三步即可使用单表常用操作:导包==》继承==》在启动类中设置扫描 ==>使用
通过我们去调用它封装的各种方法来实现sql语句的效果。对于单表查询不需要写SQL语句,这样就不用像mybatis那样每次写一个接口就要写一条sql语句
特点:
Tkmybatis是基于Mybatis框架开发的一个工具,通过调用它提供的方法实现对单表的数据操作,不需要写任何sql语句,这极大地提高了项目开发效率。
Tkmybatis使用 :
项目结构:
1、pom.xml添加依赖
mysql
mysql-connector-java
tk.mybatis
mapper
3.4.2
tk.mybatis
mapper-spring-boot-starter
1.1.3
org.projectlombok
lombok
true
2、创建数据库表对应实体类对象
DROP TABLE IF EXISTS category;
CREATE TABLE category (
category_id INT PRIMARY KEY,
category_name VARCHAR (50) NOT NULL,
description VARCHAR (100)
);
DELETE FROM category;
INSERT INTO category (category_id, category_name, description) VALUES
(1, ‘Beverages’, ‘test’),
(2, ‘Condiments’, ‘test’),
(3, ‘Oil’, ‘test’);
@Data
@Table(name = “category”)
public class Category {
@Id
@Column(name = “category_id”)
private Integer categoryID;
private String categoryName;
private String description;
}
实体类中,使用了以下注解:
@Table
描述数据库表信息,主要属性有name(表名)、schema、catalog、uniqueConstraints等。
@Id
指定表主键字段,无属性值。
@Column
描述数据库字段信息,主要属性有name(字段名)、columnDefinition、insertable、length、nullable(是否可为空)、precision、scale、table、unique、updatable等。
@ColumnType
描述数据库字段类型,可对一些特殊类型作配置,进行特殊处理,主要属性有jdbcType、column、typeHandler等。
@Transient
标识该属性不进行数据库持久化操作,无属性。
还有其他相关注解,如@ColumnResult、@JoinColumn、@OrderBy、@Embeddable等,可以做简单解一下,用的不多。
@Data
lombok插件用于简化代码的注解,无需写get set 等代码。
3、Mapper数据库操作接口
只需要继承tk.mybatis.mapper.common.Mapper即可。
4、Tkmybatis数据库操作方法API
增
Mapper.insert(record);
保存一个实体,null的属性也会保存,不会使用数据库默认值
Mapper.insertSelective(record);
保存一个实体,null的属性不会保存,会使用数据库默认值
删
Mapper.delete(record);
根据实体属性作为条件进行删除,查询条件使用等号
Mapper.deleteByExample(example)
根据Example条件删除数据
Mapper.deleteByPrimaryKey(key)
根据主键字段进行删除,方法参数必须包含完整的主键属性
改
Mapper.updateByExample(record, example)
根据Example条件更新实体record
包含的全部属性,null值会被更新
Mapper.updateByExampleSelective(record, example)
根据Example条件更新实体record
包含的不是null的属性值
Mapper.updateByPrimaryKey(record)
根据主键更新实体全部字段,null值会被更新
Mapper.updateByPrimaryKeySelective(record)
根据主键更新属性不为null的值
查
Mapper.select(record)
根据实体中的属性值进行查询,查询条件使用等号
Mapper.selectAll()
查询全部结果
Mapper.selectByExample(example)
根据Example条件进行查询
Mapper.selectByExampleAndRowBounds(example, rowBounds)
根据example条件和RowBounds进行分页查询
Mapper.selectByPrimaryKey(key)
根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
Mapper.selectByRowBounds(record, rowBounds)
根据实体属性和RowBounds进行分页查询
Mapper.selectCount(record)
根据实体中的属性查询总数,查询条件使用等号
Mapper.selectCountByExample(example)
根据Example条件进行查询总数
Mapper.selectOne(record)
根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
Example条件 (自定义查询条件)
//注意:(addEqualTo)这里的userId是映射的实体类。
@Test
public void selectAllTest2() {
Example example = new Example(Category.class);
example.createCriteria()
.andEqualTo(“categoryID”,1)
.andEqualTo(“categoryName”,“Beverages”);
List categories = categoryDao.selectByExample(example);
System.out.println(categories);
assertEquals(true, categories.size() > 0);
}
//注意:(addCondition)这里的user_id是数据库的字段。即where后面的条件。应该写sql语句。
@Test
public void selectAllTest3() {
Example example = new Example(Category.class);
example.createCriteria()
.andCondition(“category_id=”,1)
.andCondition(“category_name=”,“Beverages”);
List categories = categoryDao.selectByExample(example);
System.out.println(categories);
assertEquals(true, categories.size() > 0);
}
Example条件基本涵盖了常用的sql条件,并且支持使用原生sql语句字符串查询。
拓展-复杂查询
方式1:
public class ComplexTKMapperTest {
@Autowired
CategoryDao categoryDao;
@Test
public void testNestConditions(){
Example e = new Example(Category.class);
Example.Criteria c = e.createCriteria();
//关键字查询部分
String keyword = “key”;
if (!StringUtils.isEmpty(keyword)) {
c.orEqualTo(“categoryName”, keyword)
.orEqualTo(“categoryID”,keyword)
.orEqualTo(“description”,keyword);
}
//部门查询部门
Example.Criteria criteria = e.createCriteria();
criteria.andEqualTo(“categoryID”, “1112”);
e.and(criteria);
List users = categoryDao.selectByExample(e);
}
}
执行的sql为:
SELECT category_id, category_name, description FROM category WHERE (
category_name = ?
OR category_id = ?
OR description = ? ) AND (category_id = ?)
方式2
@Test
public void testNestConditions2(){
Weekend weekend = new Weekend<>(Category.class);
//关键字查询部分
String keyword = “key”;
WeekendCriteria<Category, Object> keywordCriteria = weekend.weekendCriteria();
if (!StringUtils.isEmpty(keyword)) {
keywordCriteria.orLike(Category::getCategoryName, keyword).
orLike(Category::getCategoryID, keyword)
.orLike(Category::getDescription, keyword);
//此处不需要再用下面这一句了,不然上面这个条件组合会重复一次
//weekend.and(keywordCriteria)
}
//部门查询部分
Example example = new Example(Category.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo(“categoryID”, “i222”);
weekend.and(criteria);
List users = categoryDao.selectByExample(weekend);
}
其中Weekend是高版本的通用mapper版本才有,而且需要java8语法支持。
执行效果如下图:
总结下来,就是
每个条件组合(a/b/c) (d)各自创建自己的cirteria,再用and或者or方法去连接
结语
Tkmybatis极大地提高了我们对单表数据库操作的开发效率,可以在实际项目开发中推荐使用。
拓展部分:
源码的简单分析,大家可以自己基于这个原理自定义特定的需求
此框架为我们实现这些功能所有的改动都在Mapper层面,所有的Mapper都继承了tk.mybatis.mapper.common.Mapper
public interface WorkerMapper extends Mapper {}