Mybatis是是一款优秀的持久层框架(持久化是程序数据在瞬时状态和持久状态间转换的过程。),在dao层大量使用,使sql语句封装在配置文件中,降低程序的耦合度。
2、动态sql,小巧灵活,简单易学。
<mapper namespace="com.bdqn.jiankang.mapper.UserDao">
(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>
(3)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>
(4)这是为了达到代码重用,我们也可以将该外部的代码之间写入association中。
(5)多表复杂数据联合查询如果有集合类型的数据,我们就需要用到collection了。
(6)oftype的值是该属性的类型,id是数据库表中的唯一字段,将唯一字段值用id表示,而不是result,可以提高查询效率。
<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>
(7)有时我们需要根据条件进行查询,多条件组合查询,这时有两种方式:
where-if组合
<resultMap type="User" i<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 > #{starttime} -->
and date <![CDATA[ <=]]> #{starttime}
</if>
<if test="endtime != null and endtime!=''">
<!-- and date < #{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 > #{starttime} -->
and date <![CDATA[ <=]]> #{starttime},
</if>
<if test="endtime != null and endtime!=''">
<!-- and date < #{endtime} -->
and date >= #{endtime},
</if>
</trim>
在trim中,要注意prifix为where,同时注意第一个if条件为and或者or的时候,要用prefixOverrides去除。
<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>
(1)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必须有添加条件,否则会报错。
(2)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>
<delete id="delversion" parameterType="Integer">
delete from app_version where appid=#{appid};
</delete>
(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>
(3)使用@param注解的方法:dao层这样写,dao.xml的#{phone}以及collection的值是注解名。
public Collection<User> findByids3(@Param("ids")int [] ids,@Param("phone") String phone);
以上就是就是关于mybatis的相关介绍以及相关元素如何具体使用,同时介绍了元素使用中的注意事项,可以参考一下,希望对大家有帮助,后面会不断更新相关知识,大家一起进步。