最近在搞一個(gè)電商系統(tǒng)中由于業(yè)務(wù)需求,需要在插入一條產(chǎn)品信息后返回產(chǎn)品Id,剛開(kāi)始遇到一些坑,這里做下筆記,以防今后忘記。
類(lèi)似下面這段代碼一樣獲取插入后的主鍵
1
2
3
4
5
6
7
8
|
User user = new User(); user.setUserName( "chenzhou" ); user.setPassword( "xxxx" ); System.out.println( "插入前主鍵為:" +user.getUserId()); userDao.insertAndGetId(user); //插入操作 System.out.println( "插入后主鍵為:" +user.getUserId()); |
經(jīng)過(guò)查詢(xún)網(wǎng)上資料,發(fā)現(xiàn)大致有兩種方式。
方式一:
在實(shí)體類(lèi)的映射文件 "*Mapper.xml" 這樣寫(xiě):
1
2
3
4
|
< insert id = "insertAndGetId" useGeneratedKeys = "true" keyProperty = "userId" parameterType = "com.chenzhou.mybatis.User" > insert into user(userName,password,comment) values(#{userName},#{password},#{comment}) </ insert > |
Tips:
useGeneratedKeys="true" 表示給主鍵設(shè)置自增長(zhǎng)
keyProperty="userId" 表示將自增長(zhǎng)后的Id賦值給實(shí)體類(lèi)中的userId字段。
parameterType="com.chenzhou.mybatis.User" 這個(gè)屬性指向傳遞的參數(shù)實(shí)體類(lèi)
這里提醒下,<insert></insert> 中沒(méi)有resultType屬性,不要亂加。
實(shí)體類(lèi)中uerId 要有g(shù)etter() and setter(); 方法
由于我在MySQL數(shù)據(jù)庫(kù)中建表時(shí)候已經(jīng)設(shè)置了字段自增長(zhǎng),故最終我選擇了第二種方式。
第二種方式:
同樣在實(shí)體類(lèi)的映射文件 "*Mapper.xml" 但是要這樣寫(xiě):
1
2
3
4
5
6
7
|
<!-- 插入一個(gè)商品 --> < insert id = "insertProduct" parameterType = "domain.model.ProductBean" > < selectKey resultType = "java.lang.Long" order = "AFTER" keyProperty = "productId" > SELECT LAST_INSERT_ID() </ selectKey > INSERT INTO t_product(productName,productDesrcible,merchantId)values(#{productName},#{productDesrcible},#{merchantId}); </ insert > |
Tips:
<insert></insert> 中沒(méi)有resultType屬性,但是<selectKey></selectKey> 標(biāo)簽是有的。
order="AFTER" 表示先執(zhí)行插入語(yǔ)句,之后再執(zhí)行查詢(xún)語(yǔ)句。
可被設(shè)置為 BEFORE 或 AFTER。
如果設(shè)置為 BEFORE,那么它會(huì)首先選擇主鍵,設(shè)置 keyProperty 然后執(zhí)行插入語(yǔ)句。
如果設(shè)置為 AFTER,那么先執(zhí)行插入語(yǔ)句,然后是 selectKey 元素-這和如 Oracle 數(shù)據(jù)庫(kù)相似,可以在插入語(yǔ)句中嵌入序列調(diào)用
keyProperty="userId" 表示將自增長(zhǎng)后的Id賦值給實(shí)體類(lèi)中的userId字段。
SELECT LAST_INSERT_ID() 表示MySQL語(yǔ)法中查詢(xún)出剛剛插入的記錄自增長(zhǎng)Id.
實(shí)體類(lèi)中uerId 要有g(shù)etter() and setter(); 方法
實(shí)現(xiàn)需求,上面那些就足夠了。
這里如果有興趣的,請(qǐng)繼續(xù)聽(tīng)我講一個(gè)Mybatis中可能誤入的坑。
為什么Mybatis 中修改添加方法為有返回值后,雖然提示插入數(shù)據(jù)庫(kù)成功并且也能讀取插入的數(shù)據(jù),但是當(dāng)你打開(kāi)數(shù)據(jù)庫(kù)后就是看不到插入的數(shù)據(jù)?
如果在實(shí)現(xiàn)上述需求時(shí),想插入后返回主鍵的話,切記不要這樣寫(xiě)。
1
2
3
4
5
6
7
8
9
10
|
@Override public Long insertProduct(ProductBean productBean) { // TODO Auto-generated method stub SqlSession session = MybatisJDBCUtil.currentSession(); ProductIDao productIDao = session.getMapper(ProductIDao. class ); // 這里*.class // 必須對(duì)應(yīng)DAO的接口層 return productIDao.insertProduct(productBean); } |
Why?
因?yàn)槿绻闶窍裆厦孢@樣寫(xiě),那么執(zhí)行后返回的也不是你想要的主鍵Id,而是執(zhí)行數(shù)據(jù)庫(kù)語(yǔ)句后受影響的行數(shù)。
而且,當(dāng)你執(zhí)行后你會(huì)發(fā)現(xiàn)提示插入成功,你用代碼也可以讀取出插入的數(shù)據(jù),但是永遠(yuǎn)只有一條記錄。
并且,你打開(kāi)數(shù)據(jù)庫(kù)會(huì)發(fā)現(xiàn),數(shù)據(jù)庫(kù)中沒(méi)有插入成功任何數(shù)據(jù)。
我在這里郁悶了N久,終于發(fā)現(xiàn)了關(guān)鍵所在。
有返回值和沒(méi)返回值的區(qū)別在于:
有返回值的只是對(duì)數(shù)據(jù)庫(kù)只讀模式訪問(wèn)數(shù)據(jù)庫(kù),對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)不會(huì)有任何修改,比如各種方式的查詢(xún)。
無(wú)返回值的則會(huì)以讀寫(xiě)模式訪問(wèn)數(shù)據(jù)庫(kù),會(huì)對(duì)數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行修改,比如刪除,增加。
除此之外,根據(jù)個(gè)人理解應(yīng)該mybatis在執(zhí)行插入語(yǔ)句時(shí)會(huì)先緩存到構(gòu)造的一個(gè)類(lèi)似session集合中,然后才去調(diào)用底層驅(qū)動(dòng)去操作修改數(shù)據(jù)庫(kù)。
1
2
|
session.commit(); MybatisJDBCUtil.closeSession(); |
沒(méi)有返回值得才有上面這兩條語(yǔ)句,也就是說(shuō)執(zhí)行了這兩條語(yǔ)句才會(huì)真正執(zhí)行插入到數(shù)據(jù)庫(kù),對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行修改。
相反,有返回值的就沒(méi)有執(zhí)行這兩條語(yǔ)句,所以只是在自己構(gòu)造的session中執(zhí)行了添加語(yǔ)句,但是并沒(méi)有提交到數(shù)據(jù)庫(kù)中,故數(shù)據(jù)庫(kù)中是沒(méi)有任何記錄的。
這也就解釋了為什么Mybatis 中修改添加方法為有返回值后,雖然提示插入數(shù)據(jù)庫(kù)成功,但是當(dāng)你打開(kāi)數(shù)據(jù)庫(kù)看不到插入的數(shù)據(jù)。
Mybatis中插入語(yǔ)句方法種不要有返回值,像這樣寫(xiě)是正確的。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Override public void insertProduct(ProductBean productBean) { // TODO Auto-generated method stub SqlSession session = MybatisJDBCUtil.currentSession(); ProductIDao productIDao = session.getMapper(ProductIDao. class ); // 這里*.class // 必須對(duì)應(yīng)DAO的接口層 productIDao.insertProduct(productBean); session.commit(); MybatisJDBCUtil.closeSession(); } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/xingyunblog/p/6243179.html