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

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

别人博客相关的截图

最新文章

  1. 上个项目的一些反思 III
  2. eclipse-4.4.2安装Groovy插件(其他版本eclipse可参考)
  3. 用计算器计算“异或CRC”
  4. SourceTree克隆仓库时,总是提示输入密码
  5. hdu 4352 XHXJ&#39;s LIS 数位DP
  6. 【剑指offer 面试题15】链表中倒数第K个结点
  7. 在Oracle 11g r2中,EXP无法导出个别空的表
  8. PHP命名空间(Namespace)的使用详解(转)
  9. UNIX网络编程——使用select函数编写客户端和服务器
  10. Mysql加锁过程详解(9)-innodb下的记录锁,间隙锁,next-key锁
  11. MATLAB多项式运算
  12. 前三次OO作业小结
  13. 为什么要用Thrift
  14. Eclipse自动生成方法注释 快捷键
  15. jquery双击事件会触发单击事件
  16. excel 鼠标上下左右移动
  17. oracle Plsql 执行update或者delete时卡死问题解决办法
  18. Mysql的基本语句
  19. 修改js confirm alert 提示框文字
  20. 纯js轮播图练习-2,js+css旋转木马层叠轮播

热门文章

  1. ASP.NET没有魔法——ASP.NET MVC Controller的实例化与执行
  2. [转载] Cassandra入门 框架模型 总结
  3. CLR设计类型之接口
  4. 【python】Python的单例模式
  5. (11.20)Java小知识!
  6. 基于Spark和SparkSQL的NetFlow流量的初步分析——scala语言
  7. python 面对post分页爬虫
  8. 转载--Typecho install.php 反序列化导致任意代码执行
  9. Python 面向对象(三) 魔术方法
  10. 永中DCS文档转换服务其它产品对比