mybatis映射器和动态SQL总结
映射器
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中自定义别名如
<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
而实际上type为实体类的情况更常用,resultMap=”BaseResultMap”表示返回类型为实体类
association – 复杂类型的结合;多个结果合成的类型
<resultMap id="fieldMapper" type="com.cmcc.healthcare.pojo.hars.Appointmentrecord">
<id property="id" column="ID" /><!---->
<result property="hospitaluid" column="HOSPITALUID" /><!--医院主键id-->
<association property="hospitalinfo" resultMap="hospitalinfoMap" /><!--医院信息-->
</resultMap>
3、主键回填,插入操作后返回自增Id,useGeneratedKeys用来控制是否打开自动生成主键这个功能,默认是false,当打开了这个开关,还要配置其属性keyProperty或keyColumn,告诉系统将生成的主键放入到哪个属性中。
<insert id="insertUserInfo" parameterType="com.cmcc.akso.entity.user.UserInfo" keyProperty= "memberId" useGeneratedKeys ="true">
4、字段为sql关键字如from,可使用反引号
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代表的是那种字符串。
<insert id="insertSelective" parameterType="com.cmcc.akso.entity.user.UserInfo"
useGeneratedKeys="true" keyProperty="memberId">
insert into t_user_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="memberCode != null">MEMBER_CODE,</if>
<if test="enterpriseId != null">enterprise_id,</if>
<if test="updateTime != null">update_time</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="memberCode != null">#{memberCode,jdbcType=VARCHAR},</if>
<if test="enterpriseId != null">#{enterpriseId,jdbcType=INTEGER},</if>
<if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP}</if>
</trim>
</insert>
<select id="findMusicIds" resultType="int">
SELECT
am.`music_id` AS musicId
FROM
t_album_music am
<trim prefix="where" prefixOverrides="and">
<if test="roleNo !=1 and roleNo!=''">
AND role_no = #{roleNo}
</if>
</trim>
</select>
2、批量操作或in需要使用到
<insert id="insertBatUserInfo" >
insert ignore into t_user_info (
MEMBER_CODE,
DEPARTMENT_ID,
UPDATE_TIME
)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.memberCode,jdbcType=VARCHAR},
#{item.departmentId,jdbcType=INTEGER},
#{item.updateTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>
<select id="getSysParamById" resultType="Base_SysParam" parameterType="String">
SELECT * FROM BASE_SYSPARAM
WHERE COMPID=#{compId}
AND PARAMID in
<foreach collection="paramId" item="item" index="index" open="("
separator="," close=")">
#{item}
</foreach>
ORDER BY TYPE,PARAMID
</select>
3、数值比较时不允许出现”>”、”&&”这样的字符,因为这个mapper文件是xml格式的,使用<![CDATA[ ]]>这类符号不再进行解析
<if test="endTime!=null"><![CDATA[ and end_time<= #{endTime}]]></if>
<if test="searchFlag != null && searchFlag == 2">
m.`music_name` AS resultName
</if>
4、选择判断
<select id="getExistedMusicIds" resultType="java.lang.Integer">
SELECT
am.`music_id` AS musicId
FROM
t_album_music am
WHERE 1=1
<choose>
<when test="roleNo !=1 and roleNo!=''">
AND role_no = #{roleNo}
</when>
<when test="roleName !=null and roleName!=''">
AND role_name like concat('%',#{roleName},'%')
</when>
<otherwise>
AND note is not NULL
</otherwise>
</choose>
</select>
5、以上示例如果不使用1=1,有时会出错,加入后又觉得多余,可以通过
<select id="getExistedMusicIds" resultType="java.lang.Integer">
SELECT
am.`music_id` AS musicId
FROM
t_album_music am
<where>
<if test="roleNo !=1 and roleNo!=''">
AND role_no = #{roleNo}
</if>
<if test="roleName !=null and roleName!=''">>
AND role_name like concat('%',#{roleName},'%')
</if>
</where>
</select>
6、更新操作使用
<update id="updateRole" parameterType="role">
update t_role
<set>
<if test="roleName != null and roleName !=''">
role_name = #{roleName},
</if>
<if test="note != null and note !=''">
note = #{note},
</if>
</set>
where role_no = #{roleNo}
</update>
7、
<select id="getExistedMusicIds" resultType="java.lang.Integer">
<bind name="pattern_roleName" value="'%'+roleName+'%'"></bind>
SELECT
am.`music_id` AS musicId
FROM
t_album_music am
<where>
<if test="roleNo !=1 and roleNo!=''">
AND role_no = #{roleNo}
</if>
<if test="roleName !=null and roleName!=''">>
AND role_name like #{pattern_roleName}
</if>
</where>
</select>
参考:mybatis官方文档
还没有评论,来说两句吧...