Mybatis逆向工程生成的Example類的使用方法
阿新 • • 發佈:2018-12-02
假如要使用updateByPrimaryKeySelective方法(根據ID更新表)
xml檔案如下:
<update id="updateByPrimaryKeySelective" parameterType="schooloa.bean.TrainingProgram"> update trainingprogram <set> <if test="professional != null"> professional = #{professional,jdbcType=VARCHAR}, </if> <if test="grade != null"> grade = #{grade,jdbcType=VARCHAR}, </if> <if test="courseno != null"> courseno = #{courseno,jdbcType=VARCHAR}, </if> <if test="coursename != null"> coursename = #{coursename,jdbcType=VARCHAR}, </if> <if test="credit != null"> credit = #{credit,jdbcType=DOUBLE}, </if> <if test="totalhour != null"> totalhour = #{totalhour,jdbcType=DOUBLE}, </if> <if test="theoryhour != null"> theoryhour = #{theoryhour,jdbcType=DOUBLE}, </if> <if test="experimenthour != null"> experimenthour = #{experimenthour,jdbcType=DOUBLE}, </if> <if test="computerhour != null"> computerhour = #{computerhour,jdbcType=DOUBLE}, </if> <if test="testform != null"> testform = #{testform,jdbcType=VARCHAR}, </if> <if test="coursetype != null"> coursetype = #{coursetype,jdbcType=VARCHAR}, </if> <if test="beginsemester != null"> beginsemester = #{beginsemester,jdbcType=VARCHAR}, </if> <if test="syllabuspath != null"> syllabuspath = #{syllabuspath,jdbcType=VARCHAR}, </if> <if test="teachingplanpath != null"> teachingplanpath = #{teachingplanpath,jdbcType=VARCHAR}, </if> <if test="createtime != null"> createtime = #{createtime,jdbcType=TIMESTAMP}, </if> </set> where id = #{id,jdbcType=INTEGER} </update>
對應的Mapper裡面的方法為:
int updateByExampleSelective(@Param("record") TrainingProgram record, @Param("example") TrainingProgramExample example);
Service
public int UpdateTrinPro(TrainingProgram trainingProgram,TrainingProgramExample trainingProgramExample) { Criteria s= trainingProgramExample.createCriteria(); s.andIdEqualTo(trainingProgram.getId());//表示條件,id=trainingProgram.getId() return trainingProgramMapper.updateByExampleSelective(trainingProgram,trainingProgramExample); }
Example使用:
TrainingProgramExample tpe=new TrainingProgramExample();
Criteria s= tpe.createCriteria();
s.andIdEqualTo(trainingProgram.getId());
trainingProgramMapper.updateByExampleSelective(trainingProgram,trainingProgramExample);
JunitTest
public void test1() { TrainingProgram trainingProgram=new TrainingProgram(); trainingProgram.setId(40); trainingProgram.setProfessional("農學"); TrainingProgramExample trainingProgramExample=new TrainingProgramExample(); int count=trainingProgramService.UpdateTrinPro(trainingProgram, trainingProgramExample); if (count>0) { System.out.println("成功"); }else { System.out.println("失敗"); } }