包装类:QueryVO.java

package pojo;

import java.util.ArrayList;
import java.util.List; public class QueryVO { private User user; private List<Integer> ids; public List<Integer> getIds() {
return ids;
} public void setIds(List<Integer> ids) {
this.ids = ids;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace命名空间,隔离sql文件 -->
<!-- #{}占位符相当于jdbc的? -->
<!-- ${value} 字符串拼接 -->
<!-- 动态代理开发原则
1、namespace必须是接口的全路径
2、接口的方法必须与sql的id一致
3、接口的入参与parameterType类型一致
4、接口的返回值必须与resultType类型一致
-->
<mapper namespace="mapper.UserMapper">
<!-- sql片段 -->
<sql id="user_sql">
id, username,birthday,sex,address
</sql> <select id="getUserBYId" parameterType="int" resultType="pojo.User">
select
<include refid="user_sql"></include>
from user where id = #{id}
</select>
<select id="getUSerByUserName" parameterType="String" resultType="pojo.User">
select id, username,birthday,sex,address from user where username like '%${value}%'
</select>
<select id="getUSerByqueryVo" parameterType="queryVo" resultType="user">
select id, username,birthday,sex,address from user where username
like '%${user.username}%'
</select>
<select id="findUserCount" resultType="Integer">
select count(1) from user
</select> <select id="getUSerByPojo" parameterType="user" resultType="pojo.User">
select id, username,birthday,sex,address from user
<!--自动补上where关键字 处理多余的and 有where标签就不使用where -->
<where>
<if test="username !=null and username != ''">
username like '%${value}%'
</if>
<if test="sex !=null and sex != ''">
and sex like #{sex}
</if>
</where>
</select> <select id="getUSerByIds" parameterType="queryVo" resultType="pojo.User">
select
<include refid="user_sql"></include>
from user
<!--自动补上where关键字 处理多余的and 有where标签就不使用where -->
<where>
<!-- foreach 循环标签 -->
<!--collection 要遍历的集合 -->
<foreach collection="ids" open="id IN(" item="uid" separator="," close=")">
#{uid}
</foreach>
</where>
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace命名空间,隔离sql文件 -->
<!-- #{}占位符相当于jdbc的? -->
<!-- ${value} 字符串拼接 -->
<!-- 动态代理开发原则
1、namespace必须是接口的全路径
2、接口的方法必须与sql的id一致
3、接口的入参与parameterType类型一致
4、接口的返回值必须与resultType类型一致
-->
<mapper namespace="mapper.UserMapper">
<!-- sql片段 -->
<sql id="user_sql">
id, username,birthday,sex,address
</sql> <select id="getUserBYId" parameterType="int" resultType="pojo.User">
select
<include refid="user_sql"></include>
from user where id = #{id}
</select>
<select id="getUSerByUserName" parameterType="String" resultType="pojo.User">
select id, username,birthday,sex,address from user where username like '%${value}%'
</select>
<select id="getUSerByqueryVo" parameterType="queryVo" resultType="user">
select id, username,birthday,sex,address from user where username
like '%${user.username}%'
</select>
<select id="findUserCount" resultType="Integer">
select count(1) from user
</select> <select id="getUSerByPojo" parameterType="user" resultType="pojo.User">
select id, username,birthday,sex,address from user
<!--自动补上where关键字 处理多余的and 有where标签就不使用where -->
<where>
<if test="username !=null and username != ''">
username like '%${value}%'
</if>
<if test="sex !=null and sex != ''">
and sex like #{sex}
</if>
</where>
</select> <select id="getUSerByIds" parameterType="queryVo" resultType="pojo.User">
select
<include refid="user_sql"></include>
from user
<!--自动补上where关键字 处理多余的and 有where标签就不使用where -->
<where>
<!-- foreach 循环标签 -->
<!--collection 要遍历的集合 -->
<foreach collection="ids" open="id IN(" item="uid" separator="," close=")">
#{uid}
</foreach>
</where>
</select>
</mapper>

测试

    @Test
public void getUSerByIds() {
SqlSession openSession = SqlSessionFactoryUtil.getSqlSessionFactory().openSession();
//获得接口实现类
UserMapper mapper = openSession.getMapper(UserMapper.class);
QueryVO vo = new QueryVO();
vo.setIds(Arrays.asList(1,2,3,4,5,6));
List<User> uSerByIds = mapper.getUSerByIds(vo);
for (User user : uSerByIds) {
System.out.println(user);
}
openSession.close();
}

Mybatis学习5标签:if,where,sql,foreach的更多相关文章

  1. MyBatis学习 之 三、动态SQL语句

    目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...

  2. MyBatis学习 之 四、动态SQL语句

    有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Oracle的序列.mysq ...

  3. MyBatis学习(二)、SQL语句映射文件(1)resultMap

    二.SQL语句映射文件(1)resultMap SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyB ...

  4. Mybatis学习(8)动态sql语句

    Mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...

  5. mybatis学习笔记四(动态sql)

    直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity; /** * 此类是: 用户实体类 * @author hpc * @ ...

  6. mybatis学习(九)——动态sql

    MyBatis 的强大特性之一便是它的动态 SQL.可以根据不同条件拼接 SQL 语句. 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似.主要由以下几种元素. if wh ...

  7. Mybatis学习总结三(动态SQL)

    通过mybatis提供的各种标签方法实现动态拼接sql. 一.if 和 where <select id="findUserList" parameterType=" ...

  8. Mybatis映射文件标签(关于sql)

    Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...

  9. MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存

    二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...

随机推荐

  1. 解决“chrome adobe flash player不是最新版本”的方法

    chrome地址栏输入chrome://components 更新flash后,重启chrome即可,可能需要搭梯子才能更新.

  2. Linux From Scratch [3]

    1. 为了编译glibc,我们需要kernel header. make mrproper # clean kernel tree make INSTALL_HDR_PATH=dest headers ...

  3. spring 事务的配置学习

    1.spring事务管理器接口PlatformTransactionManager 接口中的方法 获取事务状态信息 -TransactionStatus getTransaction(Transact ...

  4. bzoj5006: [THUWC2017 Bipartite]随机二分图

    某人在玩一个非常神奇的游戏.这个游戏中有一个左右各 nnn 个点的二分图,图中的边会按照一定的规律随机出现. 为了描述这些规律,某人将这些边分到若干个组中.每条边或者不属于任何组 (这样的边一定不会出 ...

  5. .NET数据请求序列化

    这是一个.ner框架中经常用到的数据映射类,此类主要承接业务应用做数据的增删查改操作.通过序列化将查询数据映射为需要的类. 由于经常会用到,这里收藏一下. data用作SQL数据库链接操作: Repo ...

  6. 使用jquery.mCustomScrollbar自定义滚动条(3)callback onCreate

    碰到了一个问题,想简洁,所以在页面上使用 <div class="div_box mCustomScrollbar" data-mcs-theme="dark-3& ...

  7. 【转载】这样去写你的HTML

    昨天在 twitter 上说,怎么忍心把页面写得这么难用?是的,这个世界还有一群人等着我们创建出来的东西,可以让他们的生活能过得更容易呢.比如那些需要读屏软件的用户.作为一个前端,我们又怎么会忍心呢. ...

  8. IE6/7 单选按钮 radio 无法选中解决方法

    原文地址:http://blog.sina.com.cn/s/blog_74d6cedd0100ugih.html 今天在做一个页面,居然ff没问题,ie6/7上浏览的时候radio单选按钮不能被选中 ...

  9. MySQL事务提交过程(转载)

    http://blog.csdn.net/sofia1217/article/details/53968214 上一篇文章我们介绍了在关闭binlog的情况下,事务提交的大概流程.之所以关闭binlo ...

  10. [UE4]条件融合动画: Blend Posed by int

    Aim Group=0:使用动画“Blend Pose 0” Aim Group=1:使用动画“Blend Pose 1”