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去除。

目录
相关文章
|
2月前
|
SQL Java 数据库连接
MyBatis 的映射关系
MyBatis 核心功能之一是映射关系,支持一对一、一对多和多对多三种 ORM 映射。通过实体类与配置文件结合,开发者可灵活实现数据关联,提升数据库操作效率。
234 4
|
8月前
|
SQL Java 数据库连接
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
【YashanDB知识库】解决mybatis的mapper文件sql语句结尾加分号";"报错
|
6月前
|
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;`实现代码复用,优化维护效率。
537 5
|
8月前
|
SQL Java 数据库连接
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
【YashanDB 知识库】解决 mybatis 的 mapper 文件 sql 语句结尾加分号";"报错
|
8月前
|
SQL 缓存 Java
框架源码私享笔记(02)Mybatis核心框架原理 | 一条SQL透析核心组件功能特性
本文详细解构了MyBatis的工作机制,包括解析配置、创建连接、执行SQL、结果封装和关闭连接等步骤。文章还介绍了MyBatis的五大核心功能特性:支持动态SQL、缓存机制(一级和二级缓存)、插件扩展、延迟加载和SQL注解,帮助读者深入了解其高效灵活的设计理念。
|
8月前
|
Java 数据库连接 mybatis
MyBatis篇-映射关系(1-1 1-n n-n)
本文介绍了MyBatis中四种常见关系映射的配置方法,包括一对一、一对多、多对一和多对多。**一对一**通过`resultMap`实现属性与字段的映射;**一对多**以用户-角色为例,使用`&lt;collection&gt;`标签关联集合数据;**多对一**以作者-博客为例,利用`&lt;association&gt;`实现关联;**多对多**则通过引入第三方类(如UserForDept)分别在User和Dept类中添加集合属性,并配置对应的`&lt;collection&gt;`标签完成映射。这些方法解决了复杂数据关系的处理问题,提升了开发效率。
|
8月前
|
SQL XML Java
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性
226 0
|
5月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
926 1
Spring boot 使用mybatis generator 自动生成代码插件
|
8月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
661 0
|
10月前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
436 2