封装持久层框架

来源:互联网 发布:ei数据库检索入口 编辑:程序博客网 时间:2024/05/18 00:05

 

   一些ORM框架是通过对JDBC封装而形成。  JDBC:建立数据库连接 、 发送操作数据库语句、  处理结果。


  

Connection con = DriverManager.getConnection("jdbc:odbc:wombat","login","password");Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");while (rs.next()) {int x = rs.getInt("a");String s = rs.getString("b");float f = rs.getFloat("c");}

         Spring封装了JDBC,形成了自己针对Dao层的操作,提供了三个主要的类:JDBCDaoSupport 、JdbcTemplate、JdbcAccesor。



说明:

    1、执行数据的操作的是JdbcTemplate
    2、最根本的步骤就是要把dataSource注入到JdbcTemplate
    3、通过给JdbcTemplate注入dataSource
          1、采用构造器的形式注入:jjdbcTemplate类中的jdbcTemplat(DataSource)
          2、采用setter方法进行注入:父类的jdbcAccesor的SetTemplate(DataSource)


<?xml version="1.0" encoding="UTF-8"?><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">  <!--   引入prperties配置文件  propertyplacehodeler 解析properties 文件的类   -->  <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><!-- 第一种方法:start: 向template模板类的子类personDao中注入datasource 属性;因为personDao继承了tmplate模板类,所以参数 datasorce需要被传入 --><bean id="personDao" class="com.itheima12.spring.itheima12.PersonDao"><property name="dataSource"><ref bean="dataSource"/></property></bean><!-- end --><!-- 第二种方法:start:向模板类template中注入DataSource,这样子类personDao就不需要了,直接继承几号了;但是要声明之间的父子关系; --><bean id="itheima12Template" class="com.itheima12.spring.itheima12.Itheima12Template"><property name="dataSource"><ref bean="dataSource"/></property></bean><bean id="personDao2" class="com.itheima12.spring.itheima12.PersonDao" parent="itheima12Template"></bean>   <!-- end --><bean id="personDao3" class="com.itheima12.spring.jdbc.dao.PersonDao"><property name="dataSource"><ref bean="dataSource"/></property></bean></beans>

    4、可以给JdbcDaoSupport注入dataSource (在applicationContext.xml中定义《bean id=jdbcDaosupport》,并且《property》中注入DataSource。)
    5、可以给JdbcDaoSupport注入JdbcTemplate(向JdbcTemplate中注入dataSource,然后将JdbcTemplate注入JdbcDaoSupport中。参考2/3)
 

   6. 封装类继承JdbcDaoSupport,并且调用它getJdbcTempleat().excute(sql)。


public class PersonDao extends JdbcDaoSupport{public void savePerson(String sql){this.getJdbcTemplate().execute(sql);}}


   7.客户端调用:


public class PersonDaoTest {@Testpublic void testSavePerson(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        // personDao3便士封装类PersonDaoPersonDao personDao = (PersonDao)context.getBean("personDao3");personDao.savePerson("insert into person(name,description) values('aa','aa')");}}



 ps:在spring 4 中加载DataSource的方式是这样的:

<!-- 数据库连接池 --><!-- 加载配置文件 --><context:property-placeholder location="classpath:resource/*.properties" /><!-- 数据库连接池 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="driverClassName" value="${jdbc.driver}" /><property name="maxActive" value="10" /><property name="minIdle" value="5" /></bean>8





原创粉丝点击