包装类: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. Visual studio 下C++工程相关经验

    1.链接其他库调试时产生告警: warning LNK4099: 未找到 PDB“vc100.pdb” 解决方案:属性 -> C/C++ -> 输出文件 -> 程序数据库文件名 -& ...

  2. Hadoop概念学习系列之谈hadoop/spark里为什么都有,键值对呢?(四十)

    很少有人会这样来自问自己?只知道,以键值对的形式处理数据并输出结果,而没有解释为什么要以键值对的形式进行. 包括hadoop的mapreduce里的键值对,spark里的rdd里的map等. 这是为什 ...

  3. js堆和栈

    一.栈 栈:英文为“stack”: 定义:一种存放数据的内存区域: 特点: ①LIFO,后进先出: 可视化描述: ②调用栈,函数或者子例程像堆积木一样存放,以实现层层调用: 函数调用形成一个栈帧: f ...

  4. Jmeter(十六)Logic Controllers 之 Runtime Controller

    Runtime Controller-----运行时间控制器:控制其下的Sampler运行时间. 该控制器较为简单,官方文档也没作太多说明.照着Blazemeter写个例子: 运行,查看结果. 可以看 ...

  5. [UE4]C++代码实现播放粒子特效

    转自:http://aigo.iteye.com/blog/2273345 函数参数说明(在GameplayStatics.h中) 将一个粒子放到指定位置上播放: 另一种重载形式: 将一个粒子atta ...

  6. Linux安装jsvc,及Linux服务开发

    在linux上以服务的方式启动java程序,需要提前安装jsvc.linux是利用daemon(jsvc)构建java守护进程. 编译 daemon 安装JSVC 1 下载文件,http://comm ...

  7. (转)查询或修改iPhone的短信服务中心号码(iOS通用)

    有些时候会有提示你自己的iPhone无法发送短信,原因是没有设置短信中心号码,下面就介绍一下如何设置iPhone的短信中心号码和iPhone查看短信中心号码的方法.(iOS通用) iPhone的设置短 ...

  8. plugin 看不到update按钮

    然后再按一下tab键,焦点就会在 update上了.然后再回车.

  9. SQL SERVER 数据库字段简单加密解密

    --------加密函数----------- )) RETURNS varbinary(max) AS BEGIN declare @pwd varbinary(max) SELECT @pwd = ...

  10. 第8章 传输层(1)_TCP/UDP协议的应用场景

    1. 传输层的两个协议 1.1 TCP和UDP协议的应用场景 (1)TCP协议:如果要传输的内容比较多,需要将发送的内容分成多个数据包发送.这就要求在传输层用TCP协议,在发送方和接收方建立连接,实现 ...