spring jdbctemplete应用
时间:2010-08-28 来源:夜色狼
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="userDAO" class="com.xxxxx.dao.BaseDAOImp">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
<property name="tableName" value="users"></property>
<property name="objectClass" value="com.xxxxx.admin.vo.xxx"></property>
<property name="keyColumn" value="id"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref bean="datasource" />
</constructor-arg>
</bean>
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xxxx" />
<property name="username" value="xxxx" />
<property name="password" value="xxxx" />
</bean>
</beans>
就用:
BaseDAO userDAO = null;
try {
userDAO = (BaseDAO) xxx.getBeanFactory().getBean("userDAO");
} catch (Exception e) {
e.printStackTrace();
}
// find all
List list = userDAO.findAll();
System.out.println("user count: " + list.size()
+ ((UserVO) list.get(0)).getName());