代碼直接放在Github倉庫【 https://github.com/Damaer/Myb... 】
須要聲明的是:此Mybatis
學習筆記,是從原始的Mybatis
開始的,而不是整合了其餘框架(好比Spring
)以後,我的認爲,這樣能對它的功能,它能幫咱們作什麼,有更好的理解,後面再慢慢疊加其餘的功能。
咱們知道不少時候咱們有一個需求,咱們須要把插入數據後的id返回來,以便咱們下一次操做。java
其實一開始的思路是我插入以後,再執行一次select,根據一個惟一的字段來執行select
操做,可是Student
這個類若是插入後再根據名字或者年齡查出來,這根本就是不可行的!!!重名與同年齡的人必定很多。
咱們的測試方法以下,咱們能夠看到插入前是沒有值的,插入後就有了值:mysql
/** * 測試插入後獲取id */ @Test public void testinsertStudentCacheId(){ Student student=new Student("helloworld",17,85); System.out.println("插入前:student="+student); dao.insertStudentCacheId(student); System.out.println("插入後:student="+student); }
<insert id="insertStudentCacheId" useGeneratedKeys="true" keyProperty="id" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) </insert>
須要注意的點:git
useGeneratedKeys="true"
表示設置屬性自增keyProperty="id"
設置主鍵的字段parameterType="Student"
設置傳入的類型<insert id="insertStudentCacheId" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) <!-- 指定結果類型resultType,keyProperty是屬性,自動返回到屬性id中,order是次序,after是指獲取id是在於插入後 --> <selectKey resultType="int" keyProperty="id" order="AFTER"> select @@identity </selectKey> </insert>
或者寫成:github
<insert id="insertStudentCacheId" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) <!-- 指定結果類型resultType,keyProperty是屬性,自動返回到屬性id中,order是次序,after是指獲取id是在於插入後 --> <selectKey resultType="int" keyProperty="id" order="AFTER"> select LAST_INSERT_ID() </selectKey> </insert>
兩種方式的結果:
[](http://markdownpicture.oss-cn...sql
注意要點:數據庫
<insert></insert>
沒有返回屬性(resultType)
,可是裏面的<selectKey></selectKey>
是有返回值類型的。order="AFTER"
表示先執行插入,以後才執行selectkey
語句的。select @@identity
和select LAST_INSERT_ID()
都表示選出剛剛插入的最後一條數據的id。insert
的那個對象中,咱們能夠直接使用就能夠了。其實,咱們的接口中能夠有返回值,可是這個返回值不是id,而是表示插入後影響的行數,此時sql中仍和上面同樣,不須要寫返回值。apache
<insert id="insertStudentCacheIdNoReturn" parameterType="Student"> insert into student(name,age,score) values(#{name},#{age},#{score}) <!-- 指定結果類型resultType,keyProperty是屬性,自動返回到屬性id中,order是次序,after是指獲取id是在於插入後 --> <selectKey resultType="int" keyProperty="id" order="AFTER"> select LAST_INSERT_ID() </selectKey> </insert>
接口中:markdown
// 增長新學生並返回id返回result public int insertStudentCacheId(Student student);
接口的實現類:mybatis
public int insertStudentCacheId(Student student) { int result; try { sqlSession = MyBatisUtils.getSqlSession(); result =sqlSession.insert("insertStudentCacheId", student); sqlSession.commit(); } finally { if (sqlSession != null) { sqlSession.close(); } } return result; }
Test中:框架
public void testinsertStudentCacheId(){ Student student=new Student("helloworld",17,101); System.out.println("插入前:student="+student); int result = dao.insertStudentCacheId(student); System.out.println(result); System.out.println("插入後:student="+student); }
結果證實:result的值爲1,表示插入了一行,查看數據庫,確實插入了數據。
PS:若是沒法建立鏈接,須要把Mysql
的jar包升級:
<!-- mysql驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency>
若是報如下的錯誤,那麼須要將&改爲轉義後的符號&
:
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 107; 對實體 "serverTimezone" 的引用必須以 ';' 分隔符結尾。
在xml裏面配置須要轉義,不在xml文件裏面配置則不須要
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&serverTimezone=UTC"/>
【做者簡介】:
秦懷,公衆號【秦懷雜貨店】做者,技術之路不在一時,山高水長,縱使緩慢,馳而不息。這個世界但願一切都很快,更快,可是我但願本身能走好每一步,寫好每一篇文章,期待和大家一塊兒交流。
此文章僅表明本身(本菜鳥)學習積累記錄,或者學習筆記,若有侵權,請聯繫做者覈實刪除。人無完人,文章也同樣,文筆稚嫩,在下不才,勿噴,若是有錯誤之處,還望指出,感激涕零~