[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>一、什么是高级查询:
<>① 多条件的过滤查询
简单说,即拼接sql语句,在sql查询语句之后使用:
where 条件1 and/or 条件2 and/or 条件3 …
<>② 分页查询
<>二、多条件过滤查询:
<>1,WHERE 1=1:
是一个永远为true的条件【mysql安全注入漏洞】,通过 1=1 的条件,避免判断where后边应该接收那个字段【考虑到拼接的几个字段都为空】。
StringBuilder sql = new StringBuilder(); sql.append(" WHERE 1=1"); // 拼接姓名 if
(StringUtils.isNotBlank(name)) { sql.append(" AND name LIKE ?"); parameters.add(
"%" + name + "%"); } // 拼接最小年龄 if (minAge != null) { sql.append(" AND age >= ?")
; parameters.add(minAge); }
<>2、WHERE 1=1 问题:影响性能,因为不能使用索引查询了。
解决where 1=1 索引问题【定义一个容器,当容器放进了条件,取出容器的条件(第一个条件前拼接上where,其他拼接and)】
sql.append(" WHERE "); //利用Apached 的组件 Apache commons-lang
组件:StringUtils的join方法:把集合中每个元素使用特定的字符串连接起来 sql.append(StringUtils.join(
conditions, " AND "));
<>3、思维:封装与职责分离
<>1:封装:参数过多应该封装成一个对象
<>2:职责分离:谁拥有该数据,谁就应该包含操作该数据的方法
<>4、关键字查询(从多个列中做查询)
例如:查询商品名称中或者商品品牌中带有’中国’。
细节:AND的优先级高于OR 关键字查询的多列查询拼接sql要记得使用()括起来
//暴露给子类:让子类在customizedQuery中调用,添加字节的查询条件和参数 protected void addQuery(String
condition, Object... param) {
//细节:不定长的参数是数组----集合parameters.add(数组)是将数组作为一个元素添加进来
//解决:先将数组转成集合,然后集合parameters.addAll(集合) this.conditions.add(condition); this.
parameters.addAll(Arrays.asList(param)); } //拼接关键词 if (keyword != null) { super.
addQuery("(name LIKE ? OR dormBuildId LIKE ?)","%" + keyword + "%", "%" +
keyword+ "%"); }