MyBatis智能标签

来源:互联网 发布:网络文明传播志愿服务 编辑:程序博客网 时间:2024/06/06 16:46
     <!--查询姓名中包含“雨”,并且年龄>20的学生信息-->    public  List<StudentInfo> findStudentsByCondition(Map<String,Object> map);
<!--多条件查询-->    <select id="findStudentsByCondition" resultType="StudentInfo">         select * from  studentinfo where stuname like '%' #{stuName} '%' and stuAge>#{stuAge}    </select>

多条件查询    @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();    }}

<!--查询姓名中包含“雨”,并且年龄>20的学生信息-->    public  List<StudentInfo> findStudentsByConditionMutliArgs(String stuName,int stuAge);
<!--多条件查询使用索引-->    <select id="findStudentsByConditionMutliArgs" resultType="StudentInfo">        select * from  studentinfo where stuname like '%' #{0} '%' and stuAge>#{1}    </select>

//多条件查询    @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();    }

//智能标签if    public List<StudentInfo> findByIf(StudentInfo stu);
<!--智能标签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>

//智能标签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();    }

//智能标签choose    public List<StudentInfo> findByChoose(StudentInfo stu);


<!--智能标签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>

//智能标签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();    }
//智能标签foreach    public  List<StudentInfo> findByForeachArray(int[] ids);
<!--智能标签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>
//智能标签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();    }
//智能标签foreach List<Integer>    public  List<StudentInfo> findByForeachList(List<Integer> list);
 <!--智能标签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<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();    }

//智能标签foreach List<StudentInfo>    public  List<StudentInfo> findByForeachListStudent(List<StudentInfo> list);}

 <!--智能标签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<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();    }