一、多查询条件无法整体接收问题的解决

在实际工作中,表单中所给出的查询条件有时是无法将其封装成一个对象,即查询方法只能携带多个参数,而不能携带将这多个参数进行封装的一个对象。对于这个问题,有两种解决方案:(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,#{ }中可以放的内容:

  1. 参数对象的属性
  2. 随意内容,此时的#{ }是个占位符
  3. 参数为map时的key
  4. 参数为map时,若key所对应的value为对象,即可将将对象的属性放入
  5. 参数的索引号
 <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_多查询条件问题的更多相关文章

  1. SQL Server-聚焦强制索引查询条件和Columnstore Index(九)

    前言 本节我们再来穿插讲讲索引知识,后续再讲数据类型中的日期类型,简短的内容,深入的理解,Always to review the basics. 强制索引查询条件 前面我们也讲了一点强制索引查询的知 ...

  2. #ThinkPHP_3.2.2模型# where查询条件汇总

    特别喜欢 ThinkPHP_3.2.3 框架的Model,结合官方手册及查看源代码,汇总出其大体用法: 核心转换方法: $this->parseWhere($where); $whereStr ...

  3. easyUI datagrid 根据查询条件 选中对应数据的行

    开始 输入了 土豆,南瓜,再次是小青菜,每次输入点击搜索的时候(模糊查询),选中的当前数据对应的行 在做之前,在网上查询了许多资料,也在技术群里问过许多次,弄了好久终于好了. 第一次写博客真不知道写啥 ...

  4. 根据起止日期构建指定查询条件:第N周(yyyy-MM-dd/yyyy-MM-dd)

    项目中有个查询模块中用到查询条件: 年和周. 以往我直接指定是第几周,后来测试反映如果直接选择周的话并不知道所选周代表的年月日,而无法最快查询数据,后更改查询条件如下: 指定一个起始年月,根据起始年月 ...

  5. SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法

    本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...

  6. Thinkphp 带查询条件数据分页

    //查询条件中如果有中文 $keyword= urldecode(I("request.keyword")); if ($keyword!=""){ $Mode ...

  7. jsp中查询条件的回显

    后台框架为ssh,前台纯手写无框架是最老的写法,因为是接手别人的项目无法改变框架原型,只能基于修改. 进入正题: 我这里查询条件有两种input的text(文本框)和select(下拉框). 1.te ...

  8. mybatis 使用记录(二) 动态拼接查询条件

    2016-12-16 阅读项目代码时,在项目的xml文件中发现如下写法: SELECT student_user_id FROM tbr_student_class WHERE 1=1 <if ...

  9. jeecg 扩展封装查询条件 时间段查询

    使用jeecg框架开发的小伙伴们知道,添加查询条件,通常是我们加一个配置(query="true")就可以将该字段设置为查询条件.简单方便.但是这样的配置查询条件仅适用于输入框输入 ...

随机推荐

  1. java工程师学习线路图

  2. [转载] KAFKA分布式消息系统

    转载自http://blog.chinaunix.net/uid-20196318-id-2420884.html Kafka[1]是linkedin用于日志处理的分布式消息队列,linkedin的日 ...

  3. Java build path && Deployment assembly && 编译路径 && 发布路径

    java build path java源文件,编译后,输出的路径,默认值为: *此时的源码文件夹在 /src deployment assembly 系统发布路径设置,将完成(或未完成)的项目对应的 ...

  4. concurrent.futures

    concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...

  5. pt-stalk

    1.名词解释 Collect forensic data about MySQL when problems occur 在问题发生的时候采集现场数据 pt-stalk waits for a tri ...

  6. TCP网络程序实例——服务器端与客户端交互

    实例02 客户端/服务器的交互 实例位置:光盘\Code\SL\14\02 视频位置:光盘\Video\14\ ◆ 服务器端 创建服务器端项目Server,在Main方法中创建TCP连接对象:然后监听 ...

  7. 关于HTML5新手应该知道的几点知识

    随着移动互联网的快速发展,HTML5迅速崛起,我们的生活的方方面面都被HTML5渗透着.HTML5在PC端.移动端上均应用广泛,被称为Web的未来.而随着Google正式停止支持Swiffy,HTML ...

  8. laravel中with()方法,has()方法和whereHas()方法的区别

    with() with()方法是用作"渴求式加载"的,那主要意味着,laravel将会伴随着主要模型预加载出确切的的关联关系.这就对那些如果你想加在一个模型的所有关联关系非常有帮助 ...

  9. 异常:Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException

    这个异常是出现在注入配置文件中配置好的属性时报错的: Injection of autowired dependencies failed; nested exception is java.lang ...

  10. AndroidStudio cannot resolve symbol 解决办法 清楚缓存

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...