广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis中映射文件include标签的应用
  • 358
分享到

mybatis中映射文件include标签的应用

2024-04-02 19:04:59 358人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis映射文件include标签应用1.引用同一个xml中的sql片段2.引用公用的sql片段3.对于多个xml文件需要同时引用一段相同的mybatis sql xml

mybatis映射文件include标签应用

在这里插入图片描述

MyBatis中sql标签定义SQL片段,include标签引用,可以复用SQL片段可以使用标签提取出来,在使用的地方使用标签引用即可.sql标签中id属性对应include标签中的refid属性。通过include标签将sql片段和原sql片段进行拼接成一个完整的sql语句进行执行。

具体用法如下:

1.引用同一个xml中的sql片段


<sql id="sqlid">
    res_type_id,res_type
</sql>
<select id="selectbyId" resultType="com.property.vo.PubResTypeVO">
    select
    <include refid="sqlid"/>
    from pub_res_type
</select>

2.引用公用的sql片段

include标签中也可以用property标签,用以指定自定义属性。在sql标签中通过${}取出对应的属性值


<select id="queryPubResType" parameterType="com.property.vo.PubResTypeVO" resultMap="PubResTypeList">
    select  a.res_type_id,
    <include refid="com.common.dao.FunctionDao.SF_GET_LNG_RES_TYPE">
        <property name="ai_RES_TYPE_ID" value="a.res_type_id"/>
        <property name="lng" value="#{lngId}"/>
        <property name="female" value="'女'"/>
    </include> as res_type
    from    pub_res_type a
</select>

3.对于多个xml文件需要同时引用一段相同的

在某个xml 中定义这个 sql 代码片段,在需要引用的地方使用全称引用即可,例子如下:

ShareMapper.xml


<mapper namespace="com.company.ShareMapper">       
    <sql id="someSQL">
       id,name
    </sql>          
</mapper>

CustomMapper.xml


<mapper namespace="com.company.CustomMapper">       
    <select id="selectSome" >
        select
       <include refid="com.company.ShareMapper.someSQL"/>
        from t
    </select>          
</mapper>

使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。

如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。

  • resultMap:适合使用返回值是自定义实体类的情况
  • resultType:适合使用返回值得数据类型是非自定义的,即jdk的提供的类型.

mybatis sql xml include标签 (代码去重)

mybatis sql xml 对于重复的代码片段 可以使用 include 标签进行优化

例如 以下将查询条件进行优化:


 <sql id="where"><!-- 定义代码片段-->
  <where>
   <if test="CarNo != null and CarNo != ''">
    and car_no like concat('%',#{CarNo},'%')
   </if>
   <if test="CardNo != null and CardNo != ''">
    and card_no = #{CardNo} 
   </if>
   <if test="serviceType != null and serviceType != ''">
    and service_type = #{serviceType} 
   </if>
   .....省略
  </where>
 </sql>
 
 <select id="queryList" resultType="com.framework.entity.WeightEntity">
  select * from weight 
  <include refid="where"/> <!-- 引用-->
  <if test="offset != null and limit != null">
   limit #{offset}, #{limit}
  </if>
 </select>
 
  <select id="queryTotal" resultType="int">
  select count(*) from weight 
  <include refid="where" /> <!-- 引用->
 </select>

当然 include标签不仅仅用于where, 只要重复代码片段都可使用

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: mybatis中映射文件include标签的应用

本文链接: https://www.lsjlt.com/news/158487.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作