Mybatis实现条件查询(三)
1. 准备
请先完成Mybatis基本配置(一)的基本内容
2. 疑问
我们再Mybatis基本配置(一)中实现了按照商品ID进行查询商品信息,可是在实际应用中却很少出现根据ID来查询商品的情况。因为我们的用户或许并不知道这个商品的ID是什么,他们只能记住商品编码或者商品的部分名称,更甚至是他们只记得这个商品的大致售价。如果这个时候他们想找到自己想要的那个商品该怎么办么?所以我们的系统还必须提供另外一些功能。
3. 解决
现在我们拟定我们的用户需要根据商品编码、商品名称及价格等信息来查询自己想要的商品,所以我们要做如下更改:
1) 定义查询条件实体类
package com.mybatis.entity;
public class QryPartParam {
private String partCode; //要查询的商品编码
private String partName; //要查询的商品名称
private float salePriceLow; //价格区间--起始价格
private float salePriceHigh; //价格区间--结束价格
public String getPartCode() {
return partCode;
}
public void setPartCode(String partCode) {
this.partCode=partCode;
}
public String getPartName() {
return partName;
}
public void setPartName(String partName) {
this.partName=partName;
}
public String getSalePriceLow() {
return salePriceLow;
}
public void setSalePriceLow(String salePriceLow) {
this.salePriceLow=salePriceLow;
}
public String getSalePriceHigh() {
return salePriceHigh;
}
public void setSalePriceHigh(String SalePriceHigh) {
this.SalePriceHigh=SalePriceHigh;
}
}
2)在com.mybatis.dao.PartDao中增加接口函数
public List<PartInfo> getPartInfoByUser(QryPartParam qryPartParam);
3)在com.mybatis.dao.mapper.PartMapper中增加其实现方法。
<select id="getPartInfoByUser" parameterType="com.mybatis.entity.QryPartParam" resultType="com.mybatis.entity.PartInfo">
SELECT * FROM tbInfoPart
WHERE PartCode = #{partCode}
AND PartName LIKE '%'+#{partName}+'%'
AND SalePrice <![CDATA[>=]]> #{salePriceLow}
AND SalePrice <![CDATA[<=]]> #{salePriceHigh}
</select>
需要注意的是:
(1)需要模糊查询的时候,可参考PartName LIKE '%'+#{partName}+'%'这种的写法,当然还有其他写法就待读者自行摸索
(2)当我们的查询语句中出现>或者<等此类符号时,因为这两种符号为xml的关键字符,
"<" 会产生错误,因为解析器会把该字符解释为新元素的开始。
">" 会产生错误,因为解析器会把该字符解释为新元素的结束。
所以需要特殊处理成<![CDATA[>=]]>。术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据。
当然我们也可以用转义字符。xml的转义字符列表如下:
| < | < | 小于 |
| > | > | 大于 |
| & | & | 和号 |
| ' | ' | 省略号 |
| " | " | 引号 |
4)测试下效果
public static List<PartInfo> selectPartByUser(QryPartParam qryPartParam){
InputStream iStream = TestMain.class.getClassLoader().getResourceAsStream("mybatis.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(iStream);
SqlSession session = sessionFactory.openSession();
String statement = "com.mybatis.dao.PartDao.getPartInfoByUser";
List<PartInfo> partInfos = session.selectList(statement, qryPartParam);
session.close();
return partInfos;
}
public static void main(String[] args) {
QryPartParam qryPartParam = new QryPartParam();
qryPartParam.setPartCode("001-0001");
qryPartParam.setPartName("TCL");
qryPartParam.setSalePriceLow(0f);
qryPartParam.setSalePriceHigh(10000f);
List<PartInfo> partInfos = selectPartByUser(qryPartParam);
for (PartInfo partInfo : partInfos) {
System.out.println("ID:"+partInfo.getId()+" 商品:["+
partInfo.getPartCode()+"] "+
partInfo.getPartName()+" ,售价:"+
partInfo.getSalePrice()+"元/"+
partInfo.getUnit());
}
打印结果为
ID: 商品:[-] TCL D32E161 32英寸 内置wifi 在线影视 窄边LED网络液晶电视 ,售价:.9元/台
可以看到我们已经成功查询到了该条商品
4.有关参数问题
有人可能会问,按照现在参数定义方式(com.mybatis.entity.QryPartParam),那是不是说我以后每增加一个查询,就需要定义相对应的参数类?为了解决这个问题,笔者提供其他两种参数定义方式
1)通过@Param注解来标识多参数:
public List<PartInfo> getPartInfoByUser(@Param("partCode") String partCode,
@Param("partName") String partName,
@Param("salePriceLow") float salePriceLow,
@Param("salePriceHigh") float salePriceHigh);
在Mapper中则不再需要parameterType赋值:
<select id="getPartInfoByUser" resultType="com.mybatis.entity.PartInfo">
SELECT * FROM tbInfoPart
WHERE PartCode = #{partCode}
AND PartName LIKE '%'+#{partName}+'%'
AND SalePrice <![CDATA[>=]]> #{salePriceLow}
AND SalePrice <![CDATA[<=]]> #{salePriceHigh}
</select>
此种传参方式适用于参数个数比较少的情况。
2)通过Map传参:
public List<PartInfo> getPartInfoByUser(Map<String, Object> map);
在Mapper中对应的parameterType应赋值为(其中#{}种的取值应和Map中的键名是一致的):
<select id="getPartInfoByUser" parameterType="java.util.Map" resultType="com.mybatis.entity.PartInfo">
SELECT * FROM tbInfoPart
WHERE PartCode = #{partCode}
AND PartName LIKE '%'+#{partName}+'%'
AND SalePrice <![CDATA[>=]]> #{salePriceLow}
AND SalePrice <![CDATA[<=]]> #{salePriceHigh}
</select>
此种传参方式优点在于易扩展。
5. 目录结构

Mybatis实现条件查询(三)的更多相关文章
- Mybatis多条件查询
在Mybatis多条件查询中: 1.参数如果是多条件,则需要将将添加到Map集合中进行传入. 2.就是将其参数用有序数字进行代替. Mybatis单个String类型参数传递 mysql文如下,传入参 ...
- mybatis 按照条件查询
mybatis 按照条件查询 @Autowired private StudentMapper studentMapper; @Override public Map getStudentList(i ...
- MyBatis参数条件查询传入的值为0时的判断
MyBatis条件查询对字段判断是否为空一般为: <if test="testValue!=null and testValue != ''"> and test_va ...
- SSM整合 mybatis多条件查询与分页
多条件查询与分页: 通过页面的houseName.floorage获取值传到前端视图(HouseSearchVO)实体类中的houseName,floorage建立houseSearchVO对象. 通 ...
- mybatis if条件查询 及<号的问题
摘录自:http://flt95.blog.163.com/blog/static/12736128920136185841551/ <if test="p=='1'"> ...
- mybatis中条件查询大于等于和小于等于写法
原符号 < <= > >= & ' "替换符号 < <= > >= & ' " createDat ...
- MyBatis学习总结(三)——多表关联查询与动态SQL
在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...
- MyBatis动态SQL第一篇之实现多条件查询(if、where、trim标签)
一.动态SQL概述 以前在使用JDBC操作数据时,如果查询条件特别多,将条件串联成SQL字符串是一件痛苦的事情.通常的解决方法是写很多的if-else条件语句对字符串进行拼接,并确保不能忘了空格或在字 ...
- SSM-MyBatis-13:Mybatis中多条件查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 实体类 public class Book { private Integer bookID; private ...
随机推荐
- 扩展的Sobel 算子
Custom Extended Sobel Filters https://arxiv.org/pdf/1910.00138.pdf sobel算子是进行边缘检测的一个重要算子.它通常是一个3x3的 ...
- Jmeter测试入门——分析HBase访问服务性能瓶颈
开启HBase服务 新建线程组,设定线程数为10: 设定请求方法和请求参数: 查看请求的返回结果: 查看服务响应的性能分析结果: 可能出问题的地方:Phoenix.数据库连接池(操作Phoenix)
- php截取指定两个字符之间的字符串
//截取指定两个字符之间的字符串 public function cut($begin,$end,$str){ $b = mb_strpos($str,$begin) + mb_strlen($beg ...
- Day4 - H - Following Orders POJ - 1270
Order is an important concept in mathematics and in computer science. For example, Zorn's Lemma stat ...
- Centos 8下命令行界面支持鼠标
yum install gpm* service gpm start systemctl enable gpm.service
- Acwing198 反素数
原题面:https://www.acwing.com/problem/content/200/ 题目大意:对于任何正整数x,其约数的个数记作g(x),例如g(1)=1.g(6)=4.如果某个正整数x满 ...
- java.lang.NumberFormatException: For input string: "F"
在通过myBatis执行sql时,报错: java.lang.NumberFormatException: For input string: "F" xml中sql内容为: &l ...
- Nature
1.主干 (1)<河图+洛书>:启发伏羲作八卦. (2)<三坟+五典>:失传:伏羲.神农.轩辕.少昊.颛顼.帝喾.唐尧.虞舜. (3)<八索+九丘>:失传:八卦之书 ...
- NetWork--记一次Http和TLS抓包
参考 前言 工具 wireshark IP 发送方IP: 150.236.224.39 服务IP: 10.210.164.20 消息 Http,Https消息使用org.apache.http.cli ...
- 2.15 使用web 编写一个简单记事本
首先陈列问题 (等待解决): 1. 界面是使用 H5 iframe 标签合并而成的,当窗口化之后点击任务栏,显示的内容会在任务栏的下边 希望可以找其他方式替代 (其他方法不熟练,不能应用) 如图 ...