02.TK.Mybatis之Select

本文介绍了TK.Mybatis中的一些常用查询方法,包括selectAll()、selectByPrimaryKey()和selectByExample()。详细讲述了如何进行主键查询和复杂查询,如范围查询和多条件查询,并讨论了在查询条件复杂时,XML方式可能更适合维护。

TK.Mybatis查询方法

一、概述

本章节基于第一章节的:01.Mybatis Tk插件环境搭建
TK的内置查询方法有很多,在该章节主要介绍几种常用的的方法:

  • selectAll():查询所有
  • selectByPrimaryKey(Object var1):主键查询
  • selectByExample(Example example):用来复杂查询
二、单元测试
  • 主键查询
    它会根据主键进行查询,进行传的是一个对象,它只会拼接主键。
	@Test
    public void selectAll(){
        Product param = new Product();
        param.setId(1L);
        param.setProductName("Redis深度历险记");
        //Product product = productDao.selectByPrimaryKey(1L);  等价下面语句
        Product product = productDao.selectByPrimaryKey(param);
    }
 //==>  Preparing: SELECT id,product_name,price,in_time,out_time FROM product WHERE id = ?
//==> Parameters: 1(Long)
//<==    Columns: id, product_name, price, in_time, out_time
//<==        Row: 1, Redis深度历险记, 79, 2019-12-21 18:22:20, 2019-12-23 18:22:20
//<==      Total: 1
  • 复杂查询-1:范围查询
    比如:我要查询入库时间在:2019-12-19—2019-12-25的商品
@Test
    public void selectBetWeen(){
        Example example = new Example(Product.class);
        
        LocalDateTime from = LocalDateTime.of(2019,12,19,0,0);
        LocalDateTime to = LocalDateTime.of(2019, 12, 26,0,0);

        example.createCriteria().andBetween("inTime", from, to);
        List<Product> products = productDao.selectByExample(example);
    }
    
//==>  Preparing: SELECT id,product_name,price,in_time,out_time FROM //product WHERE ( in_time between ? and ? ) 
//==> Parameters: 2019-12-19 00:00:00.0(Timestamp), 2019-12-26 //00:00:00.0(Timestamp)
//<==    Columns: id, product_name, price, in_time, out_time
//<==        Row: 1, Redis深度历险记, 79, 2019-12-21 18:22:20, 2019-12-23 //18:22:20
//<==        Row: 2, Java 8 函数编程, 56, 2019-12-24 18:22:20, 2019-12-26 //18:22:20
//<==      Total: 2
  • 复杂查询-2:多条件查询 比如:(a or b) and ( c )
    在这里,我要查询入库时间大于等2019-12-25,且出库时间大于等与2019-12-25的商品。
	@Test
    public void selectByCra(){
        Example example = new Example(Product.class);

        //LocalDateTime cur = LocalDateTime.of(2019,12,25,0,0);
        
        Example.Criteria c = example.createCriteria();
        c.andLessThanOrEqualTo("inTime", "2019-12-25 00:00:00.0");

        Example.Criteria e = example.createCriteria();
        e.andGreaterThanOrEqualTo("outTime", "2019-12-25 00:00:00.0");

        example.and(e);
        List<Product> products = productDao.selectByExample(example);
    }
    
//==>  Preparing: SELECT id,product_name,price,in_time,out_time FROM //product WHERE ( in_time <= ? ) and ( out_time >= ? ) 
//==> Parameters: 2019-12-25T00:00(String), 2019-12-25T00:00(String)
//<==    Columns: id, product_name, price, in_time, out_time
//<==        Row: 2, Java 8 函数编程, 56, 2019-12-24 18:22:20, 2019-12-26 //18:22:20
//<==      Total: 1

观察上面两个复杂查询日志结果,你会发现一个Criteria 对于一个 ()

三、最后

虽然TK帮助我们省略部分工作,但是随着查询条件的复杂度增加,编写代码也非常多,感觉不太容易维护。建议:对于复杂条件的sql还是XML方便点,后期维护起来也比较容易。

四、参考
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值