MyBatis_多查询条件问题
一、多查询条件无法整体接收问题的解决
在实际工作中,表单中所给出的查询条件有时是无法将其封装成一个对象,即查询方法只能携带多个参数,而不能携带将这多个参数进行封装的一个对象。对于这个问题,有两种解决方案:(1)根据Map查询;(2)使用索引号。
二、根据Map查询
1、修改Dao
import java.util.List;
import java.util.Map; import com.jmu.bean.Student; public interface IStudentDao {
// 根据条件查询问题
List<Student> selectStudentsByCondition(Map<String, Object> map);
}
com.jmu.dao.IStudentDao
2、修改Test
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.BasicConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.jmu.bean.Student;
import com.jmu.dao.IStudentDao;
import com.jmu.utils.MybatisUtils; public class MyTest {
private IStudentDao dao;
private SqlSession sqlSession; @Before
public void Before() {
sqlSession = MybatisUtils.getSqlSession();
dao = sqlSession.getMapper(IStudentDao.class);
BasicConfigurator.configure();
}
@After
public void after(){
if (sqlSession!=null) {
sqlSession.commit(); } } @Test
public void test08() {
Student stu = new Student("东东",21,95);
Map<String,Object> map=new HashMap<String,Object>();
map.put("nameCon", "小");
map.put("ageCon", 20);
map.put("stu", stu);
; List<Student> students = dao.selectStudentsByCondition(map);
for (Student student : students) {
System.out.println(student);
} } }
com.jmu.test.MyTest
3、修改map.xml
<mapper namespace="com.jmu.dao.IStudentDao">
<select id="selectStudentsByCondition" resultType="Student">
select id,name,age,score
from student
where name like '%' #{nameCon} '%'
and age >#{ageCon}
and score >#{stu.score} <!-- 也能放对象的属性 -->
</select>
</mapper>
mapper.xml
输出:
127 [main] DEBUG com.jmu.dao.IStudentDao.selectStudentsByCondition - <== Total: 1
Student [id=173, name=小明明, score=99.5, age=23]
三、使用索引号
在mapper.xml,#{ }中可以放的内容:
- 参数对象的属性
- 随意内容,此时的#{ }是个占位符
- 参数为map时的key
- 参数为map时,若key所对应的value为对象,即可将将对象的属性放入
- 参数的索引号
<mapper namespace="com.jmu.dao.IStudentDao">
<select id="selectStudentsByCondition" resultType="Student">
select id,name,age,score from student where name like '%' #{0} '%' and age > #{1} </select>
</mapper>
mapper.xml
import java.util.List;
import com.jmu.bean.Student; public interface IStudentDao {
// 根据条件查询问题
List<Student> selectStudentsByCondition(String name,int i);
}
IStudentDao
@Test
public void test08() { ; List<Student> students = dao.selectStudentsByCondition("明",20);
for (Student student : students) {
System.out.println(student);
} }
MyTest
输出:
Cause: org.apache.ibatis.binding.BindingException: Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]
失败,原因好像跟MyBatis的版本有关。
别人博客相关的截图



MyBatis_多查询条件问题的更多相关文章
- SQL Server-聚焦强制索引查询条件和Columnstore Index(九)
前言 本节我们再来穿插讲讲索引知识,后续再讲数据类型中的日期类型,简短的内容,深入的理解,Always to review the basics. 强制索引查询条件 前面我们也讲了一点强制索引查询的知 ...
- #ThinkPHP_3.2.2模型# where查询条件汇总
特别喜欢 ThinkPHP_3.2.3 框架的Model,结合官方手册及查看源代码,汇总出其大体用法: 核心转换方法: $this->parseWhere($where); $whereStr ...
- easyUI datagrid 根据查询条件 选中对应数据的行
开始 输入了 土豆,南瓜,再次是小青菜,每次输入点击搜索的时候(模糊查询),选中的当前数据对应的行 在做之前,在网上查询了许多资料,也在技术群里问过许多次,弄了好久终于好了. 第一次写博客真不知道写啥 ...
- 根据起止日期构建指定查询条件:第N周(yyyy-MM-dd/yyyy-MM-dd)
项目中有个查询模块中用到查询条件: 年和周. 以往我直接指定是第几周,后来测试反映如果直接选择周的话并不知道所选周代表的年月日,而无法最快查询数据,后更改查询条件如下: 指定一个起始年月,根据起始年月 ...
- SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法
本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...
- Thinkphp 带查询条件数据分页
//查询条件中如果有中文 $keyword= urldecode(I("request.keyword")); if ($keyword!=""){ $Mode ...
- jsp中查询条件的回显
后台框架为ssh,前台纯手写无框架是最老的写法,因为是接手别人的项目无法改变框架原型,只能基于修改. 进入正题: 我这里查询条件有两种input的text(文本框)和select(下拉框). 1.te ...
- mybatis 使用记录(二) 动态拼接查询条件
2016-12-16 阅读项目代码时,在项目的xml文件中发现如下写法: SELECT student_user_id FROM tbr_student_class WHERE 1=1 <if ...
- jeecg 扩展封装查询条件 时间段查询
使用jeecg框架开发的小伙伴们知道,添加查询条件,通常是我们加一个配置(query="true")就可以将该字段设置为查询条件.简单方便.但是这样的配置查询条件仅适用于输入框输入 ...
随机推荐
- linux下mysql启动出错
1.刚安装完就启动出错,是因为没有开msql服务,开启即可,service mysql start 2.MySQL: mysql is not running but lock exists rm / ...
- 【Kafka源码】处理请求
[TOC] 在KafkaServer中的入口在: apis = new KafkaApis(socketServer.requestChannel, replicaManager, groupCoor ...
- tornado超时机制
1.https://blog.lzhaohao.info/archive/build-a-comet-application-using-tornado-and-nginx/ 2.http://qin ...
- concurrent.futures
concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...
- NULL字段对于UNIQUE INDEX失效
- 一致性hash算法以及其在分布式系统中的应用(转)
初始架构
- Python之函数返回多个值
#!/usr/bin/env python26 #-*- coding:utf-8-*- def test(): a = 10 b = 20 return a,b #返回一个元组 atuple= te ...
- js实际工作中的技能点
1.基础知识 a.原型,原型链 b.作用域,闭包 c.异步,单线程 2.JS API a.DOM操作 b.Ajax c.事件绑定 3.开发环境 a.版本管理(git) b.模块化(require.js ...
- 了解Python列表的一些方法
首先定义一个名字列表,然后使用print() BIF在屏幕上显示这个列表. 接下来,使用len() BIF得出列表中有多少个数据项,然后再访问并显示第2个数据项的值: 创建了列表之后,可以使用列表方法 ...
- OPENGLES 绘制纹理带黑圈pre-multiplying
1. 问题 在进行 OpenGL 纹理混合的过程中,遇到一个诡异的现象,两个纹理混合的效果出人所料: 将一个ALPHA渐变的[胡须]加在另一张图片上,这个 [胡须]是由外向里逐渐增加透明度的,也就是最 ...