接口的綁定方案和動態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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM