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

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

一、优势:

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

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

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

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

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

2、select:

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

 <select id="query" resultType="User">
		select * from user
	</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去除。

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

<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实例:

<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组合

  <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

   <delete id="delversion" parameterType="Integer">
       delete from app_version where appid=#{appid};
   </delete>

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

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

<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,否则集合类型传不进去。

  public Collection<User> findbymap(Map<String,Object> map);

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

 <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层这样写:

public Collection<User> findByids3(@Param("ids")int [] ids,@Param("phone") String phone);

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

以上是对Mybatis的总结,更多精彩内容请关注下面公众号:

java基础笔记
java基础笔记

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卧龙不言

欢迎关注java基础笔记公众号

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

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

打赏作者

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

抵扣说明:

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

余额充值