包装类: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. HBase教程

    https://www.yiibai.com/hbase/ 自1970年以来,关系数据库用于数据存储和维护有关问题的解决方案.大数据的出现后,好多公司实现处理大数据并从中受益,并开始选择像 Hadoo ...

  2. windows defender和windows firewall

    Windows defender: Windows Defender,曾用名Microsoft Anti Spyware,是一个杀毒程序,可以运行在Windows XP和Windows Server ...

  3. 《亲测》nginx webscoket ssl conf配置示例

    非crt证书,用的pem,其中 http://localhost:5003 是你要转发到的站点网址 配置的就是 server { listen 80; server_name smarthome.yi ...

  4. 利用cbmakegen导出Code::blocks的Makefile

    首先到官网下载cbmakegen 官网地址:http://developer.berlios.de/projects/cbmakegen/ 也可通过我的百度网盘下载 [windows版本]地址:htt ...

  5. SEO优化之“不要轻易使用泛解析”

    原文地址:http://www.chinaz.com/web/2007/0505/8077.shtml 半夜三更的突然想起这个老想提出或者大家都知道的问题! 先续在这里,之后给予全面补充! 什么是泛解 ...

  6. Android接听、挂断电话

    新建一个名为ITelephony的aidl文件,注意包名不能改变,因为是通过反射方式来实现接听和挂断的

  7. zabbix 3.4新功能值解析——Preprocessing预处理

    Zabbix 3.4版本更新了许多新功能,其中一个监控项功能Preprocessing,根据官方说明文档,在监控项收集的数据存储到数据库前,预先对数据进行处理,使用效果超过预期.这个功能存放位置在创建 ...

  8. npm 淘宝镜像

    npm config set registry https://registry.npm.taobao.org

  9. SAS FORMAT 逻辑库存储 【输出格式 没有找到或无法加载】解决方法

    SAS FORMAT 逻辑库存储 [输出格式  没有找到或无法加载]解决方法:需要指定FORMAT 搜索的路径:OPTIONS FMTSEARCH=(F WORK); 以下为完整示例代码: 00@DA ...

  10. (转)C# WebApi 接口返回值不困惑:返回值类型详解

    原文地址:http://www.cnblogs.com/landeanfen/p/5501487.html 正文 前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi ...