Mybatis中輸入輸出映射與動態(tài)Sql圖文詳解
一、輸入映射
我們通過配置parameterType的值來指定輸入?yún)?shù)的類型,這些類型可以是簡單數(shù)據(jù)類型、POJO、HashMap等數(shù)據(jù)類型
1、簡單類型
2、POJO包裝類型
①這是單表查詢的時候傳入的POJO包裝類型,即可以直接傳入實體類,但是當多表查詢的時候,就需要自定義POJO類型
②我們使用自定義POJO類型來具體的了解一下
先設(shè)計 包裝類型如下,其中UserPOJO是除了User本身之外的添加的其他跟User相關(guān)的屬性的包裝類,UserVo是用于視圖層面的包裝類型,同樣也是作為Mapper配置文件的輸入類型
其中User文件同上一篇Mybatis簡單入門中的User,包括數(shù)據(jù)表部分也一樣。這里給出UserPoJO和UserVo文件
package cn.mybatis.po; public class UserPoJo extends User{ private User user; public void setUser(User user) { this.user = user; } public User getUser() { return user; } } UserPOJO
package cn.mybatis.po; public class UserVo { private UserPoJo userPoJo; public UserPoJo getUserPoJo() { return userPoJo; } public void setUserPoJo(UserPoJo userPoJo) { this.userPoJo = userPoJo; } } UserVo
然后我們配置UserMapper.xml文件
然后在UserMapper接口文件中添加
//測試包裝類型的查詢 public List<UserPoJo> findUserList(UserVo userVo) throws Exception;
使用Junit測試剛剛做的配置
@Test public void testFindUserList() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserPoJo userPoJo = new UserPoJo(); UserVo userVo = new UserVo(); userPoJo.setSex("男"); userPoJo.setUsername("u"); userVo.setUserPoJo(userPoJo); List<UserPoJo> userPoJoList = userMapper.findUserList(userVo); System.out.println(userPoJoList); }
最后結(jié)果如下
二、輸出映射
1、resultType
①在使用resultType進行映射的時候,只有查詢出來的列名和包裝類型中的屬性名一致的時候,才會映射成功
②當使用簡單類型作為輸出映射的時候,我們需要保證Sql查詢的結(jié)果只有一行一列,這樣就可以使用簡單類型
如下所示示例
SELECT COUNT(*) FROM t_user SELECT username FROM t_user WHERE id = 2
2、resultMap
查詢出來的列名和包裝類型的屬性名不一致的時候,可以使用resultMap來進行相應的映射(具體在使用中來說就是:定義resultMap中和屬性的映射關(guān)系,然后將輸出結(jié)果設(shè)置為resultMap的類型)
下面我們使用一個例子來進行具體的測試
①首先編寫mapper配置文件,其中需要加上resultMap的配置
<?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="cn.mybatis.mapper.UserMapper"> <!--定義resultMap type:resultMap最終映射的Java對象類型 id:對resultMap的標識 --> <resultMap id="userResultMap" type="user"> <!--id:標識查詢結(jié)果集中的唯一標識--> <id column="_id" property="id"></id> <!--result:標識查詢結(jié)果集中其他列的標識--> <result column="_username" property="username"></result> <result column="_password" property="password"></result> <result column="_sex" property="sex"></result> <result column="_address" property="address"></result> </resultMap> <select id="findUserById_resultMap" parameterType="int" resultMap="userResultMap"> SELECT id _id, username _username, PASSWORD _password, address _address, sex _sex FROM t_user WHERE id = #{id} </select> </mapper>
②然后在Mapper接口中添加方法
//測試resultMap public User findUserById_resultMap(int id) throws Exception;
③ 測試方法
@Test public void testFindUserById_resultMap() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserById_resultMap(2); System.out.println(user); }
④可以發(fā)現(xiàn),使用resultMap的方式跟直接查詢的結(jié)果是一致的
三、動態(tài)Sql
1、if判斷
我們在上面使用包裝類查詢的用例的時候,考慮到可能出現(xiàn)userPoJo會是null的情況,以及其相應的屬性也可能是null的情況,這樣的話,如果我們直接在Sql中進行拼接而不做判斷的話,可能會出現(xiàn)一些錯誤,所以我們使用if來進行動態(tài)的拼接。
<select id="findUserList" parameterType="cn.mybatis.po.UserVo" resultType="cn.mybatis.po.UserPoJo"> SELECT * FROM t_user <where> <if test="userPoJo != null"> <if test="userPoJo.sex != null and userPoJo.sex != ''"> AND sex = #{userPoJo.sex} </if> <if test="userPoJo.username != null and userPoJo.username != ''"> AND username LIKE '%${userPoJo.username}%' </if> </if> </where> </select>
2.Sql片段
上面的例子中,我們可以將if判斷抽取出來作為一個Sql片段,這樣做的好處是,可能再進行別的單表查詢User信息的時候可以重復使用這些Sql。
<!--定義Sql片段--> <sql id="query_user_info"> <if test="userPoJo != null"> <if test="userPoJo.sex != null and userPoJo.sex != ''"> AND sex = #{userPoJo.sex} </if> <if test="userPoJo.username != null and userPoJo.username != ''"> AND username LIKE '%${userPoJo.username}%' </if> </if> </sql>
然后在別的Sql中將上面的Sql片段引入拼接即可
<select id="findUserList" parameterType="cn.mybatis.po.UserVo" resultType="cn.mybatis.po.UserPoJo"> SELECT * FROM t_user <where> <include refid="query_user_info"></include> </where> </select>
3.foreach
當我們需要一種同樣的查詢方式只是參數(shù)不同的時候:SELECT * FROM t_user WHERE 1=1 AND (id = 1 OR id =2 OR id = 3),可以使用foreach來記性sql拼接
<sql id="query_ids"> <if test="ids != null"> <!-- SELECT * FROM t_user WHERE 1=1 AND (id = 1 OR id =2 OR id = 3) cilleation: 指定的是輸入?yún)?shù)集合的屬性名 item:每次遍歷的名稱 open:開始遍歷時拼接串 close:結(jié)束遍歷時候拼接的串 separator:遍歷的兩個對象中間需要拼接的串 --> <foreach collection="ids" item="item_id" open="AND (" close=")" separator=" OR "> id=#{item_id} </foreach> </if> </sql>
然后將上面的Sql片段加入響應的statment中
<select id="findUserByIds" parameterType="userVo" resultType="userPoJo"> SELECT * FROM t_user <where> <include refid="query_ids"></include> </where> </select>
測試結(jié)果如下
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)及算法實例:考拉茲猜想 Collatz Conjecture
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:考拉茲猜想 Collatz Conjecture,本文直接給出實現(xiàn)代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06Mybatis-Spring連接mysql 8.0配置步驟出錯的解決方法
這篇文章主要為大家詳細介紹了Mybatis-Spring連接mysql 8.0配置步驟出錯的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06SpringBoot項目如何設(shè)置權(quán)限攔截器和過濾器
這篇文章主要介紹了使用lombok時如何自定義get、set方法問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07java底層AQS實現(xiàn)類ReentrantLock鎖的構(gòu)成及源碼解析
本章我們就要來學習一下第一個?AQS?的實現(xiàn)類:ReentrantLock,看看其底層是如何組合?AQS?,實現(xiàn)了自己的那些功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03Jackson反序列化@JsonFormat 不生效的解決方案
這篇文章主要介紹了Jackson反序列化@JsonFormat 不生效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08MyBatis中的@SelectProvider注解源碼分析
這篇文章主要介紹了MyBatis中的@SelectProvider注解源碼分析,@SelectProvider功能就是用來單獨寫一個class類與方法,用來提供一些xml或者注解中不好寫的sql,今天就來說下這個注解的具體用法與源碼,需要的朋友可以參考下2024-01-01使用注解@Recover優(yōu)化丑陋的循環(huán)詳解
我們知道在實現(xiàn)一個功能的時候是可以使用不同的代碼來實現(xiàn)的,那么相應的不同實現(xiàn)方法的性能肯定也是有差別的,下面這篇文章主要給大家介紹了關(guān)于使用注解@Recover優(yōu)化丑陋的循環(huán)的相關(guān)資料,需要的朋友可以參考下2022-04-04