public class MyTemplate {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(String sql) throws SQLException{
Connection conn = this.dataSource.getConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
}
public class PersonDao extends MyTemplate{
public void savePerson() throws Exception{
this.insert("insert into person(pid,pname) values(3,'aaa')");
}
}
<!--
引入properties配置文件
-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="myTemplate" class="cn.qjc.jdbc.dao.MyTemplate">
<!-- setter注入 -->
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personDao" class="cn.qjc.jdbc.dao.PersonDao">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
</beans>
public class PersonDaoTest {
@Test
public void testPersonDao() throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("cn/qjc/jdbc/applicationContext.xml");
PersonDao personDao = (PersonDao)context.getBean("personDao");
personDao.savePerson();
}
}
public class PersonDao extends JdbcDaoSupport {
public void savePerson(String sql){
this.getJdbcTemplate().execute(sql);
}
}
<bean id="personDao" class="cn.qjc.jdbc.dao.PersonDao">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有