zoukankan      html  css  js  c++  java
  • mybatis中的智能标签之二

    智能标签foreach-Array, 智能标签foreach-List数据组,智能标签 foreach list自定义类型

    接口:
    /**
    * 智能标签foreach-Array
    * @param num
    * @return
    */
    public List<Student> findByForeachArray(int[] num);

    /**
    * 智能标签foreach-List
    * @param list
    * @return
    */
    public List<Student> findByForeachList(List<Integer> list);


    /**
    * 智能标签 foreach list自定义类型
    * @param list
    * @return
    */
    public List<Student> findByForeachListStudent(List<Student> list);





    xml文件(小配置)
    <!--智能标签foreach-array-->
    <select id="findByForeachArray" resultType="student">
    SELECT *from Student
    <where>
    <if test="array.length>0">
    id in
    <foreach collection="array" open="(" close=")" separator="," item="id">
    #{id}
    </foreach>
    </if>
    </where>
    </select>

    <!--智能标签foreach-list数组-->
    <select id="findByForeachList" resultType="student">
    SELECT *from Student
    <where>
    <if test="list.size>0">
    id in
    <foreach collection="list" open="(" close=")" separator="," item="id">
    #{id}
    </foreach>
    </if>
    </where>
    </select>

    <!--智能标签foreach-list自定义类型-->
    <select id="findByForeachListStudent" resultType="student">
    SELECT *from Student
    <where>
    <if test="list.size>0">
    id in
    <foreach collection="list" open="(" close=")" separator="," item="it">
    #{it.id}
    </foreach>
    </if>
    </where>
    </select>





    测试类:

    /**
    * 智能标签foreach-array
    */
    @Test
    public void findByForeachArray(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    int[] num={2,3,4};
    List<Student> list = mapper.findByForeachArray(num);
    for (Student item:list){
    System.out.println(item.getName());
    }
    session.commit();
    session.close();
    }




    /**
    * 智能标签foreach-list数组
    */
    @Test
    public void findByForeachList(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    List<Integer> list=new ArrayList<Integer>();
    list.add(2);
    list.add(9);
    List<Student> lists = mapper.findByForeachList(list);
    for (Student item:lists){
    System.out.println(item.getName());
    }
    session.commit();
    session.close();
    }




    /**
    * 智能标签foreach-list自定义类型
    */
    @Test
    public void findByForeachListStudent(){
    SqlSession session = MyBatisUtil.getSession();
    IStudentDAO mapper = session.getMapper(IStudentDAO.class);
    List<Student> list=new ArrayList<Student>();
    Student student=new Student();
    student.setId(2);
    Student students=new Student();
    students.setId(9);
    list.add(student);
    list.add(students);
    List<Student> lists = mapper.findByForeachListStudent(list);
    for (Student item:lists){
    System.out.println(item.getName());
    }
    session.commit();
    session.close();
    }
     
  • 相关阅读:
    js判断浏览器类型
    如何编译JAR包
    Android + Eclipse + PhoneGap 3.4 安卓最新环境配置,部分资料整合网上资料,已成功安装.
    QQ互联简单例子,七彩花都提供
    Android + Eclipse + PhoneGap 2.9.0 安卓最新环境配置,部分资料整合网上资料,已成功安装.
    碎片化知识整理
    今天开始记录我每天的学习过程,补上昨晚的的笔记
    Appium 1.6.5安装环境配置 iOS篇
    Appium1.6.4-beta iPhone真机控件获取 app-inspector
    Appium1.6.4-beta 模拟器控件获取 App-inspector
  • 原文地址:https://www.cnblogs.com/sujulin/p/7588772.html
Copyright © 2011-2022 走看看