小配置:
<!--多条件查询-->
<select id="findStudentsByCondition" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{stuName} '%' and stuAge>#{stuAge}
</select>
<!--多条件查询使用索引-->
<select id="findStudentsByConditionMutliArgs" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{0} '%' and stuAge>#{1}
</select>
<!--智能标签if-->
<select id="findByIf" resultType="StudentInfo">
select * from studentinfo where 1=1
<if test="stuName!=null">
and stuName like '%' #{stuName} '%'
</if>
<if test="stuAge!=null">
and stuAge > #{stuAge}
</if>
</select>
<!--智能标签Choose-->
<select id="findByChoose" resultType="StudentInfo">
select * from studentinfo
<where>
<choose>
<when test="stuName!=null">
and stuName like '%' #{stuName} '%'
</when>
<when test="stuAge!=null">
and stuAge > #{stuAge}
</when>
<otherwise>
and 1=2
</otherwise>
</choose>
</where>
</select>
<!--智能标签foreachArray-->
<select id="findByForeachArray" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="array.length>0">
stuId in
<foreach collection="array" open="(" close=")" separator="," item="stuId">
#{stuId}
</foreach>
</if>
</where>
</select>
<!--智能标签foreach List-->
<select id="findByForeachList" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuId in
<foreach collection="list" open="(" close=")" separator="," item="stuId">
#{stuId}
</foreach>
</if>
</where>
</select>
<!--智能标签findByForeach ListStudent-->
<select id="findByForeachListStudent" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuId in
<foreach collection="list" open="(" close=")" separator="," item="stu">
#{stu.stuId}
</foreach>
</if>
</where>
</select>
public class MyBatisTest0712 {
@Test
//05.多条件查询
public void testSelectLike(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
Map<String,Object> map=new HashMap<String,Object>();
map.put("stuName","zhang");
map.put("stuAge",17);
List<StudentInfo> list=dao.findStudentsByCondition(map);
for(StudentInfo stu:list){
System.out.println(stu.getStuName());
}
session.close();
}
@Test
//06.多条件查询使用索引
public void testSelectLikeMulti(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
List<StudentInfo> list=dao.findStudentsByConditionMutliArgs("zhang",17);
for(StudentInfo stu:list){
System.out.println(stu.getStuName());
}
session.close();
}
@Test
//07.智能标签if
public void testIf(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo stu=new StudentInfo();
stu.setStuName("zhang");
stu.setStuAge(17);
List<StudentInfo> list=dao.findByIf(stu);
for (StudentInfo stuinfo:list){
System.out.println(stuinfo.getStuName());
}
session.close();
}
@Test
//08.智能标签choose
public void testChoose(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
StudentInfo stu=new StudentInfo();
stu.setStuName("zhang");
stu.setStuAge(17);
List<StudentInfo> list=dao.findByChoose(stu);
for (StudentInfo stuinfo:list){
System.out.println(stuinfo.getStuName());
}
session.close();
}
@Test
//09.智能标签foreachArray
public void testForeachArray(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
int[] ids={2,3};
List<StudentInfo> list=dao.findByForeachArray(ids);
for (StudentInfo stuinfo:list){
System.out.println(stuinfo.getStuName());
}
session.close();
}
@Test
//10.智能标签foreach List<Integer>
public void testForeachList(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
List<StudentInfo> list2 = dao.findByForeachList(list);
for (StudentInfo stuinfo:list2){
System.out.println(stuinfo.getStuName());
}
session.close();
}
@Test
//11.智能标签foreach List<StudentInfo>
public void testForeachListStudent(){
SqlSession session= MyBatisUtil.getSession();
IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
List<StudentInfo> list=new ArrayList<StudentInfo>();
StudentInfo s1=new StudentInfo();
s1.setStuId(1);
StudentInfo s2=new StudentInfo();
s1.setStuId(2);
list.add(s1);
list.add(s2);
List<StudentInfo> list2 = dao.findByForeachListStudent(list);
for (StudentInfo stuinfo:list2){
System.out.println(stuinfo.getStuName());
}
session.close();
}
}
接口类:
//07.多条件查询
public List<StudentInfo> findStudentsByCondition(Map<String,Object> map);
//08.多条件查询使用索引
public List<StudentInfo> findStudentsByConditionMutliArgs(String stuName,int stuAge);
//09.智能标签if
public List<StudentInfo> findByIf(StudentInfo stu);
//10.智能标签if
public List<StudentInfo> findByChoose(StudentInfo stu);
//11.智能标签foreachArray
public List<StudentInfo> findByForeachArray(int[] ids);
//12.智能标签foreach List<Integer>
public List<StudentInfo> findByForeachList(List<Integer> list);
//12.智能标签foreach List<StudentInfo>
public List<StudentInfo> findByForeachListStudent(List<StudentInfo> list);