最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis新增數(shù)據(jù)并返回主鍵值方式

 更新時(shí)間:2023年03月06日 08:39:28   作者:草叔編程  
這篇文章主要介紹了MyBatis新增數(shù)據(jù)并返回主鍵值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

MyBatis新增數(shù)據(jù)并返回主鍵值

雖然這個(gè)功能比較簡單,網(wǎng)上帖子也很多,但是有一個(gè)地方有點(diǎn)坑,這里做一個(gè)對(duì)比,作為脫坑的標(biāo)記。

為了測試,寫一個(gè)簡單的添加功能驗(yàn)證一下,直接看效果。

entity

@Component("user")
public class User {
	private int id;
	private String usercode;
	private String password;
	private String name;
	private String phone;
	private int status;

controller層

@ResponseBody
@RequestMapping(method = RequestMethod.POST,value = "/register")
public int register(User user){
		System.out.println("參數(shù):"+user.toString());
		int id = userSer.register(user);
		System.out.println("id:"+id);
		System.out.println(""+user.getId());
		return id;
}

UserDao

//用戶注冊
int register(User user);

UserMapper.xml

<insert id="register" parameterType="com.hans.entity.User" useGeneratedKeys="true" keyProperty="id">
    	INSERT INTO USER (
			usercode,
			PASSWORD,
			phone,
			NAME
		)
		VALUES
			(#{phone},#{password},#{phone},#{name})
</insert>

程序運(yùn)行添加數(shù)據(jù),看到數(shù)據(jù)庫數(shù)據(jù)如下:

再看下,控制臺(tái)輸出情況

可以很清晰的看出,雖然sql語句中并沒有聲明resultType,但影響的行數(shù)(0/1)其實(shí)還是賦值給了sql的運(yùn)行結(jié)果,所以控制臺(tái)的id:1就不難理解了,真正插入操作的主鍵其實(shí)是賦值給實(shí)體類User的字段id中。

結(jié)論很明顯,添加方法返回的是影響的行數(shù)(0/1)。

插入數(shù)據(jù)的主鍵值其實(shí)是賦值給你指定的entity的某個(gè)字段中。

核心代碼:

useGeneratedKeys="true" keyProperty="id"?

MyBatis新增更新返回主鍵

在往sqlserver數(shù)據(jù)庫新增、修改數(shù)據(jù)后,可能需要緊接著進(jìn)行其他操作,比如將數(shù)據(jù)寫入redis,此時(shí)需要該條數(shù)據(jù)的id作為hash結(jié)構(gòu)中的key。

而當(dāng)sql設(shè)置主鍵自增時(shí),傳來的參數(shù)是不帶id的,所有需要在dao層執(zhí)行新增更新操作后返回主鍵。

dao層

新增操作,使用useGeneratedKeys=“true” keyProperty=“id”

? ?<insert id="addFormula" parameterType="testmaven06calculation.com.cal.res.pojo.CalData" useGeneratedKeys="true" keyProperty="id">
? ? ?? ?insert into cal_data(formula,standard,msg)values(#{formula},#{standard},#{msg});
? ? </insert>

更新操作

? ? <update id="updateFormula" parameterType="testmaven06calculation.com.cal.res.pojo.CalData" >
? ? <selectKey keyProperty="id" resultType="int" order="AFTER">
? ? ?? ?select id from cal_data where formula=#{formula};
? ? </selectKey>
? ? ?? ?update cal_data set standard=#{standard},msg=#{msg} where formula=#{formula};
? ? </update>

service層

?? ?@Transactional(rollbackFor = Exception.class)
?? ?public AjaxMessage addUpdateFormula(CalData calData) {
?? ??? ?redisTemplate.setEnableTransactionSupport(true);
?? ??? ?// 檢查公式是否存在
?? ??? ?if (existFormula(calData.getFormula())) { // 公式存在
?? ??? ??? ?// 執(zhí)行更新操作
?? ??? ??? ?try {
?? ??? ??? ??? ?redisTemplate.multi();
?? ??? ??? ??? ?calDataDao.updateFormula(calData);
?? ??? ??? ??? ?redisTemplate.opsForHash().put("calDataHash", String.valueOf(calData.getId()), calData);
?? ??? ??? ??? ?redisTemplate.exec();
?? ??? ??? ??? ?return new AjaxMessage(true, "公式已存在,更新成功", null);
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?redisTemplate.discard();
?? ??? ??? ??? ?TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
?? ??? ??? ??? ?return new AjaxMessage(false, "公式已存在,更新失敗", null);
?? ??? ??? ?}
?? ??? ?} else { // 公式不存在
?? ??? ??? ?// 執(zhí)行新增操作
?? ??? ??? ?try {
?? ??? ??? ??? ?redisTemplate.multi();
?? ??? ??? ??? ?calDataDao.addFormula(calData);
?? ??? ??? ??? ?redisTemplate.opsForSet().add("formulaSet", calData.getFormula());
?? ??? ??? ??? ?redisTemplate.opsForHash().put("calDataHash", String.valueOf(calData.getId()), calData);
?? ??? ??? ??? ?redisTemplate.exec();
?? ??? ??? ??? ?return new AjaxMessage(true, "公式不存在,新增成功", null);
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?redisTemplate.discard();
?? ??? ??? ??? ?TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
?? ??? ??? ??? ?return new AjaxMessage(false, "公式不存在,新增失敗", null);
?? ??? ??? ?}
?? ??? ?}
?? ?}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

山阴县| 大田县| 东方市| 阜宁县| 内江市| 波密县| 苗栗市| 时尚| 吉安市| 建湖县| 永吉县| 普格县| 碌曲县| 阜城县| 石楼县| 尉犁县| 兴业县| 喀喇| 石屏县| 昭苏县| 望江县| 毕节市| 南投市| 新宁县| 惠州市| 土默特右旗| 民和| 孟连| 汝南县| 绩溪县| 太谷县| 都江堰市| 阳春市| 济阳县| 云浮市| 淮南市| 玉环县| 襄汾县| 新疆| 凤山县| 汉阴县|