1. 接口绑定方案

MyBatis中, 提供了一套接口绑定方案. 程序员可以提供一个接口, 然后提供对应接口的一个mapper.xml文件. MyBatis会自动将接口和xml文件进行绑定. 实际上就是MyBatis会根据接口和对应的xml文件创建接口的实现类. 换言之, 就是可以得到接口类型的对象, 方便方法的调用.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

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>

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