Mybatis之XML映射文件标签解析

1. resultMap

         我们可以使用resultMap标签自定义结果集和实体类属性的映射规则。可以理解为将数据库表的字段名表对应的实体类的属性名映射起来。

属性:

id:唯一标识

type:用来指定映射到哪个类

extends:继承其他map的映射规则

<id>标签:用来指定主键列的映射规则 property是属性名 column是对应列名

<result>标签:用来指定普通列的映射规则

<association>标签:一个复杂类型的关联,许多结果将包装成这种类型,这里关联了SysDept

<collection>标签:一个复杂类型的集合

    <resultMap type="SysUser" id="SysUserResult">
        <id     property="userId"       column="user_id"      />
        <result property="deptId"       column="dept_id"      />
        <result property="userName"     column="user_name"    />
        <result property="nickName"     column="nick_name"    />
        <result property="email"        column="email"        />
        <result property="phonenumber"  column="phonenumber"  />
        <result property="sex"          column="sex"          />
        <association property="dept"    column="dept_id" javaType="SysDept" resultMap="deptResult" />
        <collection  property="roles"   javaType="java.util.List"           resultMap="RoleResult" />
    </resultMap>

使用SysUserResult:

	<select id="checkUserNameUnique" parameterType="String" resultMap="SysUserResult">
		select user_id, user_name from sys_user where user_name = #{userName} and del_flag = '0' limit 1
	</select>

2. sql 

        用来定义可以重用的sql代码,以便在其他语句中使用,简单实例:

	<sql id="selectUser">
        select user_id, user_name
        from sys_user 
    </sql>

使用:

	<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
	    <include refid="selectUserVo"/>
		where u.user_name = #{userName} and u.del_flag = '0'
	</select>

3. select 

Mybatis中的查询语句,最为常用,简单的查询如下,接受的参数是int类型,返回的对象类型是 hashMap类型。,其中键是列名,值是结果行中返回的值。

属性:

parameterType:传入参数的全限定名或别名。可选,mybatis可以自动推断。

resultType:返回结果的类全限定名或别名,如果返回的是集合,应该设置为集合中元素的类型。

resultMap:对定义的 resultMap 的引用。

<select id="selectPerson" parameterType="int" resultType="hashmap">
  SELECT * FROM PERSON WHERE ID = #{id}
</select>

4. insert 

插入标签,属性:

useGeneratedKeys:(仅适用于 insert 和 update)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系型数据库管理系统的自动递增字段),默认值:false。

keyProperty:(仅适用于 insert 和 update)指定能够唯一识别对象的属性,MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值,默认值:未设置(unset)。如果生成列不止一个,可以用逗号分隔多个属性名称。

	<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
 		insert into sys_user(
 			<if test="userId != null and userId != 0">user_id,</if>
 			<if test="deptId != null and deptId != 0">dept_id,</if>
 			<if test="userName != null and userName != ''">user_name,</if>
 			create_time
 		)values(
 			<if test="userId != null and userId != ''">#{userId},</if>
 			<if test="deptId != null and deptId != ''">#{deptId},</if>
 			<if test="userName != null and userName != ''">#{userName},</if>
 			sysdate()
 		)
	</insert>

5. update

	<update id="updateUser" parameterType="SysUser">
 		update sys_user
 		<set>
 			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
 			<if test="userName != null and userName != ''">user_name = #{userName},</if>
 			update_time = sysdate()
 		</set>
 		where user_id = #{userId}
	</update>

6. delete

	<delete id="deleteUserById" parameterType="Long">
 		update sys_user set del_flag = '2' where user_id = #{userId}
 	</delete>

7. 动态SQL

(1)if 标签

可以使用if标签进行条件判断,条件成立才会把if标签中的内容拼接进sql语句中。

    <select id="findByCondition" resultType="com.sangeng.pojo.User">
         select * from user where  id = #{id}
        <if test="username!=null">
           and username = #{username}
        </if>
    </select>

