目录
mybatis if标签判断字符串、判断数值相等、判断集合是否为空
笔记针对mysql,对oracle还未测试
mybatis一对多查询,一对一查询
<resultMap id="extendResultMap" type="com.zxy.dpp.recordStatistics.po.ProjDataVo">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="fill_subject" jdbcType="VARCHAR" property="fillSubject"/>
<result column="fill_subject_no" jdbcType="VARCHAR" property="fillSubjectNo"/>
<result column="proj_name" jdbcType="VARCHAR" property="projName"/>
<result column="project_accept_status" jdbcType="VARCHAR" property="projectAcceptStatus"/>
<result column="project_accept_status_value" jdbcType="VARCHAR" property="projectAcceptStatusValue"/>
<result column="file_id" jdbcType="BIGINT" property="fileId"/>
<result column="del_flag" jdbcType="VARCHAR" property="delFlag"/>
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
<!--一对一-->
<association property="commonFile" column="file_id" javaType="com.zxy.dpp.file.bean.CommonFile" select="com.zxy.dpp.file.dao.CommonFileMapper.selectByPrimaryKey"/>
<!--一对多-->
<collection property="arrivalAccountInfoList"
column="id"
ofType="com.zxy.dpp.recordStatistics.bean.ProjData"
javaType="ArrayList"
select="com.zxy.dpp.recordStatistics.dao.ArrivalAccountInfoMapper.selectAccountInfoList"/>
</resultMap>
mybatis插入语句配置自动赋值主键
如果表主键为`id` bigint NOT NULL AUTO_INCREMENT设计,插入时配置上 useGeneratedKeys="true" keyProperty="id"即可。具体写法如下
<insert id="insert" parameterType="com.zxy.dpp.recordStatistics.bean.PolicyInformation" useGeneratedKeys="true"
keyProperty="id">
insert into t_policy_information ( fill_subject, fill_subject_no,
policy_name, policy_view, policy_level,
policy_issuing_unit, text_number, writing_date,
release_date, original_URL, is_ciphertext,
file_id, del_flag, create_user_Id,
create_user_name, update_user_Id, update_user_name,
create_time, update_time)
values ( #{fillSubject,jdbcType=VARCHAR}, #{fillSubjectNo,jdbcType=VARCHAR},
#{policyName,jdbcType=VARCHAR}, #{policyView,jdbcType=VARCHAR}, #{policyLevel,jdbcType=VARCHAR},
#{policyIssuingUnit,jdbcType=VARCHAR}, #{textNumber,jdbcType=VARCHAR}, #{writingDate,jdbcType=DATE},
#{releaseDate,jdbcType=DATE}, #{originalUrl,jdbcType=VARCHAR}, #{isCiphertext,jdbcType=VARCHAR},
#{fileId,jdbcType=BIGINT}, #{delFlag,jdbcType=VARCHAR}, #{createUserId,jdbcType=BIGINT},
#{createUserName,jdbcType=VARCHAR}, #{updateUserId,jdbcType=BIGINT}, #{updateUserName,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
</insert>
mybatis if标签判断字符串、判断数值相等、判断集合是否为空
mybatis 映射文件中,if标签判断字符串相等,两种方式:因为mybatis映射文件,是使用的ognl表达式,所以在判断字符串sex变量是否是字符串Y的时候,
方式一:
<if test="sex=='Y'.toString()">
方式二:
<if test = 'sex== "Y"'>
方式三:
<if test = 'sex.equals("Y")'>
判断数据相等
<if test=" 4 == flag ">
, LAST_CHECK_TIME = sysdate
</if>
foreach使用
查询
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
插入
<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
</insert>
多数据库支持
<insert id="insert">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
<if test="_databaseId == 'oracle'">
select seq_users.nextval from dual
</if>
<if test="_databaseId == 'db2'">
select nextval for seq_users from sysibm.sysdummy1"
</if>
</selectKey>
insert into users values (#{id}, #{name})
</insert>