TkMapper(通用mapper)「建议收藏」

您所在的位置:网站首页 驼峰官网 TkMapper(通用mapper)「建议收藏」

TkMapper(通用mapper)「建议收藏」

2024-07-15 19:31| 来源: 网络整理| 查看: 265

TkMapper的配置及使用

TkMapper主要是做单标查询,复杂的多表查询我们还得自己写sql。

官方文档: 点击查看使用的是Springboot框架使用的数据库表ums_permision:

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

对应的pojo对象:代码语言:javascript复制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方法 }一、TkMapper依赖及配置

1、在pom文件中引入TkMapper依赖:

代码语言:javascript复制 tk.mybatis mapper-spring-boot-starter 2.0.2

2、在Application类上加注解@MapScan(“com.sxykj.ymall.user.mapper”),这个注解依赖的包一定是tk开头的(tk.mybatis.spring.annotation.MapperScan;)。 3、映射类extends 通用Mapper 4、配置pojo类中的属性:

1> 表名默认使用类名,驼峰转下划线(只对大写字母进行处理),如UserInfo默认对应的表名为user_info。2> 对不符合第一条默认规则的,表名可以使用@Table(name = “tableName”)进行指定。3> 表字段默认为这个类的属性名字驼峰转下划线形式。4> 可以使用@Column(name = “fieldName”)指定不符合第3条规则的字段名。5> 使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用。6> 一定有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键。7> 对于主键自增的需要加上主键策略,@GenerateValue(strategy=GenertationType.IDENTITY)或 @KeySql(useGeneratedKeys = true)。这里有个链接讲的比较详细:https://blog.csdn.net/lyf_ldh/article/details/81041141注意:TkMapper的pojo类要用包装类型 其他各层大家根据各自情况自行编写。二、简单方法使用

    我这里就直接演示Tk的方法使用了。

在这里插入图片描述在这里插入图片描述

1、selectOne(T):通过pojo对象,查询一条数据

参数:UmsPeimision对象返回值:UmsPeimision对象

2、select(T):通过pojo对象,查询一组数据

参数:UmsPeimision对象返回值:List

3、selectAll():查询所有

参数:无返回值:List

4、selectCount(T):通过pojo对象,查询该数据的条数

参数:int返回值:List

5、selectByPrimaryKey(Object):通过主键,查询数据

参数:主键返回值:UmsPeimision对象

6、existsWithPrimaryKey(Object):通过主键,查询数据是否存在

参数:主键返回值:boolean

7、insert(T):通过pojo对象, 插入对象

参数:UmsPeimision对象返回值:int所有的字段都会添加一遍即使没有值

8、insertSelective(T):通过pojo对象, 插入对象

参数:UmsPeimision对象返回值:int只给有值的字段赋值

9、updateByPrimaryKey(T):通过pojo对象主键, 更新对象

参数:UmsPeimision对象返回值:int所有的字段都会更新一遍即使没有值

10、updateByPrimaryKeySelective(T):通过pojo对象主键, 更新对象

参数:UmsPeimision对象返回值:int只给有值的字段更新

11、delete(T):通过pojo对象, 删除对象

参数:UmsPeimision对象返回值:int

12、deleteByPrimaryKey(Object):通过主键, 删除对象

参数:主键返回值:int三、Example方法使用

    创建一个Example实例,有很多方法,简单的单条件查询,还可以创建条件对象,example.createCriteria()来添加其他条件,根据查询需求条件对象可以添加多个。

1、简单条件:使用Example

1> 先创建Example对象

代码语言:javascript复制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 将Example对象传入Example方法

代码语言:javascript复制 List umsPermissions1 = umsPermissionMapper.selectByExample(example);

2、多条件查询:

1> 先创建Example对象,并添加条件

代码语言:javascript复制Example example = new Example(UmsPermission.class); //创建Example对象 example.and().andEqualTo("pid", 4); //添加条件 pid = 4

2> 创建条件对象,并添加条件

代码语言:javascript复制Example.Criteria criteria1 = example.createCriteria(); //创建条件对象 criteria1.andEqualTo("type", 2); //添加条件 type = 2 example.and(criteria1); //将条件对象添加到example实例代码语言:javascript复制 Criteria的方法参照Example的,条件对象可创建多个

3> 将Example对象传入Example方法

代码语言:javascript复制 List umsPermissions1 = umsPermissionMapper.selectByExample(example);

3、其他的一些ByExample方法:

1> selectByExample:通过Example对象,查询一组数据 参数:Example对象返回值:List 2> selectOneByExample:通过Example对象,查询一条数据 参数:Example对象返回值:UmsPeimision对象 3> selectCountByExample:通过Example对象,查询该数据的条数 参数:Example对象返回值:int 4> deleteByExample:通过Example对象,删除对象 参数:Example对象返回值:int 5> updateByExample:通过Example对象,更新对象 参数:Example对象返回值:int所有的字段都会更新一遍即使没有值 6> updateByExampleSelective:通过Example对象,更新对象 参数:Example对象返回值:int只给有值的字段更新

后面一些分页查询方法没有演示,请参考另一篇文章,PageHelper分页助手的使用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185223.html原文链接:https://javaforall.cn



【本文地址】


今日新闻


推荐新闻


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