天天看點

mybatis注解開發增删改查

1、添加

@Insert(" INSERT INTO audio (audio_id,audio_name,use_count,audio_text,audio_path,audio_type,create_time,create_by,type,version_number) " +
        " VALUES (#{audioId},#{audioName},#{useCount},#{audioText},#{audioPath},#{audioType},#{createTime},#{createBy},#{type},#{versionNumber}) ")
int insert(Audio audio);
           

2、修改

@Update("update audio set audio_path = #{audioPath},update_time=#{updateTime},update_by=#{updateBy},version_number = version_number +1 where audio_id = #{audioId}")
int updateAudioPathByAudioId(String audioPath, String audioId, Date updateTime,String updateBy);
           

3、查詢:

3.1)表字段與實體類屬性一緻

@Select( "select audio_id,audio_name,use_count,audio_text,audio_path,audio_type,type,version_number from audio where audio_id = #{audioId} " ) Audio getByAudioId(String audioId);

3.2) 表字段與實體類屬性不一緻

3.2.1)使用map

@Select( "select audio_id,audio_name,use_count,audio_text,audio_path,audio_type,type,version_number from audio where audio_id = #{audioId} " ) @Results(id="audioResult",value = { @Result(property = "id",column = "id"), @Result(property = "audioId",column = "audio_id"), @Result(property = "audioName",column = "audio_name"), @Result(property = "useCount",column = "use_count"), @Result(property = "audioText",column = "audio_text"), @Result(property = "audioPath",column = "audio_path"), @Result(property = "audioType",column = "audio_type"), @Result(property = "createTime",column = "create_time"), @Result(property = "type",column = "type"), @Result(property = "versionNumber",column = "version_number") }) Audio getByAudioId(String audioId);

其他方法使用上面的map

@Select( "select audio_id,audio_name,use_count,audio_text,audio_path,audio_type,type,version_number from audio where audio_id = #{audioId} " ) @ResultMap( value = "audioResult") Audio getByAudioId(String audioId);

3.2.2) 使用别名

@Select( "select audio_id audioId,audio_name audioName,use_count userCount,audio_text audioText,audio_path audioPath,audio_type audioType , type,version_number versionNumber from audio where audio_id = #{audioId} " ) Audio getByAudioId(String audioId);

4、删除

@Delete("delete from audio where audio_id = #{audioId}") int delete(String audioId);

繼續閱讀