Inserting auto generated id using mybatis and return the generated id to java
In our previous tutorial of MyBatis, we see how to configure MyBatis in in your project and insert data to database using Mybatis proxy functionality. You have notice that, in real life projects the value of id columns are generated at runtime to identify the object uniquely, most of the time it is generated while the object is created in java. To generate such auto generated id values in java needs extra calculation of finding out a unique id in system and may cause a bottleneck while more data are saved.
So, what will be the solution?
The solution is that, we can get this facility from underlying database that saves data. We can use database specific facilities to generate unique id and then associate that id with the object.
We can use:
- AUTO_INCREMENT facility in MySQL Database
- IDENTITY facility in MS SQL Server.
- A combination of sequence and trigger to generate new unique id in Oracle server.
Each database server has such facility that can be used to generate unique id for new records.
In this tutorial, we will modify our previous example to include auto generated id and return that id to java.
This tutorial is assuming that you have read previous tutorial of Configuring MyBatis 3. If you have not read it, it is recommended to read.
MyBatis version 3.0.4 has a bug in regarding auto generated id, you should use MyBatis version 3.1.0 instead.
Tools Used:
- MyBatis 3.1.0
- MySql Java Connector 5.1
First of all, we have to change the schema of table Product to make the id field auto generated. Following is the DDL command to make the Product table with auto generated id:
Here, I have used AUTO_INCREMENT to generate unique id in MySQL database.
Now, to use auto generated id in our mapping file ProductServices.xml to modify our <insert/> command to use it:
- You can identify three changes that we have to made in <insert/> command tag to use auto generated id and then return the newly generated id to java.
- We have added useGeneratedKeys=”true”, this specify mybatis that the id will be auto generated by database.We also have to specify keyProperty=”id” and keyColumn=”id” which tells the property name of POJO class Product that will be used as ID and also the column name of table product that is used as id.
- We have to remove id field from insert DDL, so that database can generate a new id for the column and put it.
- We have used :
<selectKey keyProperty=”id” resultType=”long” order=”AFTER”>
SELECT LAST_INSERT_ID();
</selectKey>
That will return newly generated id and save it to the id property of POJO class Product. The query written in between <selectKey/> tags is database specific and it may be change according to database. In this example, we have used the query that will work for MySQL database server only.
To test the functionality, we have made a little modification in our runner class:
The new code will not specify the value of id field and also print the new generated id in console.
Run the code and you will get output like this:
You can also try the example yourself. Just download the code from bellow links, import the project in Eclipse and create the product table using given DDL command:
Code + lib: Download
Code: Download
출처 - http://www.raistudies.com/mybatis/inserting-auto-generated-id-using-mybatis-return-id-to-java/
'Framework & Platform > mybatis' 카테고리의 다른 글
Spring3 MVC에서 Mybatis 설정하기 (0) | 2012.05.09 |
---|---|
insert 구문에서 session.commit() 적용 (0) | 2012.05.01 |
mybatis 3 settings 예제 (0) | 2012.04.30 |
mybatis password 암호화/복호화 (0) | 2012.04.28 |
Mybatis selectList 예제 (0) | 2012.03.28 |