Mybatis逆向工程生成的Example类的使用方法

 

假如要使用updateByPrimaryKeySelective方法(根据ID更新表)

xml文件如下:

 
    update trainingprogram
    
      
        professional = #{professional,jdbcType=VARCHAR},
      
      
        grade = #{grade,jdbcType=VARCHAR},
      
      
        courseno = #{courseno,jdbcType=VARCHAR},
      
      
        coursename = #{coursename,jdbcType=VARCHAR},
      
      
        credit = #{credit,jdbcType=DOUBLE},
      
      
        totalhour = #{totalhour,jdbcType=DOUBLE},
      
      
        theoryhour = #{theoryhour,jdbcType=DOUBLE},
      
      
        experimenthour = #{experimenthour,jdbcType=DOUBLE},
      
      
        computerhour = #{computerhour,jdbcType=DOUBLE},
      
      
        testform = #{testform,jdbcType=VARCHAR},
      
      
        coursetype = #{coursetype,jdbcType=VARCHAR},
      
      
        beginsemester = #{beginsemester,jdbcType=VARCHAR},
      
      
        syllabuspath = #{syllabuspath,jdbcType=VARCHAR},
      
      
        teachingplanpath = #{teachingplanpath,jdbcType=VARCHAR},
      
      
        createtime = #{createtime,jdbcType=TIMESTAMP},
      
    
    where id = #{id,jdbcType=INTEGER}
  

对应的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("失败");
		}
	}

 

你可能感兴趣的:(Mybatis逆向工程生成的Example类的使用方法)