Last active
August 29, 2015 14:14
-
-
Save chenrui333/909d1ea9da137aca7dab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Test Class, JdbcTest.java | |
package test; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.jdbc.core.PreparedStatementCreator; | |
import org.springframework.jdbc.support.GeneratedKeyHolder; | |
import org.springframework.jdbc.support.KeyHolder; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration({"classpath:so-config.xml"}) | |
public class JdbcTest { | |
@Autowired | |
private JdbcTemplate jdbcTemplate; | |
@Test | |
public void test(){ | |
Long key = 1L; | |
Long deptNumber = 2L; | |
String widgetType = "type"; | |
Long creatorId = 3L; | |
final StringBuilder widgetInsert = new StringBuilder(); | |
widgetInsert.append("INSERT INTO WIDGET (ID, KEY, DEPT_NUM, TYPE, CREATED_BY_ID, CREATED_DATE) "); | |
widgetInsert.append("VALUES (WIDGET_SEQ.NEXTVAL, ?, ?, ?, ?, ?) "); | |
Long widgetId; | |
try { | |
KeyHolder kh = new GeneratedKeyHolder(); | |
jdbcTemplate.update( | |
new PreparedStatementCreator() { | |
@Override | |
public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | |
PreparedStatement ps = con.prepareStatement(widgetInsert.toString()); | |
ps.setLong(1, key); | |
ps.setLong(2, deptNumber); | |
ps.setString(3, widgetType); | |
ps.setLong(4, creatorId); | |
ps.setLong(5, System.currentTimeMillis()); | |
return ps; | |
} | |
}, kh); | |
widgetId = kh.getKey().longValue(); | |
} catch (Exception e) { | |
// THIS EXCEPTION IS THROWN | |
throw new RuntimeException("Problem inserting widget for key=" + key, e); | |
} | |
} | |
} | |
## H2 schema, sql/so-schema.sql | |
create sequence WIDGET_SEQ; | |
create table widget( | |
id long primary key, | |
key long, | |
dept_num long, | |
type varchar(20), | |
created_by_id long, | |
created_date long | |
); | |
## XML config, test-config.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:jdbc="http://www.springframework.org/schema/jdbc" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> | |
<!-- embedded in-memory H2 database for testing --> | |
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> | |
<property name="driverClassName" value="org.h2.Driver"/> | |
<property name="url" value="jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;"/> | |
</bean> | |
<!-- insert lookup data and other data needed for testing into the in-memory H2 database --> | |
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS"> | |
<jdbc:script location="classpath:sql/so-schema.sql"/> | |
</jdbc:initialize-database> | |
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> | |
<property name="dataSource" ref="dataSource"/> | |
</bean> | |
</beans> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment