Spring in Action 笔记(II) part II
Spring in Action 笔记(II) part II
下面是测试Spring提高的Jdbc功能的主要测试类, 测试了一些使用JDBC操作数据的常用功能, 其他没有测试的请查看其Doc,TestJdbcTemplate.java
package test.jdbc; |
下面是函有main函数的 主类. TestApp.java
package test.jdbc;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestApp {
/**
* @param args
*/
public static void main(String[] args) {
DatabaseUtils dataUtils = new DatabaseUtils();
dataUtils.connect();
System.out.println( "Open database:!" );
// TODO Auto-generated method stub
//测试连接数据源 ......
ApplicationContext context = new ClassPathXmlApplicationContext( "test/jdbc/spring-traning.xml" );
/* 测试 DataSource 配置*/
TestDataSource ds = (TestDataSource) context.getBean( "dataBean" );
ds.testDataSource();
/* 测试JdbcTemplate 配置*/
TestJdbcTemplate tjt = (TestJdbcTemplate) context.getBean( "testJdbcTemplate" );
tjt.insertTestData( 2 , "test name2" );
tjt.insertTestDataWithTypeChecked( 3 , "test name 3" );
List<TestData> datas = new ArrayList<TestData>();
datas.add( new TestData( 4 , "test name 4" ));
datas.add( new TestData( 5 , "test name 5" ));
datas.add( new TestData( 6 , "test name 6" ));
tjt.insertTestDataByBatchInsert(datas);
tjt.getTestData( 1 );
tjt.getTestData( 2 );
tjt.getTestData( 5 );
tjt.getDataUsingRowMapper( 5 );
//TODO 类型转换错误. TestData data = tjt.getDataUsingRowMapperResultReader(1);
// System.out.println("测试使用 RowMapperResultSetExtractor 读取一行数据: " + data.id + "::: name " + data.name);
TestData tdata = new TestData( 9 , "TestSqlUpdate." );
tjt.insertDataUsingSqlUpdate(tdata); // 插入数据
tjt.getDataUsingRowMapper( 9 ); // 测试上面插入的数据是否成功
dataUtils.disconnect();
}
}
JDBC配置文件:jdbc.properties
# Sample ResourceBundle properties file
db.username=
addressuserdb.password=
addressuserdb.driver=
org.apache.derby.jdbc.EmbeddedDriverdb.url=
jdbc : derby:db.table=
testdb.schema=
APPdb.urlName=
jdbc : derby:test
最后是最重要的配置文件: spring-traning.xml
<?
xml version = "1.0" encoding = "UTF-8" ?><!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" ><
beans >< bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name = "location" >
< value > test/jdbc/jdbc.properties </ value >
</ property >
</ bean >
<!-- get dataSource,配置dataSource -->
<!-- 从JNDI得到DataSource -->
<!-- bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/myDataSource</value>
</property>
</bean> -->
<!-- 使用Spring中的 DriverManagerDataSource -->
<!-- bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.urlName}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean -->
<!-- 使用DBCP dataSource -->
< bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" >
< property name = "driverClassName" >
< value > ${db.driver} </ value >
</ property >
< property name = "url" >
< value > ${db.urlName} </ value >
</ property >
< property name = "username" >
< value > ${db.username} </ value >
</ property >
< property name = "password" >
< value > ${db.password} </ value >
</ property >
</ bean >
< bean id = "dataBean" class = "test.jdbc.TestDataSource" >
< property name = "dataSource" >
< ref bean = "dataSource" />
</ property >
</ bean >
<!-- ################################################################# -->
<!-- 配合DataSource测试JdbcTemplate -->
< bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" >
< property name = "dataSource" >
< ref bean = "dataSource" />
</ property >
</ bean >
< bean id = "testJdbcTemplate" class = "test.jdbc.TestJdbcTemplate" >
< constructor-arg >
< ref bean = "jdbcTemplate" />
</ constructor-arg >
<!-- 测试 SqlUpdate 接口... -->
< property name = "insertData" >
< ref bean = "insertDataUsingSqlUptate" />
</ property >
</ bean >
<!-- 测试SqlUpdate......接口 -->
< bean id = "insertDataUsingSqlUptate" class = "test.jdbc.InsertData" >
< constructor-arg >
< ref bean = "dataSource" />
</ constructor-arg >
</ bean >
</
beans >
ok, Jdbc测试的代码就结束了.
在这里主要学习了,Spring提高的使用Jdbc的一些包装类和接口, 来更方便的使用Jdbc操作数据, 不用些那么一些烦人的 try ... catch...... finally.....
感觉使用Sprig效率是很好,代码看起来也很优美哦. 呵呵:
ok ,今天就终结完了, 下一次看看在Spring中如何高效使用Hibernate吧, 下次见啦