在Spring中插入后获取id

时间:2014-11-04 09:28:04

标签: java sql spring jdbctemplate

我尝试在Spring中插入后检索id,但此解决方案不起作用:

String sqlquery2 = "INSERT into tab (attr1,attr2) VALUES (?,?)";
String sql1 ="select last_insert_id()";

jdbcTemplateObject.update(sqlquery2, value1, value2);
int id = jdbcTemplateObject.update(sql1);

2 个答案:

答案 0 :(得分:5)

对于每个jdbc访问,Spring为它的连接池使用连接。

因此它可以使用其池中的一个连接,进行插入,以及last_insert_id查询的另一个连接。

问题是last_insert_id查询与实际插入的连接有关。

为了强制Spring JdbcTemplate使用相同的连接,代码的一部分是在插入之前启动Transaction,并在last_insert_id查询之后提交它。

使用@Transactional

Reference

答案 1 :(得分:1)

为了更好地解释Xstian引用的示例,现在有一些说明已被弃用。 你需要这样做:

<\ n>在Beans.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"
    xmlns:ldap="http://www.springframework.org/schema/ldap"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/ldap 
        http://www.springframework.org/schema/ldap/spring-ldap.xsd">


    <context:property-placeholder location="/config/application.properties" />



    <!-- Initialization for data source JDBC -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- Definition for studentJDBCTemplate bean -->
    <bean id="studentJDBCTemplate" class="com.balduzzi.database.StudentJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>

    <!-- Initialization for TransactionManager -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
  ...

并在Java方法中:

public void insertStudent1(String name, String lastname, String password,String hashpass,String studentype,String codfac) {

          TransactionDefinition def = new DefaultTransactionDefinition();
          TransactionStatus status = transactionManager.getTransaction(def);

        try {
        String sqlinsertab1l = "INSERT into tab1 (type, cod)"
                          +"VALUES (?, ?)";

        jdbcTemplateObject.update(sqlinsertabl, studentype, codfac);


        String sqlgetlastidtab1 = "select max(id) from tab1";

        tab1last_id = jdbcTemplateObject.queryForObject(sqlgetlastidtab1,Integer.class);

        String sqlinsertab2 = "INSERT into tab2 (password,hashpassword) VALUES (?,?)";

        jdbcTemplateObject.update(sqlinsertab2, password, hashpass);

        String sqlgetlastidtab2 = "select max(id) from tab2";

        tab2last_id = jdbcTemplateObject.queryForObject(sqlgetlastidtab2,Integer.class );

        String sqlinsertstud = "INSERT into tab3 (tab1_id,tab2_id,name,lastname) " + "VALUES (?, ?, ?, ?)";

        jdbcTemplateObject.update(sqlinsertstud, tab1last_id, tab2last_id, name, lastname);


        transactionManager.commit(status);
         }
          catch (DataAccessException e) {
              System.out.println("Error in creating record, rolling back");
              transactionManager.rollback(status);
              throw e;
         }
          return;      
  }