时间:2021-07-01 10:21:17 帮助过:63人阅读
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- 读取配置文件 -->
<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 >
<!--方法一 继承JdbcDaoSupport -->
<bean id="classesDao" class="cn.itheima03.spring.jdbc.ClassesDaoImpl" >
<property name= "dataSource">
<ref bean= "dataSource"/>
</property>
</bean >
<!--方法二 引入JdbcTemplate,即让JdbcTemplate变成dao的成员变量-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg index= "0" ref="dataSource" ></constructor-arg>
</bean >
<bean id="classesDao2" class="cn.itheima03.spring.jdbc.ClassesDaoImpl2" >
<property name= "jdbcTemplate">
<ref bean= "jdbcTemplate"/>
</property>
</bean >
<!--方法三 继承JdbcTemplate,需要在构造方法中提供数据源 -->
<bean id="classesDao3" class="cn.itheima03.spring.jdbc.ClassesDaoImpl3" >
<constructor-arg index= "0" ref="dataSource" ></constructor-arg>
</bean >
<!-- 方法四 自定义模板 -->
<bean id="itheimaTemplate" class="cn.itheima03.spring.itheima03db.ItHeimaTemplate" >
<constructor-arg index= "0" ref="dataSource" ></constructor-arg>
</bean >
<bean id="classesDao4" class="cn.itheima03.spring.itheima03db.ClassesDaoImpl4" >
<constructor-arg index= "0" ref="dataSource" ></constructor-arg>
</bean >
</beans> ============================================== 位于src目录下的配置文件:jdbc.properties jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql \://localhost\:3306/hibernate_itheima03 jdbc.username=root jdbc.password=root |
/*
* 方式一:继承JdbcDaoSupport,需要使用数据源,在配置文件中应该配置。
*/
public class ClassesDaoImpl extends JdbcDaoSupport implements ClassesDao{
public void saveClasses()
{
this.getJdbcTemplate().execute("insert
into classes(cname,description) values(‘a‘,‘a‘)");
}
@Override public List<Classes> getClasses() { return this .getJdbcTemplate().query("select * from classes", new ClassesRowMapper()) ; } } =================================================== /** * 方式二:引入JdbcTemplate */ public class ClassesDaoImpl2 implements ClassesDao{ private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate ; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void saveClasses() { this.jdbcTemplate .execute("insert into classes(cname,description) values(‘a‘,‘a‘)"); } @Override public List<Classes> getClasses() { return null ; } } =================================================== /** * 方式三:继承JdbcTemplate,写一个以DataSource作为参数的构造函数。 */ public class ClassesDaoImpl3 extends JdbcTemplate implements ClassesDao{ public ClassesDaoImpl3(DataSource dataSource){ super(dataSource); } public void saveClasses() { this.execute("insert into classes(cname,description) values(‘a‘,‘a‘)"); } @Override public List<Classes> getClasses() { return null; } } =================================================== /** * 方式四:自定义模板。 * * 在spring内部完成是面向接口编程 * 在spring的配置文件中,通过set方法或者构造器,给接口注入实现的类 * */ public class ItHeimaTemplate { private DataSource dataSource; public DataSource getDataSource() { return dataSource ; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public ItHeimaTemplate(){} public ItHeimaTemplate(DataSource dataSource){ this.dataSource = dataSource; } /** * 实现插入数据 */ public void insert(String sql){ try { Connection conn = this.d 人气教程排行
|