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

原文地址:https://www.cnblogs.com/yiyezhiqiuwuchen/p/12788171.html