spring的關於數據源的datasource接口的深入理解



1.DataSource的接口
這是一個spring接口,可以獲取數據庫的Connection。是標准化的,取得連接的一種方式。

默認市面上有兩個數據庫連接池實現了spring的datasource接口,

分別是apache的dbcp數據庫連接池和c3p0連接池。

 

2.spring對java jdk的jdbc做了深層次的封裝,叫jdbctemplate,在org.springframework.jdbc包下

org.springframework.jdbc.core.JdbcTemplate包下,

JdbcTemplate主要提供以下五類方法:

  • execute方法:可以用於執行任何SQL語句,一般用於執行DDL語句;

  • update方法及batchUpdate方法:update方法用於執行新增、修改、刪除等語句;batchUpdate方法用於執行批處理相關語句;

  • query方法及queryForXXX方法:用於執行查詢相關語句;

  • call方法:用於執行存儲過程、函數相關語句。

 

3.DataSource是數據源,jdbctemplate是操控sql語句的,所以datasource要注入到jdbc之中

DataSource要注入到JdbcTemplate之中。

DataSource要注入到JdbcTemplate之中。

DataSource要注入到JdbcTemplate之中。

 

JdbcTemplate簡介

  Spring對數據庫的操作在jdbc上面做了深層次的封裝,使用spring的注入功能,可以把DataSource注冊到JdbcTemplate之中。

  JdbcTemplate位於中。其全限定命名為org.springframework.jdbc.core.JdbcTemplate。要使用JdbcTemlate還需一個這個包包含了一下事務和異常控制

配置Spring配置文件applicationContext.xml

復制代碼
 1 <context:property-placeholder location="classpath:db.properties"/>
 2 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 3     <property name="user" value="${jdbc.user}"></property>
 4     <property name="password" value="${jdbc.password}"></property>
 5     <property name="driverClass" value="${jdbc.driverClass}"></property>
 6     <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 7 </bean>
 8 
 9 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
10     <property name="dataSource" ref="dataSource"></property>
11 </bean>
復制代碼

  第一行代碼:用來讀取db.properties文件中的數據。

  第二行代碼:用來配置一個數據源,這里數據實現類來自C3P0中的一個屬性類。其中屬性的值就是來自於db.properties

  第九行代碼:配置一個JdbcTemplate實例,並注入一個dataSource數據源

 

 

二。spring操控數據庫的幾種方法

1.spring的自帶jdbctemplate

2.mybatis的sqlsessionFactoryBean或者sqlsessionTemplate

 

 

Spring+JdbcTemplate/Mybatis

 

1.dao組件繼承org.springframework.jdbc.core.support.JdbcDaoSupport 
applicationContext.xml文件中配置

    <util:properties id = "jdbcProperties" location = "classpath:db.properties"></util:properties> <bean id = "myDataSource2" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close"> <property name="driverClassName" value ="#{jdbcProperties.driver}"></property> <property name="url" value="#{jdbcProperties.url}"></property> <property name="username" value="#{jdbcProperties.user}"></property> <property name="password" value="#{jdbcProperties.pwd}"></property> </bean> <bean id = "empDao01" class = "hateapple.dao.EmpDao01"> <property name="dataSource" ref = "myDataSource2"></property> </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

empDao01通過setter注入dataSource,set方法繼承自JdbcDaoSupport,且不能被覆蓋重寫

//set方法簽名 public final void setDataSource(DataSource dataSource)
  • 1
  • 2
  • 1
  • 2

其實就是注入的dataSource被父類JdbcDaoSupport拿去初始化自己的成員變量jdbcTemplate了,empDao01想要使用jdbcTemplate只能通過getJdbcTemplate。

2.不繼承 
org.springframework.jdbc.core.support.JdbcDaoSupport,為empDao02注入jdbcTemplate,empDao02通過jdbcTemplate操作數據庫; 
applicationContext.xml文件配置

    <bean id = "myDataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method = "close"> <property name="driverClassName" value ="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="em"></property> <property name="password" value="em"></property> </bean> <bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref = "myDataSource"></property> </bean> <bean id = "empDao02" class = "hateapple.dao.EmpDao02"> <property name="jdbcTemplate" ref = "jdbcTemplate"></property> </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

對比兩種方式,第一種是把鑰匙交給門衛用的時候向門衛拿,第二種自己帶身上。

Spring+myBatis

1.spring整合mybatis 的核心是 SqlSessionFactoryBean、MapperFactoryBean(單一接口)

org.mybatis.spring.SqlSessionFactoryBean包含了dataSource(數據源)、mapperLocations(接口的mapper映射文件路徑);

org.mybatis.spring.mapper.MapperFactoryBean包含了sqlSessionFactory(上面的bean),mapperInterface(接口的完整名稱);
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref = "myDataSource"></property> <!-- 加載多個可以改為*.xml --> <property name="mapperLocations" value = "classpath:hateapple/mapper/StudentMapperMyBatis.xml"></property> </bean> <!-- hateapple.dao.BaseDao是包含了一個findAll()方法的接口 --> <bean id="studentMapper" class= "org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="hateapple.dao.BaseDao"></property> <property name="sqlSessionFactory" ref ="sqlSessionFactory"></property> </bean> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

測試代碼

        @Test
        public void testMybatis(){ String conf = "applicationContext.xml"; ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf); BaseDao baseDao = (BaseDao)ac.getBean("studentMapper"); List<Student> studentList = baseDao.findAll(); for (Student student : studentList) { System.out.println(student.getName()); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
如果需要多個org.mybatis.spring.mapper.MapperFactoryBean,一個一個配置肯定不現實

2. MapperScannerConfigurer批量掃描接口,並為每個接口生成一個 MapperFactoryBean的實例

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描的接口的包 --> <property name="basePackage" value ="hateapple.dao"></property> <!-- 會話工廠 --> <property name="sqlSessionFactory" ref ="sqlSessionFactory"></property> <!-- 自定義注解,只有被自定義的注解標記的接口才會被掃描 --> <property name="annotationClass" value="hateapple.annotation.MyMapperAnnotation"></property> </bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

MyMapperAnnotation .Java

public @interface MyMapperAnnotation { } 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

BaseDao.java

@MyMapperAnnotation
public interface BaseDao { public List<Student> findAll(); }
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

測試代碼:

        @Test
        public void testMybatis(){ String conf = "applicationContext.xml"; ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf); BaseDao baseDao = (BaseDao)ac.getBean("baseDao"); List<Student> studentList = baseDao.findAll(); for (Student student : studentList) { System.out.println(student.getName()); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

spring+SqlSessionTemplate

其實org.mybatis.spring.mapper.MapperFactoryBean就是封裝了一個SqlSessionTemplate操作數據庫,我們調用baseDao.findAll()最終的操作還是sqlSessionTemplate.selectList(“findAll”)

1.直接為操作數據庫類注入sqlSessionTemplate

<bean id ="sqlSessionTemplate" class = "org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index = "0" ref="sqlSessionFactory"></constructor-arg> </bean>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
    //sqlSessionTemplate是被注入進來的 @Override public List<Student> findAll() { List<Student> studentList = sqlSessionTemplate.selectList("findAll"); for (Student student : studentList) { System.out.println(student.getName()); } return studentList; }
本文部分轉自http://blog.csdn.net/zhaohuijiadelu/article/details/51899080


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM