TkMapper的配置及使用
TkMapper主要是做单标查询,复杂的多表查询我们还得自己写sql。
id | pid | name | value | icon | type | uri | status | create_time | sort |
---|---|---|---|---|---|---|---|---|---|
1 | 0 | 商品 | null | null | 0 | null | 1 | 2018-09-29 16:15:14 | 0 |
2 | 1 | 商品列表 | pms:product:read | null | 1 | /pms/product/index | 1 | 2018-09-29 16:17:01 | 0 |
3 | 1 | 添加商品 | pms:product:create | null | 1 | /pms/product/add | 1 | 2018-09-29 16:18:51 | 0 |
… | … | … | … | … | … | … | … |
package com.sxykj.ymall.user.bean;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;
public class UmsPermission {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Long pid;
private String name;
private String value;
private String icon;
private Integer type;
private String uri;
private Integer status;
private Date createTime;
private Integer sort;
//省略getter和setter方法
}
1、在pom文件中引入TkMapper依赖:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
2、在Application类上加注解@MapScan(“com.sxykj.ymall.user.mapper”),这个注解依赖的包一定是tk开头的(tk.mybatis.spring.annotation.MapperScan;)。 3、映射类extends 通用Mapper 4、配置pojo类中的属性:
我这里就直接演示Tk的方法使用了。
1、selectOne(T):通过pojo对象,查询一条数据
2、select(T):通过pojo对象,查询一组数据
3、selectAll():查询所有
4、selectCount(T):通过pojo对象,查询该数据的条数
5、selectByPrimaryKey(Object):通过主键,查询数据
6、existsWithPrimaryKey(Object):通过主键,查询数据是否存在
7、insert(T):通过pojo对象, 插入对象
8、insertSelective(T):通过pojo对象, 插入对象
9、updateByPrimaryKey(T):通过pojo对象主键, 更新对象
10、updateByPrimaryKeySelective(T):通过pojo对象主键, 更新对象
11、delete(T):通过pojo对象, 删除对象
12、deleteByPrimaryKey(Object):通过主键, 删除对象
创建一个Example实例,有很多方法,简单的单条件查询,还可以创建条件对象,example.createCriteria()来添加其他条件,根据查询需求条件对象可以添加多个。
1、简单条件:使用Example
1> 先创建Example对象
Example example = new Example(UmsPermission.class); //创建Example对象
2> 选择使用的方法:(常用方法)
方法 | 解释 |
---|---|
selectProperties(“id”,“pid”…) | 选择查询的列,select id , pid … |
excludeProperties(“name”,“icon”) | 排除查询的列,结果不显示 |
and().andEqualTo(“id”,4) | 等值查询,and id = 4 |
and().andLike(“pid”,”%5%”) | 模糊查询:and pid like %5% |
and().andBetween(“id”, 5, 8) | 区间查询:and (id betewwn 5 and 8) |
and().andGreaterThan(“id”, 18) | 大于查询:and (id > 18) |
and().andGreaterThanOrEqualTo(“id”, 18) | 大于等于查询:and (id >= 18) |
and().andLessThan(“id”, 18) | 小于查询:and (id < 18) |
and().andLessThanOrEqualTo(“id”, 18) | 小于等于查询:and (id <= 18) |
orderBy(“pid”) | 排序,order by pid,默认为ASC |
orderBy(“pid”).desc() | 逆序排序 |
and().andIsNull(“name”) | 空条件:and name is null |
… | 其他方法自行研究 |
例如:
example.and().andEqualTo("pid", 4);
3> 将Example对象传入Example方法
List<UmsPermission> umsPermissions1
= umsPermissionMapper.selectByExample(example);
2、多条件查询:
1> 先创建Example对象,并添加条件
Example example = new Example(UmsPermission.class); //创建Example对象
example.and().andEqualTo("pid", 4); //添加条件 pid = 4
2> 创建条件对象,并添加条件
Example.Criteria criteria1 = example.createCriteria(); //创建条件对象
criteria1.andEqualTo("type", 2); //添加条件 type = 2
example.and(criteria1); //将条件对象添加到example实例
Criteria的方法参照Example的,条件对象可创建多个
3> 将Example对象传入Example方法
List<UmsPermission> umsPermissions1
= umsPermissionMapper.selectByExample(example);
3、其他的一些ByExample方法:
后面一些分页查询方法没有演示,请参考另一篇文章,PageHelper分页助手的使用
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185223.html原文链接:https://javaforall.cn