【精选】TKMybatis的使用大全和例子(example、Criteria、and、or)

您所在的位置:网站首页 standard和criteria以及code的区别 【精选】TKMybatis的使用大全和例子(example、Criteria、and、or)

【精选】TKMybatis的使用大全和例子(example、Criteria、and、or)

#【精选】TKMybatis的使用大全和例子(example、Criteria、and、or)| 来源: 网络整理| 查看: 265

1. 什么是TKMybatis

简单来说,类似mybatis-plus,就是对原有的mybatis进行封装,简化我们的SQL操作

与mybatis不同的是,我们不要要写对应的xml文件来进行SQL操作了,只需要在对应的service层去写TKMybatis封装好的代码即可

2. 怎么使用TKMybatis

引入依赖

tk.mybatis mapper-spring-boot-starter 2.0.4

POJO加依赖

javax.persistence persistence-api 1.0 compile

在启动类中配置 @MapperScan 扫描

@SpringBootApplication @MapperScan(basePackages = {"com.tom.order.mapper"}) public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } } crud(增删改查)方法

操作 类型 介绍

增加 Mapper.insert(record); 保存一个实体,null的属性也会保存,不会使用数据库默认值 Mapper.insertSelective(record); 保存一个实体,忽略空值,即没提交的值会使用使用数据库默认值

删除 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.selectByPrimaryKey(key) 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号 Mapper.selectCount(record) 根据实体中的属性查询总数,查询条件使用等号 Mapper.selectCountByExample(example) 根据Example条件进行查询总数 Mapper.selectOne(record) 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号。 但是如果存在某个属性为int,则会初始化为0。可能影响到实际使用

例子:

@Autowired private BrandMapper brandMapper; public Example createExample(Brand brand) { // 自定义条件搜索对象 Example Example example = new Example(Brand.class); //条件构造器 Example.Criteria criteria = example.createCriteria(); if (brand != null) { if (!StringUtils.isEmpty(brand.getName())) { criteria.andLike("name", '%' + brand.getName() + '%'); } if (!StringUtils.isEmpty(brand.getLetter())) { criteria.andEqualTo("letter", brand.getLetter()); } } return example; } @Override public List findAll() { return brandMapper.selectAll(); } @Override public List findList(Brand brand) { Example example = createExample(brand); return brandMapper.selectByExample(example); } @Override public Brand findById(Integer id) { return brandMapper.selectByPrimaryKey(id); }

例子: 这是service一小段代码,我们看看都简化一些操作 比如 在这里插入图片描述

然后我们也发现了核心的代码是哪里 在这里插入图片描述 是这里

然后我们又发现有几行代码是我们还没看懂的 在这里插入图片描述 这个是TKMybatis的另一个简化操作,简化我们的SQL的and和or

and/or方法

我们一般的sql写法是 where a=1 and b=1

select name from student where name=“zhangsan1” and age = 18;

复杂一点的写法 where (a=1 and b=2) or (b=1 and d=2)

select name from student where (name=“zhangsan1” and `code=16) or (age = 18 and address = “beijing”);

分析 使用tkmapper一般为了快速实现逻辑,都使用的example,比如上面的一般sql写法转换为tkmapper的example写法

> Example e = new Example(Student.class); > > > Example.Criteria criteria = e.createCriteria(); > > criteria.andEqualTo("name", zhangsan1); > > criteria.andEqualTo("age ", 18);

对于复杂的sql写法怎么转换?其实一般不建议还用example写复杂sql,后期不好维护,如果为了快速实现逻辑不注重规范的话,这里教一下怎么写

分析结果拆分:where (a=1 and b=2) or (b=1 and d=2),这里是两个小的嵌套子查询

实现逻辑 总查询

Example e = new Example(Student.class); Example.Criteria criteria = e.createCriteria(); criteria.andEqualTo("a", 1); criteria.andEqualTo("b", 2);

但是我们是不推荐这么去书写的

总结一些常用的

and/or方法具体API

and方法

andEqualTo(“field”,value) 表示条件为实体类字段"field"等于value值 Example example = new Example(WorkGuideModel.class); example.createCriteria() .andEqualTo("createUserId","1") .andEqualTo("isDelete",0); List list = mapper.selectByExample(example); return list;

相当于

select * from tb_work_guide where( ( create_user_id = ? and is_delete = ? ) )

where后为什么会多两层括号我也不知道…反正查询结果是对的,下面为了美观和方便,就手动把括号去掉了

另一种单参数写法: 参数为map

Map param = new HashMap(); param.put("createUserId","1"); param.put("isDelete","0"); Example example = new Example(WorkGuideModel.class); example.createCriteria() .andEqualTo(param); List list = mapper.selectByExample(example); return list;

andAllEqualTo(param) 与andEqualTo的单参数形式一样,参数为map

andNotEqualTo(“field”,value) 与andEqualTo相反,条件为实体类字段"field"不等于value值,同时此方法没有单参数

Example example = new Example(WorkGuideModel.class); example.createCriteria() .andNotEqualTo("createUserId","1"); List list = mapper.selectByExample(example); return list;

执行sql:

select * from tb_work_guide where create_user_id ?

andIn(“field”,list) 表示条件为实体类"field"字段的值包含ids里的值,与sql语句中的in()相同 List ids = new ArrayList(); ids.add(1); ids.add(2); Example example = new Example(WorkGuideModel.class); example.createCriteria() .andIn("createUserId",ids); List list = mapper.selectByExample(example); return list;

执行sql:

select * from tb_work_guide where create_user_id in ( ? , ? )

list中不一定要是Integer类型,也可以是String

List titles = new ArrayList(); titles.add("标题1"); titles.add("标题2"); Example example = new Example(WorkGuideModel.class); example.createCriteria() .andIn("title",titles); List list = mapper.selectByExample(example); return list;

举一反三:

与sql语句中的FIND_IN_SET也相同

需要注意的是,FIND_IN_SET(str,strList),这里的str为数据库中的字段名,如create_user_id,而不是实体类的createUserId

执行sql:

select * from tb_work_guide where FIND_IN_SET (create_user_id , ‘1,2’)

andNotIn(“field”,list) 与andIn()相反,查询"field"字段不包含list中的值的结果

执行sql:

select * from tb_work_guide where create_user_id not in ( ? , ? )

andIsNull(“field”) 表示实体类"field"字段为null Example example = new Example(WorkGuideModel.class); example.createCriteria() .andIsNull("createUserId"); List list = mapper.selectByExample(example); return list;

执行sql:

select * from tb_work_guide where create_user_id is null

andIsNotNull(“field”) 与andIsNull()相反,表示实体类"field"字段不为null Example example = new Example(WorkGuideModel.class); example.createCriteria() .andIsNotNull("createUserId"); List list = mapper.selectByExample(example); return list;

执行sql:

select * from tb_work_guide where create_user_id is not null

andBetween(“field”,value1,value2) 表示"field"字段的值在value1和value2之间,注意:这个区间是包头包尾的,1 ?

andGreaterThanOrEqualTo(“field”,value) 与andGreaterThan()差不多,表示"field"字段中大于等于value的值

执行sql:

select * from tb_work_guide where age >= ?

andLessThan(“field”,value) 表示查询"field"字段中小于value的值 Example example = new Example(WorkGuideModel.class); example.createCriteria() .andLessThan("age",20); List list = mapper.selectByExample(example); return list;

执行sql:

select * from tb_work_guide where age < ?

andLessThanOrEqualTo(“field”,value) 与andLessThan()差不多,表示"field"字段中小于等于value的值

执行sql:

select * from tb_work_guide where age



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3