mybatis中xml文件標簽,屬性總結使用,遇到新的加進來。


一、parameterType和parameterMap     resultType和resultMap

* mybatis xml文件 中有個parameterType和parameterMap
* 1.parameterType
* 使用過:
* 1.1基本數據類型和引用類型(但是這里解釋,比如dao層中add(String name) 必須在dao層要用add(@Param("name")String name),只有用了@Param才能映射到。

  當你dao層傳入的是不同類型,比如String和Integer   --》public List<SysMenu> loginViewMenu(@Param("userId")String userId,@Param("delSign")Integer delSign);

  在xml文件中不用寫parameterType  ,直接賦值即可。<select id="loginViewMenu"  resultMap="loginMenu">
* 1.2對象類型
* 1.3java.util.HashMap
* 1.4java.util.List dao層用List<Map>,List<對象> 都可以。
*
* 2.parameterMap (目前沒用過,廢棄)
*
*
*
*
*
* mybatis的xml文件返回值類型有resultType和resultMap
* 1.resultType
* 1.1 返回基本數據類型和String類型
* 1.2返回一個對象類型(本人目前用的最多,字段可以用vo實體類來接,這樣就可以映射到不同表字段)
*
* 2.resultMap(此時需要去用起來,感覺映射很強大,很重要,減少多次去連接數據庫,直接sql語句搞定返回映射,例如一對一,一對多,返回一些無限東西,比如后台菜單有層級,樹狀結構,用這個簡單)
*
* 2.1 當返回一個對象類型
* 2.2當返回一個實體類里包含一個實體屬性(association標簽)
* 2.3當返回一個實體類里包含一個集合屬性比如list (collection標志)
*

例如這里返回,某個用戶所擁有角色,角色所擁有權限

<mapper namespace="com.lyh.beacon.dao.SysUserRoleDao">
<!-- 1.這里多去理解,這個jdbcType必須都大寫,不然報錯
2. Collection 用ofType 集合
3. assciation 用javaType 對象
4. column 數據庫列參數 property 映射的實體類參數
5. id標簽是唯一列 result標簽其他列
-->
<resultMap type="com.lyh.beacon.model.sys.SysMenu" id="loginMenu">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column = "name" property="name" jdbcType="VARCHAR"/>
<result column = "pid" property="pid" jdbcType="INTEGER"/>
<result column = "code" property="code" jdbcType="VARCHAR"/>
<result column ="levels" property="levels" jdbcType="INTEGER"/>
<result column = "front_url" property = "frontUrl" jdbcType="VARCHAR"/>
<result column = "back_url" property="backUrl" jdbcType="VARCHAR"/>

<collection property="listMenu" ofType="com.lyh.beacon.model.sys.SysMenu" >
<id column="id2" property="id" jdbcType="INTEGER"/>
<result column = "name2" property="name" jdbcType="VARCHAR"/>
<result column = "pid2" property="pid" jdbcType="INTEGER"/>
<result column = "code2" property="code" jdbcType="VARCHAR"/>
<result column ="levels2" property="levels" jdbcType="INTEGER"/>
<result column = "front_url2" property = "frontUrl" jdbcType="VARCHAR"/>
<result column = "back_url2" property="backUrl" jdbcType="VARCHAR"/>
</collection>

</resultMap>

<!-- 這個可以做成登陸時返回相應模塊給前端去展示 ,利用resultMap很強大,那種遞歸,都可以用resultMap來做,好的很-->
<select id="loginViewMenu" parameterType="java.lang.String" resultMap="loginMenu">
select m1.*,m2.id id2,m2.name name2,m2.pid pid2,m2.code code2,m2.levels levels2,m2.front_url frontUrl2,m2.back_url backUrl2
from sys_menu m1,sys_menu m2,sys_user_role ur ,sys_role_menu rm
where m1.id = m2.pid and ur.user_id = #{userId} and ur.role_id = rm.role_id and rm.menu_id = m2.id
</select>

 

二、insert 標簽的時候,內部使用 <trim prefix= "" suffix = "" prefixOverrides="" suffixOverrides=""> </trim>

(在update,delete,select中應該也可用)

prefix :在trim標簽內的sql語句加上前綴。

suffix:在trim標簽內的sql語句加上后綴。

prefixOverrides:在trim標簽內的sql語句去掉多余的前綴內容。

suffixOverrides:在trim標簽內的sql語句去掉多余的后綴內容。  如  suffixOverrides=","  去掉英文逗號

這里插入一條數據說明

如下:如果沒有suffixOverrides=","

則insert into employee(id,name,gender,) values(1,小何,男,)  這樣肯定錯誤的,最后參數后有英文逗號

故加上了suffixOverrides后 insert into employee(id,name,gender) values(1,小何,男)  正確

其他類似 

insert into employee()

insert into employee
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="gender != null">
gender,
</if>
</trim>

<trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="gender != null">
#{gender,jdbcType=CHAR},
</if>

</trim>

三、<sql></sql>標簽 ,<include refid = /> 標簽, <if test =></if> 標簽 ,<where>其他的條件</where> ,<foreach></foreach>批量標簽
<set></set>標簽 <choose><when></when>...<otherWise></otherWise></choose>標簽

學習:https://blog.csdn.net/m0_38054145/article/details/81906343


免責聲明!

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



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