国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Mybatis中輸入輸出映射與動態Sql圖文詳解

Mybatis中輸入輸出映射與動態Sql圖文詳解

2021-07-15 10:29風沙迷了眼 Java教程

這篇文章主要給大家介紹了關于Mybatis中輸入輸出映射與動態Sql的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

一、輸入映射

我們通過配置parametertype的值來指定輸入參數的類型,這些類型可以是簡單數據類型、pojo、hashmap等數據類型

1、簡單類型

Mybatis中輸入輸出映射與動態Sql圖文詳解

2、pojo包裝類型

①這是單表查詢的時候傳入的pojo包裝類型,即可以直接傳入實體類,但是當多表查詢的時候,就需要自定義pojo類型

Mybatis中輸入輸出映射與動態Sql圖文詳解

②我們使用自定義pojo類型來具體的了解一下

先設計 包裝類型如下,其中userpojo是除了user本身之外的添加的其他跟user相關的屬性的包裝類,uservo是用于視圖層面的包裝類型,同樣也是作為mapper配置文件的輸入類型

Mybatis中輸入輸出映射與動態Sql圖文詳解

其中user文件同上一篇mybatis簡單入門中的user,包括數據表部分也一樣。這里給出userpojo和uservo文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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文件

Mybatis中輸入輸出映射與動態Sql圖文詳解

然后在usermapper接口文件中添加

?
1
2
//測試包裝類型的查詢
 public list<userpojo> finduserlist(uservo uservo) throws exception;

使用junit測試剛剛做的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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);
 }

最后結果如下

Mybatis中輸入輸出映射與動態Sql圖文詳解

二、輸出映射

1、resulttype

①在使用resulttype進行映射的時候,只有查詢出來的列名和包裝類型中的屬性名一致的時候,才會映射成功

②當使用簡單類型作為輸出映射的時候,我們需要保證sql查詢的結果只有一行一列,這樣就可以使用簡單類型

如下所示示例

?
1
2
3
select count(*) from t_user
 
select username from t_user where id = 2

2、resultmap  

查詢出來的列名和包裝類型的屬性名不一致的時候,可以使用resultmap來進行相應的映射(具體在使用中來說就是:定義resultmap中和屬性的映射關系,然后將輸出結果設置為resultmap的類型)  

下面我們使用一個例子來進行具體的測試

①首先編寫mapper配置文件,其中需要加上resultmap的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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:標識查詢結果集中的唯一標識-->
 <id column="_id" property="id"></id>
 <!--result:標識查詢結果集中其他列的標識-->
 <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接口中添加方法

?
1
2
//測試resultmap
public user finduserbyid_resultmap(int id) throws exception;

③ 測試方法

?
1
2
3
4
5
6
7
8
9
@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);
 }

④可以發現,使用resultmap的方式跟直接查詢的結果是一致的

Mybatis中輸入輸出映射與動態Sql圖文詳解

三、動態sql

1、if判斷

我們在上面使用包裝類查詢的用例的時候,考慮到可能出現userpojo會是null的情況,以及其相應的屬性也可能是null的情況,這樣的話,如果我們直接在sql中進行拼接而不做判斷的話,可能會出現一些錯誤,所以我們使用if來進行動態的拼接。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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>

Mybatis中輸入輸出映射與動態Sql圖文詳解

2.sql片段

上面的例子中,我們可以將if判斷抽取出來作為一個sql片段,這樣做的好處是,可能再進行別的單表查詢user信息的時候可以重復使用這些sql。

?
1
2
3
4
5
6
7
8
9
10
11
<!--定義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片段引入拼接即可

?
1
2
3
4
5
6
<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

當我們需要一種同樣的查詢方式只是參數不同的時候:select * from t_user where 1=1 and (id = 1 or id =2 or id = 3),可以使用foreach來記性sql拼接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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: 指定的是輸入參數集合的屬性名
  item:每次遍歷的名稱
  open:開始遍歷時拼接串
  close:結束遍歷時候拼接的串
  separator:遍歷的兩個對象中間需要拼接的串
  -->
  <foreach collection="ids" item="item_id" open="and (" close=")" separator=" or ">
  id=#{item_id}
  </foreach>
 </if>
 </sql>

然后將上面的sql片段加入響應的statment中

?
1
2
3
4
5
6
<select id="finduserbyids" parametertype="uservo" resulttype="userpojo">
select * from t_user
<where>
 <include refid="query_ids"></include>
</where>
</select>

測試結果如下

Mybatis中輸入輸出映射與動態Sql圖文詳解

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://www.cnblogs.com/fsmly/p/10335456.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品国产三级国产aⅴ中文 | 国精品一区 | 久久久夜色精品亚洲 | 国产成人精品a视频一区www | www日韩 | 九九热1| 九一麻豆精品 | 日韩一区二区三区视频 | 久久品 | 国产精品国产三级国产aⅴ中文 | 亚洲视频1区 | 一区二区精品在线 | 毛片在线播放网站 | 中文字幕在线精品 | 日韩激情一区二区三区 | 成人高清视频在线观看 | 日韩美女av在线 | 亚洲精品成人免费 | 日韩一区二 | 99久久久国产精品 | 成人精品一区二区 | 丝袜+亚洲+另类+欧美+变态 | 福利视频在线播放 | 久久波多野结衣 | 天堂va久久久噜噜噜久久va | 欧美天堂一区二区三区 | 精品欧美乱码久久久久久 | 亚洲免费看片 | 中文字幕视频在线 | 成人久久久久久 | 99在线视频播放 | 国产精品一区二区三区在线播放 | 久久男人天堂 | 欲色视频| 中国a毛片 | 国产成人精品一区二区三区网站观看 | 欧美午夜一区二区福利视频 | 婷婷天堂| 国产精品视屏 | 久草久| 国产一级二级毛片 |