Mybatis的sql映射文件的顶级元素使用实例(超级详细)(上)

简介: Mybatis是是一款优秀的持久层框架(持久化是程序数据在瞬时状态和持久状态间转换的过程。),在dao层大量使用,使sql语句封装在配置文件中,降低程序的耦合度。

Mybatis是是一款优秀的持久层框架(持久化是程序数据在瞬时状态和持久状态间转换的过程。),在dao层大量使用,使sql语句封装在配置文件中,降低程序的耦合度。

一、优势:

1、不用再考虑公共问题,专心在业务实现上结构统一,易于学习、维护。

2、动态sql,小巧灵活,简单易学。

二、下面我们具体介绍元素的使用:

1、mapper:他的属性namespace与接口完全限定名保存一致。下图的UserDao.xml的mapper应为:

<mapper namespace="com.bdqn.jiankang.mapper.UserDao">

image.png

2、select:

(1)单一表查询,只需要根据实体类的字段进行即可。

1. <select id="query" resultType="User">
2.    select * from user
3.  </select>

(2)多表查询,一个实体类里有其他实体类作为属性,这是我们无法通过一个实体类输出所有的字段,必须进行手动映射,使用ResultMap。ResultMap进行手动映射也解决了字段信息与对象属性不一致的情况,在复杂联合查询中自由控制映射结果。

 <resultMap type="RegRum" id="reg">
        <id property="patientid" column="patientid" />
        <result property="patientname" column="patientname"></result>
        <result property="sex" column="sex" />
        <result property="cardtype" column="cardtype" />
        <result property="cardid" column="cardid" />
        <result property="socalnum" column="socalnum" />
        <result property="phone" column="phone" />
        <result property="age" column="age" />
        <result property="position" column="position" />
        <result property="status" column="status" />
        <result property="remark" column="remark" />
        <result property="date" column="date" />
        <association javaType="Doctor" property="doctor" resultMap="doctor">
        </association>
  </resultMap>

assocation是连接实体类javabean属性的,javaType指定类型,property是RugRum的实体属性,resultMap是外部引用的resultMap。

    <resultMap id="doctor" type="Doctor">
        <id property="doctorid" column="doctorid"></id>
        <result property="dname" column="dname"></result>
        <result property="subroomname" column="subroomname"></result>
    </resultMap>

这是为了达到代码重用,我们也可以将该外部的代码之间写入association中。

(3)多表复杂数据联合查询如果有集合类型的数据,我们就需要用到collection了。

    <resultMap type="User" id="querybyname1">
        <id property="uid" column="uid" />
        <result property="uname" column="uname"></result>
        <result property="upwd" column="upwd" />
        <collection property="roles" ofType="Role">
            <id property="rid" column="rid"></id>
            <result column="rname" property="rname"></result>
            <collection property="permissions" ofType="Permission">
                <id property="pid" column="pid"></id>
                <result column="pname" property="pname"></result>
            </collection>
        </collection>
    </resultMap>

oftype的值是该属性的类型,id是数据库表中的唯一字段,将唯一字段值用id表示,而不是result,可以提高查询效率。

(4)有时我们需要根据条件进行查询,多条件组合查询,这时有两种方式:

where-if组合

 <select id="querySelect" resultMap="reg">
        select * from doctor as d left join regnum as r on r.doctorid
        = d.doctorid left join subjectroom as s on s.subroomid=d.subroomid
        <where>
            <if test="patientid!=null and patientid!=''">
                and patientid=#{patientid}
            </if>
            <if test="dname != null and dname!=''">
                and dname like '%${dname}%'
            </if>
            <if test="subjectroom != null and subjectroom!=''">
                and s.subroomname like '%${subjectroom}%'
            </if>
            <if test="starttime != null and starttime!=''">
                <!-- and date &gt; #{starttime} -->
                and date <![CDATA[ <=]]> #{starttime}
            </if>
            <if test="endtime != null and endtime!=''">
                <!-- and date &lt; #{endtime} -->
                and date >= #{endtime}
            </if>
        </where>
    </select>

trim-if组合

 <select id="querySelect2" resultMap="reg">
        select * from doctor as d left join regnum as r on r.doctorid
        = d.doctorid left join subjectroom as s on s.subroomid=d.subroomid
        <trim prefix="where" prefixOverrides="and|or">
            <if test="patientid!=null and patientid!=''">
                and patientid=#{patientid},
            </if>
            <if test="dname != null and dname!=''">
                and dname like '%${dname}%',
            </if>
            <if test="subjectroom != null and subjectroom!=''">
                and s.subroomname like '%${subjectroom}%',
            </if>
            <if test="starttime != null and starttime!=''">
                <!-- and date &gt; #{starttime} -->
                and date <![CDATA[ <=]]> #{starttime},
            </if>
            <if test="endtime != null and endtime!=''">
                <!-- and date &lt; #{endtime} -->
                and date >= #{endtime},
            </if>
        </trim>

在trim中,要注意prifix为where,同时注意第一个if条件为and或者or的时候,要用prefixOverrides去除。

目录
相关文章
|
1月前
|
SQL Java 数据库连接
MyBatis 的映射关系
MyBatis 核心功能之一是映射关系,支持一对一、一对多和多对多三种 ORM 映射。通过实体类与配置文件结合,开发者可灵活实现数据关联,提升数据库操作效率。
189 4
|
7月前
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
5月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
458 5
|
7月前
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
7月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
7月前
|
SQL XML Java
七、MyBatis自定义映射resultMap
七、MyBatis自定义映射resultMap
196 6
|
7月前
|
Java 数据库连接 mybatis
MyBatis篇-映射关系(1-1 1-n n-n)
本文介绍了MyBatis中四种常见关系映射的配置方法,包括一对一、一对多、多对一和多对多。**一对一**通过`resultMap`实现属性与字段的映射;**一对多**以用户-角色为例,使用`&lt;collection&gt;`标签关联集合数据;**多对一**以作者-博客为例,利用`&lt;association&gt;`实现关联;**多对多**则通过引入第三方类(如UserForDept)分别在User和Dept类中添加集合属性,并配置对应的`&lt;collection&gt;`标签完成映射。这些方法解决了复杂数据关系的处理问题,提升了开发效率。
|
7月前
|
SQL XML Java
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
193 0
|
10月前
|
XML Java 数据库连接
Mybatis映射关系
简介:本文介绍了MyBatis框架中四种常见的关系映射方式,包括一对一、一对多、多对一及多对多。一对一通过简单属性映射实现;一对多通过在主对象中添加集合属性并使用`&lt;collection&gt;`标签映射子对象集合;多对一则利用`&lt;association&gt;`标签在主对象中映射单个子对象;多对多需引入第三方类,分别在两个主对象中添加对方的集合属性,并通过`&lt;collection&gt;`标签实现映射。
197 32
|
11月前
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件