接口的绑定方案和动态SQL
1. 接口绑定方案
MyBatis中, 提供了一套接口绑定方案. 程序员可以提供一个接口, 然后提供对应接口的一个mapper.xml文件. MyBatis会自动将接口和xml文件进行绑定. 实际上就是MyBatis会根据接口和对应的xml文件创建接口的实现类. 换言之, 就是可以得到接口类型的对象, 方便方法的调用.
2.1 实现方式
2.1.1 定义接口
|
package com.bjsxt.mapper; import java.util.List; import com.bjsxt.pojo.User;
public interface UserMapper { List<User> selAll(); } |
2.1.2 编写对应接口的映射文件
注意:
a) xml文件名要和接口名一致
b) namespace属性必须为接口的全限定路径
c) id属性必须和接口对应的方法名一致
|
<mapper namespace="com.bjsxt.mapper.UserMapper"> <select id="selAll" resultType="User"> select * from t_user </select> </mapper> |
2.1.3 在核心配置文件中扫描接口
a) 扫描单个接口, 可以使用mapper标签的class属性
|
<mappers> <mapper class="com.bjsxt.mapper.UserMapper" /> </mappers> |
b) 当扫描多个接口时, 为简化配置, 可以使用package标签, 表示扫描对应包下的所有接口.
|
<mappers> <package name="com.bjsxt.mapper" /> </mappers> |
2.1.4 应用
在使用时, 可以通过SqlSession对象的getMapper方法, 得到接口的代理对象, 从而可以调用定义好的方法.
|
@Test public void testBind() { SqlSession session = MyBatisUtil.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> list = mapper.selAll(); for (User user : list) { System.out.println(user); } session.close(); } |
2.2 通过接口绑定解决多参数的传递
2.2.1 方式一
a) 接口中定义方法
|
User selByUP(String username, String password); |
b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{index}或#{param+数字}的方式.
|
<select id="selByUP" resultType="user"> select * from t_user where username=#{0} and password=#{1} </select> |
2.2.2 方式二
a) 接口中定义方法, 参数中使用@Param注解设定参数名用于在SQL语句中使用.
|
User selByUP(@Param("username") String username, @Param("password") String password); |
b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{参数名称}或#{param+数字}的方式.
|
<select id="selByUP" resultType="user"> select * from t_user where username=#{username} and password=#{password} </select> |
2. 动态SQL
根据条件的不同, SQL语句也会随之动态的改变. MyBatis中, 提供了一组标签用于实现动态SQL.
3.1 <if>
用于进行条件判断, test属性用于指定判断条件. 为了拼接条件, 在SQL语句后强行添加1=1的恒成立条件.
|
<select id="sel" resultType="user"> select * from t_user where 1=1 <if test="username != null and username != ''"> and username=#{username} </if> <if test="password != null and password != ''"> and password=#{password} </if> </select> |
3.2 <where>
用于管理where子句. 有如下功能:
a) 如果没有条件, 不会生成where关键字
b) 如果有条件, 会自动添加where关键字
c) 如果第一个条件中有and, 去除之
|
<select id="sel" resultType="user"> select * from t_user <where> <if test="username != null and username != ''"> and username=#{username} </if> <if test="password != null and password != ''"> and password=#{password} </if> </where> </select> |
3.3 <choose><when><otherwise>
这是一套标签, 功能类似于switch...case...
|
<select id="sel" resultType="user"> select * from t_user <where> <choose> <when test="username != null and username != ''"> and username = #{username} </when> <when test="password != null and password != ''"> and password = #{password} </when> <otherwise> and 1=1 </otherwise> </choose> </where> </select> |
3.4 <set>
用于维护update语句中的set子句. 功能如下:
a) 满足条件时, 会自动添加set关键字
b) 会去除set子句中多余的逗号
c) 不满足条件时, 不会生成set关键字
|
int updUser(User user); |
|
<update id="updUser" parameterType="user"> update t_user <set> id=#{id}, <!-- 防止所有条件不成立时的语法错误 --> <if test="username != null and username != ''"> username=#{username}, </if> <if test="password != null and password != ''"> password=#{password}, </if> </set> where id=#{id} </update> |
3.5 <trim>
用于在前后添加或删除一些内容
a) prefix, 在前面添加内容
b) prefixOverrides, 从前面去除内容
c) suffix, 向后面添加内容
d) suffixOverrides, 从后面去除内容
|
<update id="updUser" parameterType="user"> update t_user <!-- prefix: 前缀, 表示向前面添加内容 prefixOverrides: 从前面删除内容 suffix: 后缀, 表示向后面添加内容 suffixOverrides: 从后面删除内容 --> <trim prefix="set" prefixOverrides="user" suffix="hahaha" suffixOverrides=","> username=#{username}, </trim> where id=#{id} </update> |
3.6 <bind>
用于对数据进行再加工, 用于模糊查询
|
<select id="sel" resultType="user"> select * from t_user <where> <if test="username!=null and username!=''"> <bind name="username" value="'%' + username + '%'"/> and username like #{username} </if> </where> </select> |
1. 接口绑定方案
MyBatis中, 提供了一套接口绑定方案. 程序员可以提供一个接口, 然后提供对应接口的一个mapper.xml文件. MyBatis会自动将接口和xml文件进行绑定. 实际上就是MyBatis会根据接口和对应的xml文件创建接口的实现类. 换言之, 就是可以得到接口类型的对象, 方便方法的调用.
2.1 实现方式
2.1.1 定义接口
|
package com.bjsxt.mapper; import java.util.List; import com.bjsxt.pojo.User;
public interface UserMapper { List<User> selAll(); } |
2.1.2 编写对应接口的映射文件
注意:
a) xml文件名要和接口名一致
b) namespace属性必须为接口的全限定路径
c) id属性必须和接口对应的方法名一致
|
<mapper namespace="com.bjsxt.mapper.UserMapper"> <select id="selAll" resultType="User"> select * from t_user </select> </mapper> |
2.1.3 在核心配置文件中扫描接口
a) 扫描单个接口, 可以使用mapper标签的class属性
|
<mappers> <mapper class="com.bjsxt.mapper.UserMapper" /> </mappers> |
b) 当扫描多个接口时, 为简化配置, 可以使用package标签, 表示扫描对应包下的所有接口.
|
<mappers> <package name="com.bjsxt.mapper" /> </mappers> |
2.1.4 应用
在使用时, 可以通过SqlSession对象的getMapper方法, 得到接口的代理对象, 从而可以调用定义好的方法.
|
@Test public void testBind() { SqlSession session = MyBatisUtil.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> list = mapper.selAll(); for (User user : list) { System.out.println(user); } session.close(); } |
2.2 通过接口绑定解决多参数的传递
2.2.1 方式一
a) 接口中定义方法
|
User selByUP(String username, String password); |
b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{index}或#{param+数字}的方式.
|
<select id="selByUP" resultType="user"> select * from t_user where username=#{0} and password=#{1} </select> |
2.2.2 方式二
a) 接口中定义方法, 参数中使用@Param注解设定参数名用于在SQL语句中使用.
|
User selByUP(@Param("username") String username, @Param("password") String password); |
b) 映射文件中提供对应的标签. 此时, SQL语句中获取方式有两种, 通过#{参数名称}或#{param+数字}的方式.
|
<select id="selByUP" resultType="user"> select * from t_user where username=#{username} and password=#{password} </select> |
2. 动态SQL
根据条件的不同, SQL语句也会随之动态的改变. MyBatis中, 提供了一组标签用于实现动态SQL.
3.1 <if>
用于进行条件判断, test属性用于指定判断条件. 为了拼接条件, 在SQL语句后强行添加1=1的恒成立条件.
|
<select id="sel" resultType="user"> select * from t_user where 1=1 <if test="username != null and username != ''"> and username=#{username} </if> <if test="password != null and password != ''"> and password=#{password} </if> </select> |
3.2 <where>
用于管理where子句. 有如下功能:
a) 如果没有条件, 不会生成where关键字
b) 如果有条件, 会自动添加where关键字
c) 如果第一个条件中有and, 去除之
|
<select id="sel" resultType="user"> select * from t_user <where> <if test="username != null and username != ''"> and username=#{username} </if> <if test="password != null and password != ''"> and password=#{password} </if> </where> </select> |
3.3 <choose><when><otherwise>
这是一套标签, 功能类似于switch...case...
|
<select id="sel" resultType="user"> select * from t_user <where> <choose> <when test="username != null and username != ''"> and username = #{username} </when> <when test="password != null and password != ''"> and password = #{password} </when> <otherwise> and 1=1 </otherwise> </choose> </where> </select> |
3.4 <set>
用于维护update语句中的set子句. 功能如下:
a) 满足条件时, 会自动添加set关键字
b) 会去除set子句中多余的逗号
c) 不满足条件时, 不会生成set关键字
|
int updUser(User user); |
|
<update id="updUser" parameterType="user"> update t_user <set> id=#{id}, <!-- 防止所有条件不成立时的语法错误 --> <if test="username != null and username != ''"> username=#{username}, </if> <if test="password != null and password != ''"> password=#{password}, </if> </set> where id=#{id} </update> |
3.5 <trim>
用于在前后添加或删除一些内容
a) prefix, 在前面添加内容
b) prefixOverrides, 从前面去除内容
c) suffix, 向后面添加内容
d) suffixOverrides, 从后面去除内容
|
<update id="updUser" parameterType="user"> update t_user <!-- prefix: 前缀, 表示向前面添加内容 prefixOverrides: 从前面删除内容 suffix: 后缀, 表示向后面添加内容 suffixOverrides: 从后面删除内容 --> <trim prefix="set" prefixOverrides="user" suffix="hahaha" suffixOverrides=","> username=#{username}, </trim> where id=#{id} </update> |
3.6 <bind>
用于对数据进行再加工, 用于模糊查询
|
<select id="sel" resultType="user"> select * from t_user <where> <if test="username!=null and username!=''"> <bind name="username" value="'%' + username + '%'"/> and username like #{username} </if> </where> </select> |
接口的绑定方案和动态SQL的更多相关文章
- 黑马MyBatisday2 MyBatis Dao层实现 接口代理实现&传统实现 动态SQL和SQL抽取 自定义类型处理 分页插件PageHelper
package com.itheima.mapper; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelp ...
- Oracle基础 动态SQL语句
一.静态SQL和动态SQL的概念. 1.静态SQL 静态SQL是我们常用的使用SQL语句的方式,就是编写PL/SQL时,SQL语句已经编写好了.因为静态SQL是在编写程序时就确定了,我们只能使用SQL ...
- .Net程序员学用Oracle系列(28):PLSQL 之SQL分类和动态SQL
1.SQL 语句分类 1.1.分类方法及类型 1.2.数据定义语言 1.3.数据操纵语言 1.4.其它语句 2.动态 SQL 理论 2.1.动态 SQL 的用途 2.2.动态 SQL 的语法 2.3. ...
- 动态SQL详解
动态SQL 在之前用户所编写的PL/SQL程序时有一个最大的特点:就是所操作的数据库对象(例如:表)必须存在,否则创建的子程序就会出问题,而这样的操作在开发之中被称为静态SQL操作,而动态SQL操作可 ...
- 本地动态SQL
(转自:http://blog.itpub.net/26622598/viewspace-718134) 一.什么是动态SQL 大多数PL/SQL都做着一件特殊的结果可预知的工作.例如,一个存储过程可 ...
- Spring mybatis源码篇章-动态SQL基础语法以及原理
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis的XML文件加载 前话 前文通过Spring中配置mapperLocations属性来进行对m ...
- mybatis 接口绑定 和 动态SQL
一.MyBatis 接口绑定方案及多参数传递 1.作用:实现创建一个接口后把mapper.xml由mybatis生成接口的实现类,通过调用接口对象就可以获取mapper.xml中编写的sql 2.后面 ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
- MyBatis学习(一)---配置文件,Mapper接口和动态SQL
MyBatis MyBatis官方学习网站 http://www.mybatis.org/mybatis-3/zh/index.html 为什么需要MyBatis? Jdbc操作数据库的不足之处 1. ...
随机推荐
- asp.net MVC 5 路由 Routing
ASP.NET MVC ,一个适用于WEB应用程序的经典模型 model-view-controller 模式.相对于web forms一个单一的整块,asp.net mvc是由连接在一起的各种代码层 ...
- VSCode typescript ctrl+shift+b can't be compiled error:TS5007
环境: vscode:1.12.2 node 7.4.0 TypeScript:2.3.2 从svn 更新下来,别的电脑环境编译是没问题的,在我的电脑上编译失败并出现以下错误 error TS5007 ...
- 【jQuery】(2)---Jquery过滤选择器
1.基础选择器: 名称 说明 举例 #id 根据元素Id选择 $("divId") 选择ID为divId的元素 element 根据元素的 ...
- 函数式编程之-模式匹配(Pattern matching)
模式匹配在F#是非常普遍的,用来对某个值进行分支匹配或流程控制. 模式匹配的基本用法 模式匹配通过match...with表达式来完成,一个完整的模式表达式长下面的样子: match [somethi ...
- 野路子Java开发的一篇随笔
园子的朋友们,一年半的时间大家过的还好吧? 流水它带走光阴的故事改变了我们,再次的见面我们又历经了多少的路程,落花流水,冷暖自知,这一年半,关于工作上的关键词只有两个:加班(996弱多了) ...
- eclipse 在jboss的debug配置(ubuntu系统)
转自:https://blog.csdn.net/iteye_3878/article/details/81695877 由于我在ubuntu下权限设置分开,如 /home/jboss/ (jboss ...
- gops —— Go 程序诊断分析工具
GitHub: https://github.com/google/gops 一个用于列出和诊断分析系统中正在运行的 Go 程序的命令行工具 安装 go get -u github.com/googl ...
- Git 撤销所有未提交(Commit)的内容
撸了好多代码,但是突然设计改了(~~o(>_<)o ~~):或者引入个第三方库,后来又发现用不着,想删掉,但文件太多了(比如几百个):那,怎么办呢,都不想了...Git 比人聪明,所以能很 ...
- 最近公共祖先(least common ancestors,LCA)
摘要: 本文主要介绍了解决LCA(最近公共祖先问题)的两种算法,分别是离线Tarjan算法和在线算法,着重展示了在具体题目中的应用细节. 最近公共祖先是指对于一棵有根树T的两个结点u和v,它们的LCA ...
- Java 8 ThreadLocal 源码解析
Java 中的 ThreadLocal是线程内的局部变量, 它为每个线程保存变量的一个副本.ThreadLocal 对象可以在多个线程中共享, 但每个线程只能读写其中自己的副本. 目录: 代码示例 源 ...