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