Spring 与JDBC结合
来源:互联网 发布:linux空间扩容 编辑:程序博客网 时间:2024/05/21 11:06
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"><beanclass="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 {@Testpublic 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 {@Testpublic 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>配合写--><beanclass="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>
0 0
- Spring 与JDBC结合
- spring与jdbc的结合使用
- spring与jdbc结合的方式
- spring与jdbc的结合的示例
- Spring学习心得(21)-- spring与jdbc结合的方式
- spring(数据库)jdbc配置、spring与jdbc编程的结合和jdbc模板方式的使用
- 结合spring+hibernate与jdbc的事务,From 荣浩
- struts2与spring、jdbc结合以及AOP事务管理的配置
- spring与jdbc结合的配置和实现
- Spring与JDBC整合应用+一个Springjdbc与mvc结合的例子
- Spring学习第二天——代理对象,AOP,Spring与JDBC结合,spring要与hibernate版本对应
- ajax与spring 结合
- struts2与spring结合
- struts2与spring结合
- flex与Spring结合
- strust与spring结合
- Spring与Struts结合
- ibatis-----与Spring结合
- JQuery 1.7 笔记
- 标准C++中的类型定义
- java SE复习笔记53
- HNOI 2002 彩票
- epoll是如何监控多个描述符及如何获得通知(2) http://blog.chinaunix.net/uid-23629988-id-3575499.html?/gov-17311/
- Spring 与JDBC结合
- BIOS入门之我见-输入
- java SE复习笔记54
- epoll是如何监控多个描述符及如何获得通知(1) http://blog.chinaunix.net/uid-23629988-id-3569332.html
- C++编程思想
- DOM解析
- 游戏节目
- 高性能网络I/O框架-netmap源码分析(1) http://blog.chinaunix.net/uid-23629988-id-3594118.html
- POJ 3321:Apple Tree 树状数组