Mybatis学习5标签:if,where,sql,foreach
包装类: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的更多相关文章
- MyBatis学习 之 三、动态SQL语句
目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...
- MyBatis学习 之 四、动态SQL语句
有些时候,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息.使用Oracle的序列.mysq ...
- MyBatis学习(二)、SQL语句映射文件(1)resultMap
二.SQL语句映射文件(1)resultMap SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyB ...
- Mybatis学习(8)动态sql语句
Mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件判断) 2. ...
- mybatis学习笔记四(动态sql)
直接贴图,注解在代码上,其他的配置文件在学习一中就不贴了 1 数据库 2 实体类 package com.home.entity; /** * 此类是: 用户实体类 * @author hpc * @ ...
- mybatis学习(九)——动态sql
MyBatis 的强大特性之一便是它的动态 SQL.可以根据不同条件拼接 SQL 语句. 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似.主要由以下几种元素. if wh ...
- Mybatis学习总结三(动态SQL)
通过mybatis提供的各种标签方法实现动态拼接sql. 一.if 和 where <select id="findUserList" parameterType=" ...
- Mybatis映射文件标签(关于sql)
Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...
- MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存
二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...
随机推荐
- CI 数据库操作总结
最简单示例 $query = $this->db->query("YOUR QUERY"); foreach ($query->result() as $row) ...
- FB的破解与安装
1使用破解序列号安装 先找到host文件,一般可能是隐藏的windows/system32/drivers/etc在下面加入127.0.0.1 activate.adobe.com127.0.0.1 ...
- 阿里云ECS安装flannel启动问题
在阿里云ECS安装flannel,安装过程可以在网上找文章,这样的文章很多.我这里讲一下启动flannel遇到的两个问题的解决方法. 1,network.go:102] failed to retri ...
- jquery add()方法
<html><meta charset="utf-8"><head><script type="text/javascript& ...
- jqgrid使用(1)生成表格
1.引入js,css 2,基本配置 function init() { $("#list1").jqGrid({ url: "../Listing.ashx", ...
- ARCGIS 出图显示not map metafile into memory.Not enough memory
有许多的原因,比如系统有问题,磁盘空间不够,或虚拟内存设置不对等.再有就是输出地图时分辨率的设置是否太大等. not enough memory 这个问题是ESRI的一个所谓的“臭名昭著 ...
- SAS 报表输出一些新式控制
SAS 报表输出一些新式控制 *******************************:*Purpose: 报表*Programm: *Programmor: *Date: *Version: ...
- html 网页生产pdf文件
在nuget中安装组件 Install-Package CPechkin https://www.nuget.org/packages/CPechkin/ 根据html生产pdf文件 using Sy ...
- Linux 双网卡配置两个IP同时只有一个会通的原因
http://blog.csdn.net/centerpoint/article/details/38542719 根本原因: Linux默认启用了反向路由检查 如果2个网卡在一个Lan里面,那么服务 ...
- virt-install详解
man virt-install VIRT-INSTALL() Virtual Machine Manager VIRT-INSTALL() NAME virt-install - provision ...