TkMapper(通用mapper)

TkMapper的配置及使用

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

  • 官方文档:
    点击查看
  • 使用的是Springboot框架
  • 使用的数据库表ums_permision:
idpidnamevalueicontypeuristatuscreate_timesort
10商品nullnull0null12018-09-29 16:15:140
21商品列表pms:product:readnull1/pms/product/index12018-09-29 16:17:010
31添加商品pms:product:createnull1/pms/product/add12018-09-29 16:18:510
  • 对应的pojo对象:
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依赖:

<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类中的属性:

  • 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<UmsPeimision>

3、selectAll():查询所有

  • 参数:无
  • 返回值:List<UmsPeimision>

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

  • 参数:int
  • 返回值:List<UmsPeimision>

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对象

    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方法:

  • 1> selectByExample:通过Example对象,查询一组数据

    • 参数:Example对象
    • 返回值:List<UmsPeimision>
  • 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分页助手的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值