java mybatis新增和修改(批量)
新增(单条数据)
- 持久层接口
/**
- 新增问题
- @param question
- @return
*/
int addQuestion(Question question);
- mapper*.xml
<!-- 新增问题信息 -->
<insert id="addQuestion" parameterType="com.dc.hndj.model.Question">
<selectKey resultType="int" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() as id
</selectKey>
insert into
t_question
(questionTypes,classify,question,createUserId,questionUUID)
values
(#{questionTypes},#{classify},#{question},#{createUserId},#{questionUUID})
</insert>
新增(批量)
- 持久层接口
/**
- 批量添加答案
- @param answerList
- @return
*/
int addAnswer(List<Answer> answerList);
- mapper*.xml
<!-- 批量新增答案信息 -->
<insert id="addAnswer" parameterType="java.util.List">
insert into
t_answer(questionId,options,optionDescribe,grade)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.questionId},#{item.options},#{item.optionDescribe},#{item.grade})
</foreach>
</insert>
修改(单条数据)
- 持久层接口
/**
* 修改问题
* @param question
* @return
*/
int updateQuestion(Question question);
- mapper*.xml
<!-- 修改问题信息 -->
<update id="updateQuestion" parameterType="com.dc.hndj.model.Question">
update
t_question
set
<if test="question != null and question != ''">
question=#{question}
</if>
where id=#{id}
</update>
修改(批量)
- 持久层接口
/**
* 批量修改选项
* @param optionBankList
* @return
*/
int updateOptionBankByIds(@Param("list") List<OptionBank> optionBankList);
- mapper*.xml
<!-- 批量修改答案信息 -->
<insert id="updateOptionBankByIds" parameterType="java.util.List">
update option_bank
<trim prefix="set" suffixOverrides=",">
<trim prefix="description = case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.description !=null and item.description != '' ">
when id=#{item.id} then #{item.description}
</if>
</foreach>
</trim>
<trim prefix="description_en = case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.descriptionEn != null and item.descriptionEn != '' ">
when id=#{item.id} then #{item.descriptionEn}
</if>
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id}
</foreach>
</insert>