MyBatis新增數(shù)據(jù)并返回主鍵值方式
MyBatis新增數(shù)據(jù)并返回主鍵值
雖然這個功能比較簡單,網(wǎng)上帖子也很多,但是有一個地方有點坑,這里做一個對比,作為脫坑的標記。
為了測試,寫一個簡單的添加功能驗證一下,直接看效果。
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>
程序運行添加數(shù)據(jù),看到數(shù)據(jù)庫數(shù)據(jù)如下:
再看下,控制臺輸出情況
可以很清晰的看出,雖然sql語句中并沒有聲明resultType,但影響的行數(shù)(0/1)其實還是賦值給了sql的運行結(jié)果,所以控制臺的id:1就不難理解了,真正插入操作的主鍵其實是賦值給實體類User的字段id中。
結(jié)論很明顯,添加方法返回的是影響的行數(shù)(0/1)。
插入數(shù)據(jù)的主鍵值其實是賦值給你指定的entity的某個字段中。
核心代碼:
useGeneratedKeys="true" keyProperty="id"?
MyBatis新增更新返回主鍵
在往sqlserver數(shù)據(jù)庫新增、修改數(shù)據(jù)后,可能需要緊接著進行其他操作,比如將數(shù)據(jù)寫入redis,此時需要該條數(shù)據(jù)的id作為hash結(jié)構(gòu)中的key。
而當sql設置主鍵自增時,傳來的參數(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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Web監(jiān)聽器Listener接口原理及用法實例
這篇文章主要介紹了Java Web監(jiān)聽器Listener接口原理及用法實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06ReentrantReadWriteLock?讀寫鎖分析總結(jié)
這篇文章主要介紹了ReentrantReadWriteLock 讀寫鎖分析總結(jié),ReentranReadWriteLock中有兩把鎖,一把讀鎖,一把寫鎖,關(guān)于這兩把鎖的介紹,需要的小伙伴可以參考一下2022-05-05實現(xiàn)Servlet程序的三種方法(小結(jié))
這篇文章主要介紹了實現(xiàn)Servlet程序的三種方法(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01