Spring + JDBC
1、JDBC编程的特点:
类似于模板编程:即,固定代码+动态参数
创建一个类,命名为 JdbcTemplate,用来负责“模板编程”
JDBCTemplate{ // 模板编程
private DataSource dataSource;
public void setDataSource(DataSource dataSource){
this.dataSource = dataSource;
}
public void update(String sql){ //用来负责操作DB
//产生链接
//产生 Statement
// 执行executeUpdate方法
}
}
2、演示Spring操作Jdbc的思路:建文件夹 spring_jdbc/cn.google.spring.jdbc.JdbcTemplate.java,将上面方法补充完全
import javax.sql.DataSource;
public class JdbcTemplate{
private DataSource dataSource;
封装……
public void update(String sql){
Connection conn;
try{
conn = dataSource.getConnection();
Statement stat = conn.createStatement();
stat.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}
}
}
3、在 spring_jdbc/cn.google.spring.jdbc.applicationContext.xml 中
<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>
<bean id="jdbcTemplate" class="cn.google.spring.jdbc.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="personDao" class="cn.google.spring.jdbc.PersonDao">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
</beans>
4、在spring_jdbc/jdbc.properties 中配置链接池属性
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/springaop
jdbc.username=root
jdbc.password=root
5、spring_jdbc/cn.itcast.spring.jdbc.中 创建 PersonDao
public class PersonDao extends JdbcTemplate{
public void update(){
this.update("update person set pname='a' where pid=2 ");
}
}
6、测试类 PersonTest
public class PersonTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("cn/google/spring/jdbc/applicationContext.xml");
PersonDao personDao = (PersonDao)context.getBean("personDao");
personDao.update();
}
}
结果:把pid=2 的pname值改为 a
-------------------------------------------------------------------------------------------------------------------------------
·Spring中有一个与 JDBC相关联的功能类叫 JDBCDaoSupport,它是一个抽象类,下面是一部分“源码”:
public abstract class JdbcDaoSupport extends DaoSupport{
private JdbcTemplate jdbcTemplate;
// 此处是利用 DI 的方式为DataSource注入值
public final void setDataSource(DataSource dataSource){
if(this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()){
this.jdbcTemplate = createJdbcTemplate(dataSource);
initTemplateConfig();
}
}
protected JdbcTemplate createJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
// 此处是利用 DI 的方式为 JdbcTemplate 注入值
public final void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
initTemplateConfig();
}
protected JdbcTemplate createJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
·还有一个很重要的类叫 JdbcTemplate,因为它可以通过代参构造函数给DataSource属性赋值。
Spring 把操作Jdbc的方法都封装进 JdbcTemplate 这个类中。
public class JdbcTemplate extends JdbcAccessor(){
public jdbcTemplate(){
}
public jdbcTemplate(DataSource dataSource){
setDataSource(dataSource);
}
// 在JdbcTemplate 中封装了很多的 jdbc 的操作。
}
public abstract class JdbcAccessor{
private DataSource dataSource;
public void setDataSource(DataSource dataSource){
this.dataSource= dataSource;
}
}
总结:spring + JDBC 结构总共三种
1、让自己写的一个dao类继承JdbcDaoSupport
2、让自己写的一个dao类继承JdbcTemplate
3、让自己写的一个dao类里有一个属性为JdbcTemplate
方式1、令PersonDao 继承 JdbcDaoSupport
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class PersonDao extends JdbcDaoSupport{
public void update(){
this.getJdbcTemplate().execute("update person set pname='a' where pid=3");
}
}
运行测试类
public class PersonTest{
@Test
public void test(){
ApplicationContext context = new ClassPathApplicationContext("applicationContext.xml");
PersonDao personDao = (PersonDao)context.getBean("personDao");
personDao.update( );
}
}
结果:
pid =3 的数据被update了。
方式2、令 PersonDao2 继承 JdbcTemplate
import org.springframework.jdbc.core.JdbcTemplate;
public class PersonDao2 extends JdbcTemplate{
public PersonDao2(DataSource dataSource){
super(dataSource);
}
public void update(){
this.execute("update person set pname='aa' where pid=2");
}
}
同时修改 applicationContext.xml
<bean id="personDao2" class="cn.google.spring.jdbc.PersonDao2">
<constructor-arg index="0" ref="dataSource"></constructor-arg>
</bean>
运行测试
结果:pid为2的数据变化了。
方式3、建PersonDao3,令 JdbcTemplate作为这个类中的而一个属性。
import org.springframework.jdbc.core.JdbcTemplate;
public class PersonDao3{
private JdbcTemplate jdbcTemplate;
封装……
public void update(){
this.jdbcTemplate.execute("update person set pname='aaa' where pid=2");
}
}
同时修改 applicationContext.xml:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="personDao3" class="cn.google.spring.jdbc.PersonDao3">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
·测试类 PersonTest
public class PersonTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("cn/google/spring/jdbc/applicationContext.xml");
PersonDao personDao = (PersonDao)context.getBean("personDao2");
personDao.query();
}
}
1、Spring中有两种引入dataSource的方式:
1)在dataSource的设置中直接写值
2)引入properties 文件(这种比较好)
<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">
<!-- 负责解析properties文件的,必须和 DataSource的<bean>配合写-->
<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>
</beans>