idea

接口:

 //04.查询学生姓名中包含"星星"的 ,并且年里在20岁以上的学生信息
    public List<StudentInfo> findStudentListLike(StudentInfo info);
    //05.查询姓名中包含“雨”,并且年龄>20的学生信息
    public  List<StudentInfo> findStudentsByCondition(Map<String,Object> map);
    //05.查询姓名中包含“雨”,并且年龄>20的学生信息
    public  List<StudentInfo> findStudentsByConditionMutliArgs(String stuName,int stuAge);
    //06.智能标签if
    public List<StudentInfo> findByIf(StudentInfo stu);
    //06.智能标签choose
    public List<StudentInfo> findByChoose(StudentInfo stu);
    //07.智能标签foreach
    public  List<StudentInfo> findByForeachArray(int[] ids);
    //07.智能标签foreach List<Integer>
    public  List<StudentInfo> findByForeachList(List<Integer> list);
    //07.智能标签foreach List<StudentInfo>
    public  List<StudentInfo> findByForeachListStudent(List<StudentInfo> list);

小配置·:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="bdqn.dao.Departmentdao">

 <!--模糊查询-->
    <select id="findStudentListLike" resultType="StudentInfo">
        <!--select * from studentinfo where stuname like concat('%',#{stuName},'%') and stuAge>#{stuAge}-->
            select * from studentinfo where stuname like '%${stuName}%' and stuAge>#{stuAge}
    </select>
    <!--多条件查询-->
    <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>

    <!--智能标签foreach List-->
    <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>
    <!--智能标签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="stuno">
                    #{stuno}
                </foreach>
            </if>
        </where>
    </select>

    <!--智能标签foreach Array-->
    <select id="findByForeachArray" resultType="StudentInfo">
        select * from studentinfo
        <where>
            <if test="array.length>0">
                stuid in
                <foreach collection="array" open="(" close=")" separator="," item="stuno">
                    #{stuno}
                </foreach>
            </if>
        </where>
    </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>

    <!--智能标签if-->
    <select id="findByIf" resultType="StudentInfo">
        select * from studentinfo
        <where>
            <if test="stuName!=null"><!--用户录入的姓名字段-->
                 and stuName like '%' #{stuName} '%'
            </if>
            <if test="stuAge!=null">
                and stuAge>#{stuAge}
            </if>
        </where>
    </select>
</mapper>
测试类:

  
  
package cn.happy.test;
import cn.happy.dao.IDeptDAO; import cn.happy.dao.IStudentInfoDAO; import cn.happy.entity.Dept; import cn.happy.entity.Emp; import cn.happy.entity.StudentInfo; import cn.happy.util.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test;
import java.util.*;
/**  * Created by Happy on 2017-07-09.  */ public class MyBatisTest0712 {     //07.多表连接查询 一对多  单条SQL     @Test     public void testOneToMany(){         SqlSession session= MyBatisUtil.getSession();         IDeptDAO dao = session.getMapper(IDeptDAO.class);         Dept dept = dao.getEmpsByDeptNo(5);         System.out.println(dept.getDeptName());         for (Emp emp:dept.getEmps()) {             System.out.println(emp.getEmpName());         }
        session.close();     }
    //06.智能标签Foreach List<StudentInfo>     @Test     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(2);         StudentInfo s2=new StudentInfo();         s2.setStuId(5);         list.add(s1);         list.add(s2);         List<StudentInfo> list2 = dao.findByForeachListStudent(list);         for (StudentInfo stuinfo:list2) {
            System.out.println(stuinfo.getStuName());         }         session.close();     }
    //06.智能标签Foreach List<Integer>     @Test     public void testForeachList(){         SqlSession session= MyBatisUtil.getSession();         IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);         List<Integer> list=new ArrayList<Integer>();         list.add(2);         list.add(5);         List<StudentInfo> list2 = dao.findByForeachList(list);         for (StudentInfo stuinfo:list2) {
            System.out.println(stuinfo.getStuName());         }         session.close();     }
    //06.智能标签Foreach array     @Test     public void testForeachArray(){         SqlSession session= MyBatisUtil.getSession();         IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);         int[] ids={2,5};         List<StudentInfo> list = dao.findByForeachArray(ids);         for (StudentInfo stuinfo:list) {
            System.out.println(stuinfo.getStuName());         }         session.close();     }
    //05.智能标签choose     @Test     public void testChoose(){         SqlSession session= MyBatisUtil.getSession();         IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);         StudentInfo stu=new StudentInfo();         // stu.setStuName("雨");        // stu.setStuAge(20);         List<StudentInfo> list = dao.findByChoose(stu);         for (StudentInfo stuinfo:list) {
            System.out.println(stuinfo.getStuName());         }         session.close();     }
    //05.智能标签if     @Test     public void testIf(){         SqlSession session= MyBatisUtil.getSession();         IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);         StudentInfo stu=new StudentInfo();        // stu.setStuName("雨");        stu.setStuAge(20);         List<StudentInfo> list = dao.findByIf(stu);         for (StudentInfo stuinfo:list) {
            System.out.println(stuinfo.getStuName());         }         session.close();     }
    //04.多条件查询     @Test     public void testSelectLikeMulti(){         SqlSession session= MyBatisUtil.getSession();         IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);
        List<StudentInfo> list = dao.findStudentsByConditionMutliArgs("雨",20);         for (StudentInfo stu:list) {             System.out.println(stu.getStuName());         }         session.close();     }
    //04.多条件查询     @Test     public void testSelectLike(){         SqlSession session= MyBatisUtil.getSession();         IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class);         Map<String,Object> map=new HashMap<String,Object>();         map.put("stuName","雨");         map.put("stuAge",20);         List<StudentInfo> list = dao.findStudentsByCondition(map);         for (StudentInfo stu:list) {             System.out.println(stu.getStuName());         }         session.close();     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值