mybatis多查询

1.使用map

Javabean

package com.cbh.beans;

public class Student {
private int id ;
private int age;
private String name;
private double score;
public Student() {
	
}
public Student(String name,int age,double score){
	super();
	
	this.age=age;
	this.name =name;
	this.score=score;
	
}

public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
@Override
public String toString() {
	return "Student [id=" + id + ", age=" + age + ", name=" + name + ", score="
			+ score + "]";
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public double getScore() {
	return score;
}
public void setScore(double score) {
	this.score = score;
}
}

dao:

package com.cbh.dao;

import java.util.List;
import java.util.Map;

import com.cbh.beans.Student;

public interface IStudent {
List<Student> selectStudentsByCondition(Map<String,Object> map);



}
mapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cbh.dao.IStudent">
<select id="selectStudentsByCondition" resultType="Student">
select id,name,age,score from student1 where name like '%' #{nameCon} '%' and age>#{ageCon} and score>#{scoreCon}
</select>
</mapper>
		

测试类

package com.cbh.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import com.cbh.Utils.MybatisUtils;
import com.cbh.beans.Student;
import com.cbh.dao.IStudent;

public class mytest {
	private IStudent dao;
	@Before
	public void before() {
	SqlSession sqlSession=	MybatisUtils.getSqlSession();
		dao =sqlSession.getMapper(IStudent.class) ;
	}
@After
public void after(){
if(sqlsession!=null){
sqlsession.close();
}
	
}
@Test
public void test8(){
Map<String ,Object>map=new HashMap<String,Object>();
map.put("nameCon", "大");
map.put("ageCon", 23);
map.put("scoreCon",99);
	List<Student> student=dao.selectStudentsByCondition(map);
	for (Student student2 : student) {
		System.out.println(student2);
	}
}  
}

使用索引号:

dao:

package com.cbh.dao;

import java.util.List;
import com.cbh.beans.Student;

public interface IStudent {
List<Student>selectStudentsByCondition(String name,int age,int score);


}
 测试类:
package com.cbh.test;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import com.cbh.Utils.MybatisUtils;
import com.cbh.beans.Student;
import com.cbh.dao.IStudent;

public class mytest {
	private IStudent dao;
	@Before
	public void before() {
	SqlSession sqlSession=	MybatisUtils.getSqlSession();
		dao =sqlSession.getMapper(IStudent.class) ;
	}
	
@After
public void after(){
if(sqlsession!=null){
sqlsession.close();
}
	
}
@Testpublic void test8(){
List<Student> student=dao.selectStudentsByCondition("大", 24, 96);
for (Student student2 : student) {
System.out.println(student2);
}
}
 }


mapper:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cbh.dao.IStudent">
<select id="selectStudentsByCondition" resultType="Student">
select id,name,age,score from student1 where name like '%' #{0} '%'
and age>#{1} and score>#{2}
</select> 
<!-- 
#{}中可以放入的内容
1.参数对象的属性
2.随意的内容,此时#{}是个占位符
3.参数为map时的key
4.参数为map时,若key所对应的value为对象,则可以将该对象的属性放入
5.参数的索引号(本次就是参数的索引号)
 -->
</mapper>
		









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值