如果参数username为null则执行的sql为:select  from user where id = ?

如果参数username不为null则执行的sql为:select  from user where id = ? and username = ?

(2)trim 标签

可以使用该标签动态的添加前缀或后缀,也可以使用该标签动态的消除前缀。

1)prefixOverrides 属性

用来设置需要被清除的前缀,多个值可以用  |  分隔,注意  |  前后不要有空格。例如: and|or

    <select id="findByCondition" resultType="com.zy.pojo.User">
        select * from user
        <trim prefixOverrides="and|or" >
            and
        </trim>
    </select>

最终执行的sql为: select * from user

2)suffixOverrides 属性

用来设置需要被清除的后缀,多个值可以用  |  分隔,注意  |  前后不要有空格。例如: and|or

    <select id="findByCondition" resultType="com.zy.pojo.User">
        select * from user
        <trim suffixOverrides="like|and" >
            where 1=1 like
        </trim>
    </select>

最终执行的sql为: select * from user 去掉了后缀like

3)prefix 属性

用来设置动态添加的前缀,如果标签中有内容就会添加上设置的前缀,这里标签里面有内容,就会在这个标签前面加上一个  where 

    <select id="findByCondition" resultType="com.zy.pojo.User">
        select * from user
        <trim prefix="where" >
           1=1
        </trim>
    </select>

最终执行的sql为:select * from user where 1=1 动态增加了前缀where

4)suffix 属性

用来设置动态添加的后缀,如果标签中有内容就会添加上设置的后缀,这里标签里面有内容 where ,所以会在 select * from user where这个语句后面加上 1=1

    <select id="findByCondition" resultType="com.zy.pojo.User">
        select * from user
        <trim suffix="1=1" >
           where
        </trim>
    </select>

(3)where 标签

where标签等价于:

<trim prefix="where" prefixOverrides="and|or" >

</trim>

如果标签里面有内容,就会在前面加上 where ,并且会清除最后的 and 或者 or 后缀。

(4)set 标签

set 标签等价于:常用于更新操作。

<trim prefix="set" suffixOverrides="," >

</trim>

如果标签里面有内容,就在前面加上set, 并且清除最后的逗号 ,

实例:

	<update id="updateUser" parameterType="SysUser">
 		update sys_user
 		<set>
 			<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
 			<if test="userName != null and userName != ''">user_name = #{userName},</if>
 			update_time = sysdate(),
 		</set>
 		where user_id = #{userId}
	</update>

等价于下面: 

<trim prefix="set" suffixOverrides="," >
 	<if test="deptId != null and deptId != 0">dept_id = #{deptId},</if>
 	<if test="userName != null and userName != ''">user_name = #{userName},</if>
 	update_time = sysdate(),
</trim>

(5)foreach 标签

可以使用foreach标签遍历集合或者数组类型的参数,获取其中的元素拿来动态的拼接SQL语句。常用于批量操作数据库,如批量插入,批量删除。

collection:表示要遍历的参数。

open:表示遍历开始时拼接的语句

item:表示给当前遍历到的元素的取的名字

separator:表示每遍历完一次拼接的分隔符

close:表示最后一次遍历完拼接的语句

实例:

list 列表装有 5 个 ExamPaperItem 对象。将paperId questionId 批量插入到数据表中。

Mapper

int batchPaperQuestion(List<ExamPaperItem> list);

 xml

    <!--批量插入-->
    <insert id="batchPaperQuestion">
        insert into exam_paper_item(paper_id, question_id) values
        <foreach item="item" collection="list" separator=",">
            (#{item.paperId},#{item.questionId})
        </foreach>
    </insert>

 实际的执行的 sql 语句:

insert into exam_paper_item(paper_id, question_id) values (121,64),  (121,65), (121,66), (121,67), (121,68) 

以上就是xml映射文件中常用标签的使用方法。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小印z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值