发布于 2017-11-30 07:28:34 | 193 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


这篇文章主要为大家详细介绍了spring与jdbc整合操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

先上一段简单示例


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();
  }
}

Dao类


public class PersonDao extends MyTemplate{

  public void savePerson() throws Exception{
    this.insert("insert into person(pid,pname) values(3,'aaa')");
  }
}

spring配置文件


 <!-- 
    引入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();
  }
}

以上代码将DataSource注入给MyTemplate,再把DataSource注入给PersonDao,因为personDao继承MyTemplate,所以拥有Datasource属性。既然PersonDao继承MyTemplate,所以PersonDao类注入可以改为

<bean id="personDao" class="cn.qjc.jdbc.dao.PersonDao" parent="myTemplate"></bean>

以上例子中MyTemplate类似于设计模式中的模板模式也叫模板方法模式,模板方法模式是所有模式中最为常见的几个模式之一,是基于继承的代码复用的基本技术。

  模板模式 = 静态代码+动态变量

在spring中动态变量可以用注入的形式给予。这样的编程方式适合包装成模板。静态代码构成了模板,而动态变量则是需要传入的参数。

spring与jdbc结合核心类JdbcTemplate

  1、基于模板的设置(为什么可以设置成基于模板的形式)

  2、完成了资源的创建和释放的工作

  3、简化为我们对JDBC的操作

  4、完成了对JDBC的核心流程的工作,包括SQL语句的创建和执行

  5、仅需要传递DataSource就可以把它实例化

  6、JdbcTemplate只需要创建一次

  7、JdbcTemplate是线程安全类

使用spring+jdbc修改上面例子(myTemplate类去掉)


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

spring配置文件改为


<bean id="personDao" class="cn.qjc.jdbc.dao.PersonDao">
    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>
</bean>

JdbcTemplate类结构图

执行过程

说明:

    1、执行数据的操作的是JdbcTemplate

    2、最根本的步骤就是要把dataSource注入到JdbcTemplate

    3、通过给JdbcTemplate注入dataSource

           a、采用构造器的形式注入

           b、采用setter方法进行注入

    4、可以给JdbcDaoSupport注入dataSource

    5、可以给JdbcDaoSupport注入JdbcTemplate

所以spring与jdbc整合有三种方法,但实际上核心类为JdbcTemplate

 1、使用JdbcTemplate

       在Dao类中,用JdbcTemplate作为属性,用spring对JdbcTemplate进行注入。再对JdbcTemplate进行DataSource注入。

       注:为什么只要对JdbcTemplate注入DataSource就可以了?

 2、继承jdbcDaoSupport

      在Dao类中,继承JdbcDaoSupport。因为JdbcDaoSupport已经有了JdbcTemplate的引用,所以只要继承JdbcDaoSupport就相当于有了JdbcTemplate属性。

 3、继承JdbcTemplate

spring还提供了其他ORM框架整合模式都差不多,完全可直接套用。

spring+hibernate

spring+Jdo

由此可看出spring IOC 和 DI 的强大,IOC和DI 完成了从接口到类的对应。利用spring容器程序员很容易的在客户端实现面向接口编程,而且很容易给接口装配,结构也可以设置的很灵活。因为接口是自己写的,类也是自己写的,配置文件也是自己写的。spring实际完成了创建对象和装配的工作,它会自动的对应起来。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务