This section is about retrieving auto-generated keys using usingGeneratedKeyColumns" method of SimpleJdbcInsert.
This section is about retrieving auto-generated keys using usingGeneratedKeyColumns" method of SimpleJdbcInsert.The auto-generated keys can be generated using "usingGeneratedKeyColumns" method. The name of the auto generated key column can be specified by passing it's name to method "usingGeneratedKeyColumns". The column must be integer or long and also it must be set for auto increment in database.
The main difference between simple insert and insert using "usingGeneratedKeyColumns" method is that you need not to add the 'id' to the Map. The "executeAndReturnKey" method returns a 'java.lang.Number' object whose numerical instance can be used in our domain class.
SJIautokey.java
package net.roseindia; import java.util.HashMap; import java.util.Map; import javax.sql.DataSource; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; public class SJIautokey { private SimpleJdbcInsert insertRecord; public void setDataSource(DataSource dataSource) { new SimpleJdbcTemplate(dataSource); this.insertRecord = new SimpleJdbcInsert(dataSource).withTableName( "records").usingGeneratedKeyColumns("id"); } public void add(Emp e) { Mapparameters = new HashMap (3); parameters.put("firstname", e.getFirstName()); parameters.put("lastname", e.getLastName()); Number newId = insertRecord.executeAndReturnKey(parameters); e.setId(newId.intValue()); } public Emp newEmp() { Emp emp = new Emp(); emp.setFirstName("Mahima"); emp.setLastName("Singhla"); return emp; } }
SJIautokeyMain.java
package net.roseindia; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class SJIautokeyMain { public static void main(String[] args) { XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource( "SJIautokey.xml")); SJIautokey myBean = (SJIautokey) beanFactory.getBean("SJIautokey"); myBean.add(myBean.newEmp()); } }
SJIautokey.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="SJIautokey" class="net.roseindia.SJIautokey">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://192.168.10.13:3306/ankdb" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
</beans>
After executing code , the rows affected at database table "records" :