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的更多相关文章

  1. 黑马MyBatisday2 MyBatis Dao层实现 接口代理实现&传统实现 动态SQL和SQL抽取 自定义类型处理 分页插件PageHelper

    package com.itheima.mapper; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelp ...

  2. Oracle基础 动态SQL语句

    一.静态SQL和动态SQL的概念. 1.静态SQL 静态SQL是我们常用的使用SQL语句的方式,就是编写PL/SQL时,SQL语句已经编写好了.因为静态SQL是在编写程序时就确定了,我们只能使用SQL ...

  3. .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. ...

  4. 动态SQL详解

    动态SQL 在之前用户所编写的PL/SQL程序时有一个最大的特点:就是所操作的数据库对象(例如:表)必须存在,否则创建的子程序就会出问题,而这样的操作在开发之中被称为静态SQL操作,而动态SQL操作可 ...

  5. 本地动态SQL

    (转自:http://blog.itpub.net/26622598/viewspace-718134) 一.什么是动态SQL 大多数PL/SQL都做着一件特殊的结果可预知的工作.例如,一个存储过程可 ...

  6. Spring mybatis源码篇章-动态SQL基础语法以及原理

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-Mybatis的XML文件加载 前话 前文通过Spring中配置mapperLocations属性来进行对m ...

  7. mybatis 接口绑定 和 动态SQL

    一.MyBatis 接口绑定方案及多参数传递 1.作用:实现创建一个接口后把mapper.xml由mybatis生成接口的实现类,通过调用接口对象就可以获取mapper.xml中编写的sql 2.后面 ...

  8. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

  9. MyBatis学习(一)---配置文件,Mapper接口和动态SQL

    MyBatis MyBatis官方学习网站 http://www.mybatis.org/mybatis-3/zh/index.html 为什么需要MyBatis? Jdbc操作数据库的不足之处 1. ...

随机推荐

  1. parseInt和map方法使用案例分析

    ["1","2","3"].map(parseInt) //[1,NaN,NaN] 先看map()方法 定义和用法 map() 方法返回一个 ...

  2. 【shiro】(2)---基于RUL的权限管理

    基于RUL的权限管理 我想在写shiro权限管理认证前,先来一个基于URL实现的权限管理控制. 一.基于URI的权限业务逻辑  实现思路:       将系统操作的每个url配置在权限表中,将权限对应 ...

  3. HashMap source code view(1)

    前言 HashMap source code view 类注释 Hash table based implementation of the Map interface. This implement ...

  4. [NewLife.XCode]事务处理(算准你的每一分钱)

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netstandard,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示 ...

  5. Vc数据库编程基础MySql数据库的常见库命令.跟表操作命令

    Vc数据库编程基础MySql数据库的常见操作 一丶数据库常见的库操作 1.1查看全部数据库 命令:  show databases 1.2 创建数据库 命令: Create database 数据库名 ...

  6. (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)

    在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机 前提:服务器端安装桌面版的centos系统 CentOS Linux release 7.5.1804 (Core) ...

  7. JavaScript之函数(上)

    在编程语言中,无论是面向过程的C,兼备面过程和对象的c++,还是面向对象的编程语言,如java,.net,php等,函数均扮演着重要的角色.当然,在面向对象编程语言JavaScript中(严格来说,J ...

  8. 闪电侠 Netty 小册里的骚操作

    前言 即使这是一本小册,但基于"不提笔不读书"的理念,仍然有必要总结一下.此小册对于那些"硬杠 Netty 源码 却不曾在千万级生产环境上使用实操"的用户非常有 ...

  9. zepto 事件分析4(事件队列)

    前面分析了zepto的事件绑定,接下来分析事件解绑,先看一下zepto中解绑的off方法: $.fn.off = function(event, selector, callback){ var $t ...

  10. [Linux] PHP-FPM开启慢日志记录

    fpm:FastCGI Process Manager 是一种替代的PHP FastCGI实现,对于负载较重的站点非常有用. .先进的进程控制,优雅的停止启动 .能够使用不同的uid/gid/chro ...