超全MyBatis动态SQL详解 MyBatis数据库教程( 四 )


文章插图
姓名和性别都不存在时查询, 此时此刻 。where 不会再出现了 。

超全MyBatis动态SQL详解 MyBatis数据库教程

文章插图
4.2 set
set 标签也类似 。在 [2.2 在 UPDATE 更新列中使用 if 标签] 中 。如果我们的途径 updateByPrimaryKeySelective 没有使用
4.3 trim
set 和 where 其实都是 trim 标签的一种类别 。该两种功能都可以使用 trim 标签进行实现 。
4.3.1 trim 来表示 where
如以上的 where 标签 。我们也完全可以编辑成
trim prefix="where" prefixOverrides="AND |OR"/trim表示当 trim 中含有内容时 。添加 where 。且第一个为 and 或 or 时 。会将其去掉 。而如果没有内容 。则不添加 where 。
4.3.2 trim 来表示 set
相对应的 。set 标签可以如下表示
trim prefix="SET" suffixOverrides=","/trim表示当 trim 中含有内容时 。添加 set 。且末尾的内容为 , 时 。会将其去掉 。而没有内容 。不添加 set
4.3.3 trim 的几个属性
prefix: 当 trim 元素包含有内容时 。增加 prefix 所指定的前缀prefixOverrides: 当 trim 元素包含有内容时 。去除 prefixOverrides 指定的 前缀suffix: 当 trim 元素包含有内容时 。增加 suffix 所指定的后缀suffixOverrides: 当 trim 元素包含有内容时 。去除 suffixOverrides 指定的后缀5 foreach 标签
foreach 标签可以对数组 。Map 或实现 Iterable 接口 。
foreach 中有以下几个属性:
collection: 必填 。集合/数组/Map的名称.item: 变量名 。即从迭代的对象中取出的每一个值index: 索引的属性名 。当迭代的对象为 Map 时 。该值为 Map 中的 Key.open: 循环开头的字符串close: 循环结束的字符串separator: 每当循环的分隔符很多的比很好理解 。collection 中的值大概怎么设定呢?
跟接口途径中的参数有关 。
1. 只有一个数组参数或集合参数
默认状态: 集合collection=list 。数组是collection=array
介绍: 使用 @Param 来指定参数的名称 。如我们在参数前@Param("ids") 。则就填编辑 collection=ids
2. 多参数
多参数请使用 @Param 来指定 。不然SQL中会很不方便
3. 参数是Map
指定为 Map 中的对应的 Key 就可以 。其实上面的 @Param 末尾也是转化为 Map 的 。
4. 参数是对象
使用属性.属性就可以 。
5.1 在 where 中使用 foreach
在 where条件中使用 。如按id集合查询 。按id集合删除等 。
5.1.1 查询条件
我们渴望查询玩家 id 集合中的任何玩家消息 。
5.1.2 动态 SQL
函数接口
/** * 获得 id 集合中的玩家消息 * @param ids * @return */ ListStudent selectByStudentIdList(ListInteger ids);对应 SQL
select id="selectByStudentIdList" resultMap="BaseResultMap" select include refid="Base_Column_List" / from student where student_id in foreach collection="list" item="id" open="(" close=")" separator="," index="i" #{id} /foreach /select5.1.3 测试
@Test public void selectByStudentIdList() { SqlSession sqlSession = null; sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); ListInteger ids = new LinkedList(); ids.add(1); ids.add(3); ListStudent students = studentMapper.selectByStudentIdList(ids); for (int i = 0; i students.size(); i++) { System.out.println(ToStringBuilder.reflectionToString(students.get(i), ToStringStyle.MULTI_LINE_STYLE)); } sqlSession.commit(); sqlSession.close(); }结果
超全MyBatis动态SQL详解 MyBatis数据库教程

文章插图
5.2 foreach 实现批量插入
可以通过foreach来实现批量插入 。
5.2.1 动态SQL
接口途径
/** * 批量插入学生 */ int insertList(ListStudent students);对应的SQL
insert id="insertList" insert into student(name, phone, email, sex, locked) values foreach collection="list" item="student" separator="," ( #{student.name}, #{student.phone},#{student.email}, #{student.sex},#{student.locked} ) /foreach /insert5.2.2 测试
@Test public void insertList() { SqlSession sqlSession = null; sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); ListStudent students = new LinkedList(); Student stu1 = new Student(); stu1.setName("批量01"); stu1.setPhone("13888888881"); stu1.setLocked((byte) 0); stu1.setEmail("13888888881@138.com"); stu1.setSex((byte) 1); students.add(stu1); Student stu2 = new Student(); stu2.setName("批量02"); stu2.setPhone("13888888882"); stu2.setLocked((byte) 0); stu2.setEmail("13888888882@138.com"); stu2.setSex((byte) 0); students.add(stu2); System.out.println(studentMapper.insertList(students)); sqlSession.commit(); sqlSession.close(); }结果

推荐阅读