MyBatis新增數(shù)據(jù)并返回主鍵值方式
MyBatis新增數(shù)據(jù)并返回主鍵值
雖然這個(gè)功能比較簡單,網(wǎng)上帖子也很多,但是有一個(gè)地方有點(diǎn)坑,這里做一個(gè)對(duì)比,作為脫坑的標(biāo)記。
為了測(cè)試,寫一個(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
//用戶注冊(cè) 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)文章
Java Web監(jiān)聽器Listener接口原理及用法實(shí)例
這篇文章主要介紹了Java Web監(jiān)聽器Listener接口原理及用法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06ReentrantReadWriteLock?讀寫鎖分析總結(jié)
這篇文章主要介紹了ReentrantReadWriteLock 讀寫鎖分析總結(jié),ReentranReadWriteLock中有兩把鎖,一把讀鎖,一把寫鎖,關(guān)于這兩把鎖的介紹,需要的小伙伴可以參考一下2022-05-05實(shí)現(xiàn)Servlet程序的三種方法(小結(jié))
這篇文章主要介紹了實(shí)現(xiàn)Servlet程序的三種方法(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java實(shí)現(xiàn)生成pdf并解決表格分割的問題
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)生成pdf,并解決表格分割的問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11