Mybatis动态SQL配置
使用 if where foreach标签对映射配置文件中sql语句进行动态配置
1、首先在dao接口中设置两个查询方法
package sun.dao; import sun.domain.QueryObj;
import sun.domain.User; import java.util.List; public interface UserDao { /**
* 根据已有条件进行查询(if where)
*/
List<User> findUserByCondition(User user); /**
* 根据集合中的id进行查询(if where foreach)
*/
List<User> findUserByList(QueryObj qobj);
}
2、配置映射配置文件(使用if where foreach三种标签)
<?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"> <mapper namespace="sun.dao.UserDao">
<resultMap id="userMap" type="sun.domain.User">
<!--主键字段对应-->
<id property="user_id" column="id"></id>
<!--非主键关系对应-->
<result property="user_name" column="username"></result>
<result property="user_birthday" column="birthday"></result>
<result property="user_address" column="address"></result>
<result property="user_sex" column="sex"></result>
</resultMap> <!--公共sql语句抽取-->
<sql id="defaultSelect">
select * from user
</sql> <!--根据条件进行查询-->
<select id="findUserByCondition" resultMap="userMap" parameterType="user">
<include refid="defaultSelect"></include>
<where>
<if test="user_name!=null">
and username=#{user_name}
</if>
<if test="user_sex!=null">
and sex=#{user_sex}
</if>
</where>
</select>
<!--根据id列表进行查询-->
<select id="findUserByList" parameterType="queryobj" resultMap="userMap">
<include refid="defaultSelect"></include>
<where>
<if test="ids!=null and ids.size()>0">
<foreach collection="ids" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
</mapper>
在映射配置文件中可以使用sql标签对常用的sql语句进行抽取,在操作标签内如果需要使用该sql语句可使用include标签进行导入即可。
(注意:使用sql语句进行抽取时,sql语句后面不要添加分号,否则会导致在操作标签内引用后拼接sql字符串时造成错误)
如果在该段代码中有关于parameterType未使用全限定类名的疑惑请参考Mybatis项目构建和CURD操作博客最下面的properties标签和typeAliases标签的使用
3、测试类中进行测试
package sun.test; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sun.dao.UserDao;
import sun.domain.QueryObj;
import sun.domain.User; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; public class MybatisTest { private InputStream in;
private SqlSession sqlSession;
private UserDao userDao; @Before
public void init() throws IOException {
// 读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建SqlSessionFactory
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
// 使用工厂生产sqlsession对象
sqlSession = factory.openSession();
// 使用sqlsession创建UserDao接口代理对象
userDao = sqlSession.getMapper(UserDao.class);
} @After
public void destory() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
} @Test
public void conditionTest(){
User user = new User();
user.setUser_name("kelvin");
user.setUser_sex("女");
List<User> users = userDao.findUserByCondition(user);
for (User user1 : users) {
System.out.println(user1);
}
} @Test
public void ids(){
QueryObj queryObj = new QueryObj();
ArrayList<Integer> ids = new ArrayList<Integer>();
ids.add(51);
ids.add(52);
ids.add(53);
queryObj.setIds(ids);
List<User> users = userDao.findUserByList(queryObj);
for (User user : users) {
System.out.println(user);
} }
}
测试结果如下:


Mybatis动态SQL配置的更多相关文章
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- 自己动手实现mybatis动态sql
发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...
- mybatis原理分析学习记录,mybatis动态sql学习记录
以下个人学习笔记,仅供参考,欢迎指正. MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转 ...
- mybatis动态sql中的两个内置参数(_parameter和_databaseId)
mybatis动态sql中的两个内置参数(_parameter和_databaseId) <!-- mybatis动态sql的两个内置参数 不只是方法传递过来的参数可以被 ...
- mybatis动态sql和分页
mybatis动态sql foreach BookMapper.xml <select id="selectBooksIn" resultType="com.lin ...
- Mybatis动态sql及分页、特殊符号
目的: mybatis动态sql(案例:万能查询) 查询返回结果集的处理 mybatis的分页运用 mybatis的特殊符号 mybatis动态sql(案例:万能查询) 根据id查询 模糊查询 (参数 ...
- MyBatis从入门到精通(第4章):MyBatis动态SQL【foreach、bind、OGNL用法】
(第4章):MyBatis动态SQL[foreach.bind.OGNL用法] 4.4 foreach 用法 SQL 语句中有时会使用 IN 关键字,例如 id in (1,2,3).可以使用 ${i ...
- 【MyBatis】MyBatis 动态 SQL
MyBatis 动态SQL if 可以根据实体类的不同取值,使用不同的 SQL 语句来进行查询. 使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分. 持久层 DAO 接口: pub ...
- mybatis动态sql以及分页
1.mybatis动态sql 2.模糊查询 3.查询返回结果集的处理 4.分页查询 5.特殊字符处理 1.mybatis动态sql If.trim.foreach If 标签判断某一字段是否为空 &l ...
随机推荐
- C#算法设计排序篇之01-冒泡排序(附带动画演示程序)
冒泡排序(Bubble Sort) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/672 访问. 它重复地访问要排序的元 ...
- Vue 页面导出成PDF文件
注意事项 如果导出的页面中设计到图片或者其他文件跨域文件,需要后端服务配合 安装依赖 npm install html2Canvas --save npm install jspdf--save 封装 ...
- K8s集群verification error" while trying to verify candidate authority certificate "kubernetes"
问题内容 because of "crypto/rsa: verification error" while trying to verify candidate authorit ...
- LeetCode 646 最长数对链详解
题目描述 给出 n 个数对. 在每一个数对中,第一个数字总是比第二个数字小. 现在,我们定义一种跟随关系,当且仅当 b < c 时,数对(c, d) 才可以跟在 (a, b) 后面.我们用这种形 ...
- 获取客户端用户真实ip方法整理(jekyll迁移)
layout: post title: 获取客户端用户真实ip方法整理 date: 2019-08-22 author: xiepl1997 tags: springboot 由请求获取客户端ip地址 ...
- Python基础入门知识点——深浅拷贝
深浅拷贝 对象引用.浅拷贝.深拷贝(拓展.难点.重点) Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果 其实这个是由于共享内存导致的结果 拷贝 ...
- 多线程系列(二)之Thread类
在上一遍文章中讲到多线程基础,在此篇文章中我们来学习C#里面Thread类.Thread类是在.net framework1.0版本中推出的API.如果对线程的概念还不太清楚 的小伙伴请阅读我的上一遍 ...
- centos iso镜像自动挂载
vim /etc/fstab 添加如下行:/usr/ison/centos.iso /media/cdrom iso9660 defaults,loop 0 0
- 仿京东BOE官网 jQuery代码
$(function() { $("#chanping").mouseenter(function() { $("#column").slideDown(500 ...
- 【转】Camera 简介
一.摄像头(CAMERA)又称为电脑相机.电脑眼等,它作为一种视频输入设备,在过去被广泛的运用于视频会议.远程医疗及实时监控等方面. 近年以来,随着互联网技术的发展,网络速度的不断提高,再加上感光成像 ...