Mybatis学习第8节 -- 动态sql-if

需求

  1. 查询id小于10,并且shopname包含"关键词"的记录
  2. 如果用户没有输入任何关键词, 那么列出所有结果

接口

List<Shop> get10ShopByTitle(String value);

映射

<select id="get10ShopByTitle" resultMap="simpleResultMap">    select * from tb_shop    <where>        <if test="value != null">            `shop_name` LIKE #{value} limit 10        </if>    </where></select>

测试用例

@Testpublic void testGet10ShopByTitle() {    String template = "查询结果: %s\n";    SqlSession session = MyBatisUtil.getSqlSession();    ShopMapper mapper = session.getMapper(ShopMapper.class);    System.out.printf(template, mapper.get10ShopByTitle("%ello%"));    String nullString = null;    System.out.printf(template, mapper.get10ShopByTitle(nullString));    session.close();}

测试结果

Opening JDBC Connection
Created connection 288615534.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1133ec6e]
==> Preparing: select * from tb_shop WHERE `shop_name` LIKE ? limit 10 
==> Parameters: %ello%(String)
<== Columns: shop_id, owner_id, area_id, shop_category_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice
<== Row: 40, 1, null, null, Hello Jack, null, null, null, null, null, null, null, 0, null
<== Row: 41, 1, null, null, Hello Jack, null, null, null, null, null, null, null, 0, null
<== Total: 2
查询结果: [Shop{id=40, ownerId=1, areaId=null, categoryId=null, name=‘Hello Jack‘, desc=‘null‘, addr=‘null‘, phone=‘null‘, image=‘null‘, priority=null, createTime=null, lastEditTime=null, enableStatus=0, advice=‘null‘}
, Shop{id=41, ownerId=1, areaId=null, categoryId=null, name=‘Hello Jack‘, desc=‘null‘, addr=‘null‘, phone=‘null‘, image=‘null‘, priority=null, createTime=null, lastEditTime=null, enableStatus=0, advice=‘null‘}
]
==> Preparing: select * from tb_shop 
==> Parameters: 
<== Columns: shop_id, owner_id, area_id, shop_category_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice
<== Row: 1, 1, 3, 14, 正式店铺名称, 测试描述, 正式地址, 13810524086, /upload/item/shop/1/2017091621545314507.jpg, 10, 2017-08-03 00:08:32, 2017-09-16 21:54:53, 0, 审核中
<== Row: 28, 1, 2, 22, 小黄人主题奶茶店, 不接受预订,请直接来店里进行消费, 位于东苑2号, 13810524086, /upload/images/item/shop/28/2017092601041469991.png, 50, 2017-09-26 01:04:13, 2017-09-26 01:04:13, 1, null
<== Row: 29, 1, 3, 22, 暴漫奶茶店, 过来喝喝就知道啦,你是我的奶茶, 西苑1号, 1211334565, /upload/images/item/shop/29/2017092601054939287.jpg, 40, 2017-09-26 01:05:49, 2017-09-26 01:05:49, 1, null
<== Row: 30, 1, 2, 20, 彪哥大排档, 敢说不好吃吗, 东苑1号, 13628763625, /upload/images/item/shop/30/2017092601063878278.jpg, 30, 2017-09-26 01:06:37, 2017-09-26 01:06:37, 1, null
<== Row: 31, 1, 2, 20, 威哥大排档, 干掉彪哥大排档, 东苑南路, 126554437261, /upload/images/item/shop/31/2017092601072177572.jpg, 20, 2017-09-26 01:07:21, 2017-09-26 01:07:21, 1, null
<== Row: 32, 1, 2, 22, 你是我的奶茶, 奶茶店再次来袭, 东苑六路, 13652384615, /upload/images/item/shop/32/2017092601081463136.jpg, 10, 2017-09-26 01:08:13, 2017-09-26 01:08:13, 1, null
<== Row: 35, 8, 2, 22, 奶茶来了, 奶茶来了, 西苑7路, null, null, 0, null, null, 0, null
<== Row: 38, 1, 2, 10, 杰克的店铺abcd, etc test, etc test, etc test, etc test, null, 2018-06-02 22:04:58, null, 1, 审核中-
<== Row: 40, 1, null, null, Hello Jack, null, null, null, null, null, null, null, 0, null
<== Row: 41, 1, null, null, Hello Jack, null, null, null, null, null, null, null, 0, null
<== Total: 10
查询结果: [Shop{id=1, ownerId=1, areaId=3, categoryId=14, name=‘正式店铺名称‘, desc=‘测试描述‘, addr=‘正式地址‘, phone=‘13810524086‘, image=‘/upload/item/shop/1/2017091621545314507.jpg‘, priority=10, createTime=2017-08-03, lastEditTime=2017-09-16, enableStatus=0, advice=‘审核中‘}
, Shop{id=28, ownerId=1, areaId=2, categoryId=22, name=‘小黄人主题奶茶店‘, desc=‘不接受预订,请直接来店里进行消费‘, addr=‘位于东苑2号‘, phone=‘13810524086‘, image=‘/upload/images/item/shop/28/2017092601041469991.png‘, priority=50, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}
, Shop{id=29, ownerId=1, areaId=3, categoryId=22, name=‘暴漫奶茶店‘, desc=‘过来喝喝就知道啦,你是我的奶茶‘, addr=‘西苑1号‘, phone=‘1211334565‘, image=‘/upload/images/item/shop/29/2017092601054939287.jpg‘, priority=40, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}
, Shop{id=30, ownerId=1, areaId=2, categoryId=20, name=‘彪哥大排档‘, desc=‘敢说不好吃吗‘, addr=‘东苑1号‘, phone=‘13628763625‘, image=‘/upload/images/item/shop/30/2017092601063878278.jpg‘, priority=30, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}
, Shop{id=31, ownerId=1, areaId=2, categoryId=20, name=‘威哥大排档‘, desc=‘干掉彪哥大排档‘, addr=‘东苑南路‘, phone=‘126554437261‘, image=‘/upload/images/item/shop/31/2017092601072177572.jpg‘, priority=20, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}
, Shop{id=32, ownerId=1, areaId=2, categoryId=22, name=‘你是我的奶茶‘, desc=‘奶茶店再次来袭‘, addr=‘东苑六路‘, phone=‘13652384615‘, image=‘/upload/images/item/shop/32/2017092601081463136.jpg‘, priority=10, createTime=2017-09-26, lastEditTime=2017-09-26, enableStatus=1, advice=‘null‘}
, Shop{id=35, ownerId=8, areaId=2, categoryId=22, name=‘奶茶来了‘, desc=‘奶茶来了‘, addr=‘西苑7路‘, phone=‘null‘, image=‘null‘, priority=0, createTime=null, lastEditTime=null, enableStatus=0, advice=‘null‘}
, Shop{id=38, ownerId=1, areaId=2, categoryId=10, name=‘杰克的店铺abcd‘, desc=‘etc test‘, addr=‘etc test‘, phone=‘etc test‘, image=‘etc test‘, priority=null, createTime=2018-06-02, lastEditTime=null, enableStatus=1, advice=‘审核中-‘}
, Shop{id=40, ownerId=1, areaId=null, categoryId=null, name=‘Hello Jack‘, desc=‘null‘, addr=‘null‘, phone=‘null‘, image=‘null‘, priority=null, createTime=null, lastEditTime=null, enableStatus=0, advice=‘null‘}
, Shop{id=41, ownerId=1, areaId=null, categoryId=null, name=‘Hello Jack‘, desc=‘null‘, addr=‘null‘, phone=‘null‘, image=‘null‘, priority=null, createTime=null, lastEditTime=null, enableStatus=0, advice=‘null‘}
]

<wiz_tmp_tag class="wiz-block-scroll">

总结

在if的test属性中可以直接使用接口的parameter, 而bind比较特别, 他使用的是传入参数的属性

 

原文地址:https://www.cnblogs.com/litran/p/10545762.html

时间: 2024-08-30 18:00:00

Mybatis学习第8节 -- 动态sql-if的相关文章

MyBatis学习 之 三、动态SQL语句

有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Oracle的序列.mysql的函数生成Id.这时我们可以使用动态sql. 下文均采用mysql语法和函数(例如字符串链接函数CONCAT). 3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的s

MyBatis学习 之 四、动态SQL语句

有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Oracle的序列.mysql的函数生成Id.这时我们可以使用动态sql. 下文均采用mysql语法和函数(例如字符串链接函数CONCAT). 3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的s

Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射 mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sq

Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql(转发同上)

原地址:http://www.cnblogs.com/shanheyongmu/p/7121807.html mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql. 对查询条件进行判断,如果输入参数不为空才进行查询条件拼接. 1.3 mapper.xml <select id

MyBatis框架(三)动态SQL,分页,二进制存入数据库图片

一.动态sql语句,分页 1, <if>条件 <if test="key!=null"> 拼接sql语句 </if> 2, <choose><when><otherwise> 注意:只能执行一个分支 <choose> <when test="key=='value'"> 拼接sql语句 </when> <when test="key=='val

Mybatis中输入输出映射和动态Sql

一.输入映射 我们通过配置parameterType的值来指定输入参数的类型,这些类型可以是简单数据类型.POJO.HashMap等数据类型 1.简单类型 2.POJO包装类型 ①这是单表查询的时候传入的POJO包装类型,即可以直接传入实体类,但是当多表查询的时候,就需要自定义POJO类型 ②我们使用自定义POJO类型来具体的了解一下 先设计 包装类型如下,其中UserPOJO是除了User本身之外的添加的其他跟User相关的属性的包装类,UserVo是用于视图层面的包装类型,同样也是作为Map

Mybatis——传入POJO对象,动态SQL,多表关系

传入POJO对象 QueryVo.java public class QueryVo implements Serializable{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } } SQL映射文件 <!-- 传入POJO对象,查询用户 --> <select id="findUserByPOJO&qu

Mybatis学习第6节 -- 修改功能和修改部分字段

方案一, 先查询再修改 接口 int updateShop(Shop shop); 映射 <update id="updateShop" parameterType="Shop"> UPDATE `tb_shop` SET `owner_id` = 'owner_id', `area_id` = 'area_id', `shop_category_id` = 'shop_category_id', `shop_name` = 'shop_name', `

Mybatis学习(6)动态加载、一二级缓存

一.动态加载: resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: 如果查询订单并且关联查询用户信息.如果先查询订单信息即可满足要求,当需要查询用户信息时再查询用户信息.把对用户信息的按需去查询就是延迟加载. 需要先说明下是按照这个sql的思路来实现延迟加载的: mysql> select orders.*, (select user.username from us