前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis的sql映射文件的顶级元素使用实例(超级详细)

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

作者头像
jiankang666
发布2022-05-12 20:27:29
3770
发布2022-05-12 20:27:29
举报
文章被收录于专栏:java基础笔记

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

一、优势:

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

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

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

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

代码语言:javascript
复制
<mapper namespace="com.bdqn.jiankang.mapper.UserDao">

2、select:

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

代码语言:javascript
复制
 <select id="query" resultType="User">
    select * from user
  </select>

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

代码语言:javascript
复制
 <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。

代码语言:javascript
复制
    <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了。

代码语言:javascript
复制
    <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组合

代码语言:javascript
复制
 <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组合

代码语言:javascript
复制
 <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去除。

3、insert实现:注意每个字段之间一一对应就行了。

代码语言:javascript
复制
<insert id="addRum" parameterType="RegRum"> 
     insert into regnum(patientname,cardtype,cardid,socalnum,phone,sex,
     age,position,firstdiagnose,doctorid,status,remark,date) 
     VALUES(#{regnum.patientname},#{regnum.cardtype},#{regnum.cardid}
</insert>

4、update:可以使用set-if或者trim-if

set-if实例:

代码语言:javascript
复制
<update id="updateregnum">
        update regnum
        <set >
            <if test="phone!=null and phone!=''">
                phone=#{phone},
            </if>
            <if test="position!=null and position!=''">
                position=#{position},
            </if>
            <if test="firstdiagnose!=null and firstdiagnose!=''">
                firstdiagnose=#{firstdiagnose},
            </if>
            <if test="doctorid!=0">
                doctorid=#{doctorid},
            </if>
            <if test="remark!=null and remark!=''">
                remark=#{remark},
            </if>
            <if test="date!=null and date!=''">
                date=#{date},
            </if>
            <if test="age!=0">
                age=#{age},
            </if>
            <if test="status!=null and status !=''">
                status=#{status},
            </if>
            <if test="sex==0 or sex==1">
                sex=#{sex},
            </if>
        </set>
        where patientid=#{patientid}
    </update>

if的判断test必须有添加条件,否则会报错。

trim-if组合

代码语言:javascript
复制
  <update id="updateregnum">
        update regnum
        <trim prefix="set" suffixOverrides="," suffix=" where patientid=#{patientid}">
            <if test="phone!=null and phone!=''">
                phone=#{phone},
            </if>
            <if test="position!=null and position!=''">
                position=#{position},
            </if>
            <if test="firstdiagnose!=null and firstdiagnose!=''">
                firstdiagnose=#{firstdiagnose},
            </if>
            <if test="doctorid!=0">
                doctorid=#{doctorid},
            </if>
            <if test="remark!=null and remark!=''">
                remark=#{remark},
            </if>
            <if test="date!=null and date!=''">
                date=#{date},
            </if>
            <if test="age!=0">
                age=#{age},
            </if>
            <if test="status!=null and status !=''">
                status=#{status},
            </if>
            <if test="sex!=0">
                sex=#{sex},
            </if>
        </trim>
    </update>

5、delete

代码语言:javascript
复制
   <delete id="delversion" parameterType="Integer">
       delete from app_version where appid=#{appid};
   </delete>

6、foreach的实例:迭代一个集合,通常用于in条件。

(1)只传入一个参数,集合类型的。

代码语言:javascript
复制
<select id="findByids2" resultType="User">
        select * from user where userid in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
</select>

collection表示传入的集合名字,item的值表示集合中每一个值的别名,open以什么开头,close以什么结尾,separator表示值之间以什么分隔。

(2)传入多个参数,我们可以用map集合传入。当然也可以用@param注解。

使用map时:dao层的map值一定要是Object,否则集合类型传不进去。

代码语言:javascript
复制
  public Collection<User> findbymap(Map<String,Object> map);

dao.xml的#{phone}以及collection的值是map所对应的键。

代码语言:javascript
复制
 <select id="findbymap" resultType="User">
        select * from user where userid in
        <foreach collection="idslist" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
 
        and phone=#{phone};
  </select>

使用@param注解的方法:dao层这样写:

代码语言:javascript
复制
public Collection<User> findByids3(@Param("ids")int [] ids,@Param("phone") String phone);

dao.xml的#{phone}以及collection的值是注解名。

以上是对Mybatis的总结

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java基础笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、优势:
  • 二、下面我们具体介绍元素的使用:
    • 1、mapper:他的属性namespace与接口完全限定名保存一致。下图的UserDao.xml的mapper应为:文件结构如下:
      • 2、select:
        • 3、insert实现:注意每个字段之间一一对应就行了。
          • 4、update:可以使用set-if或者trim-if
            • 5、delete
              • 6、foreach的实例:迭代一个集合,通常用于in条件。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档