智能标签

小配置:

    <!--多条件查询-->
    <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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值