包装类: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. 目前还活着的erlang web框架的比较

    参见 https://github.com/ChicagoBoss/ChicagoBoss/wiki/Comparison-of-Erlang-Web-Frameworks

  2. Python 简说 list,tuple,dict,set

    python 是按缩进来识别代码块的 . 缩进请严格按照Python的习惯写法:4个空格,不要使用Tab,更不要混合Tab和空格,否则很容易造成因为缩进引起的语法错误. list  有序集合  访问不 ...

  3. pm2 常用命令

    pm2 是一个带有负载均衡功能的Node应用的进程管理器.当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的.它非常适合IaaS结构,但不要把它用于 ...

  4. Netty Tutorial Part 1: Introduction to Netty [z]

    Netty Tutorial, Part 1: Introduction to Netty Update:  Part 1.5 Has Been Published: Netty Tutorial P ...

  5. 1049.(*) Counting Ones

    题意:题目大意:给出一个数字n,求1~n的所有数字里面出现1的个数 思路:转自(柳婼 の blog)遍历数字的低位到高位,设now为当前位的数字,left为now左边的所有数字构成的数字,right是 ...

  6. C++标准模板库(STL)介绍:set的基本用法

    1.元素的方向遍历 使用反向迭代器reverse_iterator可以反向遍历集合,输出集合元素的反向排序结果.它需要用到rbegin()和rend()两个方法,它们分别给出了反向遍历的开始位置和结束 ...

  7. vue-router总结

    之前写过一篇关于vue-router的文章,主要是介绍怎么结合cli2在项目中使用vue-router,比较的简单,今天想结合cli3来总结一下vue-router的具体用法. cli3 在介绍vue ...

  8. appium 元素文件 -查找元素 封装思路和方法

    方法1. try: target="//android.widget.TextView[@text='立即體驗']" element = WebDriverWait(dr,5,0. ...

  9. js 取一个对象的长度,取出来的是undefined,自己写的一个计算长度的函数解决了。

    收藏 牙膏儿 发表于 3年前 阅读 13085 收藏 7 点赞 1 评论 1 [粉丝福利]-<web 前端基础到实战系列课程>免费在线直播教学>>>   昨晚写一段代码, ...

  10. IDC:IDC 清单

    ylbtech-IDC:IDC 清单 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbte ...