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 */@Testpublic  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数组 */@Testpublic  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自定义类型 */@Testpublic  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();}
 
时间: 2024-08-01 06:23:07

mybatis中的智能标签之二的相关文章

mybatis中#{}和${}的区别 (二)

mybatis中#{}和${}的区别 1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程 2.使用 Statement 对象.在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理.PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处. 3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, prepared

MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(二)

(本文示例工程源代码下载地址:http://down.51cto.com/data/1975295) 在上一篇博文的最后,介绍了使用@PostConstruct注解标注StudentDao的init方法,这样在Spring完成依赖注入后此方法即会被Spring调用,从而也就完成了studentMapper的初始化工作. 如果只有StudentDao一个DAO类,这样做当然没有问题.不过在实际应用中,必定存在多个DAO类.每个DAO类的初始化方法,除了传入的映射器接口类型(如StudentMapp

MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(一)

在MyBatis中,当我们编写好访问数据库的映射器接口后,MapperScannerConfigurer就能自动成批地帮助我们根据这些接口生成DAO对象(),然后我们再使用Spring把这些DAO对象注入到业务逻辑层的对象(Service类的对象).因此,在这种情况下的DAO层,我们几乎不用编写代码,而且也没有地方编写,因为只有接口.这固然方便,不过如果我们需要在DAO层写一些代码的话,这种方式就无能为力了.此时,MyBatis-Spring提供给我们的SqlSessionDaoSupport类

MyBatis中的OGNL教程

MyBatis中的OGNL教程 有些人可能不知道MyBatis中使用了OGNL,有些人知道用到了OGNL却不知道在MyBatis中如何使用,本文就是讲如何在MyBatis中使用OGNL. 如果我们搜索OGNL相关的内容,通常的结果都是和Struts有关的,你肯定搜不到和MyBatis有关的,虽然和Struts中的用法类似但是换种方式理解起来就有难度. MyBatis常用OGNL表达式 e1 or e2 e1 and e2 e1 == e2,e1 eq e2 e1 != e2,e1 neq e2

MySQL实现序列(Sequence)效果以及在Mybatis中如何使用这种策略

前言: 在oracle中一般使用序列(Sequence)来处理主键字段,在MySQL中是没有序列的,但是MySQL有提供了自增长(increment)来实现类似的目的,但也只是自增,而不能设置步长.开始索引.是否循环等.最重要的是一张表只能设置一个字段使用自增,但有的时候我们需要两个或两个以上的字段实现自增(单表多字段自增),MySQL本身是实现不了的,但我们可以用创建一个序列表,使用函数来获取序列的值 一 MySQL中序列的实现 (1)新建一个测试表: DROP TABLE IF EXISTS

mybatis中Invalid bound statement (not found) 和 Result Maps collection already contains value for...错误解决方案

一.Invalid bound statement (not found) 使用mybatis有时候会报Invalid bound statement (not found)这种错误,总结了下,可能有两种情况如下: 1.mybatis的对应的mapper.xml找不到对应的命名sql或者名称与mapper接口名称不一致. 2.xml文件与接口名称都对,但是在mybatis配置文件中漏掉了配置,也会报这种错误. 二.Result Maps collection already contains v

Mybatis中的resultType和resultMap

一.概述 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在. 在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值. ①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给r

Mybatis中传参包There is no getter for property named &#39;XXX&#39; in &#39;class java.lang.String&#39;

一.发现问题 <select id="queryStudentByNum" resultType="student" parameterType="string"> select num,name,phone from student <where> <if test = " num!=null and num!='' "> AND num = #{num} </if> <

Mybatis中传参包There is no getter for property named &#39;roomName&#39; in &#39;class java.lang.String&#39;

一.发现问题 <select id="queryStudentByNum" resultType="student" parameterType="string"> select num,name,phone from student  <where> <if test = " num!=null and num!='' ">AND num = #{num}</if></w