mybatis映射器和动态SQL总结

清疚 2022-07-14 06:55 307阅读 0赞

映射器

mybatis的映射器包含两部分:接口和映射文件。映射器的配置元素包括:select、insert、update、delete、sql、resultMap。mybatis映射文件的总结:
1、对于parameterType入参类型常用的有java.util.Map、java.lang.Integer等,可以采用系统自带的别名如byte、long、short、int、integer、float、double、boolean、string、date、map、list、decimal、bigdecimal等简化配置,也可以在mapper中自定义别名如

  1. <typeAlias alias="HdPluginDO" type="com.abc.dal.dataObject.HdPluginDO"/>

传递多个参数的情况下,有如下传参方式:
1) 封装到map,用map接收;
2) 通过@Param(“userId”)传递,无需parameterType接收可直接使用;
3) 通过POJO封装同时用它接收。
建议5个以下参数采用第二种方式,5个以上采用第三种。返回类型一般有resultType和resultMap两种,insert、update、delete是不需要返回类型的,默认是整型。

2、resultMap用来映射结果集。type为映射的实体类,可以是类的全限定名或别名;id为此resultMap的标识。resultMap包含id、result、association 子元素,id和result用来将POJO的属性和SQL的列名做对应。
resutltMap的type属性设置为java.util.Map可以在dao中用Map接收数据

Center

而实际上type为实体类的情况更常用,resultMap=”BaseResultMap”表示返回类型为实体类

Center 1

association – 复杂类型的结合;多个结果合成的类型

  1. <resultMap id="fieldMapper" type="com.cmcc.healthcare.pojo.hars.Appointmentrecord">
  2. <id property="id" column="ID" /><!---->
  3. <result property="hospitaluid" column="HOSPITALUID" /><!--医院主键id-->
  4. <association property="hospitalinfo" resultMap="hospitalinfoMap" /><!--医院信息-->
  5. </resultMap>

3、主键回填,插入操作后返回自增Id,useGeneratedKeys用来控制是否打开自动生成主键这个功能,默认是false,当打开了这个开关,还要配置其属性keyProperty或keyColumn,告诉系统将生成的主键放入到哪个属性中。

  1. <insert id="insertUserInfo" parameterType="com.cmcc.akso.entity.user.UserInfo" keyProperty= "memberId" useGeneratedKeys ="true">

4、字段为sql关键字如from,可使用反引号

  1. INSERT IGNORE INTO t_user_info( member_code, enterprise_id, `from` ) VALUES ( #{memberCode}, #{enterpriseId}, #{from})

动态SQL

动态SQL的基本元素包括:if、set、where、bind、foreach、choose(when,otherwise)、trim(where,set)

1、用来截取并去掉特殊的字符串

prefix代表的是语句的前缀,而prefixOverrides代表的是那种字符串。

  1. <insert id="insertSelective" parameterType="com.cmcc.akso.entity.user.UserInfo"
  2. useGeneratedKeys="true" keyProperty="memberId">
  3. insert into t_user_info
  4. <trim prefix="(" suffix=")" suffixOverrides=",">
  5. <if test="memberCode != null">MEMBER_CODE,</if>
  6. <if test="enterpriseId != null">enterprise_id,</if>
  7. <if test="updateTime != null">update_time</if>
  8. </trim>
  9. <trim prefix="values (" suffix=")" suffixOverrides=",">
  10. <if test="memberCode != null">#{memberCode,jdbcType=VARCHAR},</if>
  11. <if test="enterpriseId != null">#{enterpriseId,jdbcType=INTEGER},</if>
  12. <if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP}</if>
  13. </trim>
  14. </insert>
  15. <select id="findMusicIds" resultType="int">
  16. SELECT
  17. am.`music_id` AS musicId
  18. FROM
  19. t_album_music am
  20. <trim prefix="where" prefixOverrides="and">
  21. <if test="roleNo !=1 and roleNo!=''">
  22. AND role_no = #{roleNo}
  23. </if>
  24. </trim>
  25. </select>

2、批量操作或in需要使用到

  1. <insert id="insertBatUserInfo" >
  2. insert ignore into t_user_info (
  3. MEMBER_CODE,
  4. DEPARTMENT_ID,
  5. UPDATE_TIME
  6. )
  7. values
  8. <foreach collection="list" item="item" index="index" separator=",">
  9. (
  10. #{item.memberCode,jdbcType=VARCHAR},
  11. #{item.departmentId,jdbcType=INTEGER},
  12. #{item.updateTime,jdbcType=TIMESTAMP}
  13. )
  14. </foreach>
  15. </insert>
  16. <select id="getSysParamById" resultType="Base_SysParam" parameterType="String">
  17. SELECT * FROM BASE_SYSPARAM
  18. WHERE COMPID=#{compId}
  19. AND PARAMID in
  20. <foreach collection="paramId" item="item" index="index" open="("
  21. separator="," close=")">
  22. #{item}
  23. </foreach>
  24. ORDER BY TYPE,PARAMID
  25. </select>

3、数值比较时不允许出现”>”、”&&”这样的字符,因为这个mapper文件是xml格式的,使用<![CDATA[ ]]>这类符号不再进行解析

  1. <if test="endTime!=null"><![CDATA[ and end_time<= #{endTime}]]></if>
  2. <if test="searchFlag != null && searchFlag == 2">
  3. m.`music_name` AS resultName
  4. </if>

4、选择判断

  1. <select id="getExistedMusicIds" resultType="java.lang.Integer">
  2. SELECT
  3. am.`music_id` AS musicId
  4. FROM
  5. t_album_music am
  6. WHERE 1=1
  7. <choose>
  8. <when test="roleNo !=1 and roleNo!=''">
  9. AND role_no = #{roleNo}
  10. </when>
  11. <when test="roleName !=null and roleName!=''">
  12. AND role_name like concat('%',#{roleName},'%')
  13. </when>
  14. <otherwise>
  15. AND note is not NULL
  16. </otherwise>
  17. </choose>
  18. </select>

5、以上示例如果不使用1=1,有时会出错,加入后又觉得多余,可以通过去处理,当where的某个条件成立时,才会加入这个条件

  1. <select id="getExistedMusicIds" resultType="java.lang.Integer">
  2. SELECT
  3. am.`music_id` AS musicId
  4. FROM
  5. t_album_music am
  6. <where>
  7. <if test="roleNo !=1 and roleNo!=''">
  8. AND role_no = #{roleNo}
  9. </if>
  10. <if test="roleName !=null and roleName!=''">>
  11. AND role_name like concat('%',#{roleName},'%')
  12. </if>
  13. </where>
  14. </select>

6、更新操作使用根据传入的参数动态更新指定的字段

  1. <update id="updateRole" parameterType="role">
  2. update t_role
  3. <set>
  4. <if test="roleName != null and roleName !=''">
  5. role_name = #{roleName},
  6. </if>
  7. <if test="note != null and note !=''">
  8. note = #{note},
  9. </if>
  10. </set>
  11. where role_no = #{roleNo}
  12. </update>

7、是通过ognl表达式定义的上下文变量,这样更加方便使用。用来替换mysql 的concat拼接和oracle中的||拼接。

  1. <select id="getExistedMusicIds" resultType="java.lang.Integer">
  2. <bind name="pattern_roleName" value="'%'+roleName+'%'"></bind>
  3. SELECT
  4. am.`music_id` AS musicId
  5. FROM
  6. t_album_music am
  7. <where>
  8. <if test="roleNo !=1 and roleNo!=''">
  9. AND role_no = #{roleNo}
  10. </if>
  11. <if test="roleName !=null and roleName!=''">>
  12. AND role_name like #{pattern_roleName}
  13. </if>
  14. </where>
  15. </select>

参考:mybatis官方文档

发表评论

表情:
评论列表 (有 0 条评论,307人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mybatis 映射

    mybatis 映射器 1 映射器 Mapper 是由java接口和 XML 文件共同组成。它的作用如下 1)定义参数类型 2)描述缓存 3)描述 SQL 语句 4