最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring連接數(shù)據(jù)庫的幾種常用的方式

 更新時間:2016年12月09日 09:31:57   作者:@ 小浩  
本篇文章主要介紹了Spring連接數(shù)據(jù)庫的幾種常用的方式,具有一定的參考價值,有需要的可以了解一下。

本文簡單的講解使用Spring連接數(shù)據(jù)庫的幾種常用方法:

測試主類為:

package myspring2;
import java.sql.*;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MySpringTest {
 public static void main(String args[]) throws Exception{ 
  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");  
  DataSource dataSource=ctx.getBean("dataSource",DataSource.class);
 String sql="select * from user_inf";  
 Connection connection=dataSource.getConnection(); 
  Statement stm=connection.createStatement();  
 ResultSet rs=stm.executeQuery(sql); 
  while(rs.next())   
{    System.out.println("用戶名為:"); 
   System.out.println(rs.getString(2)); 
  }         
}

} 

第一種:使用spring自帶的DriverManagerDataSource   配置文件如下:

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

 xsi:schemaLocation="

     http://www.springframework.org/schema/beans  

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

 

      http://www.springframework.org/schema/tx    

 

   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

 

      http://www.springframework.org/schema/context

 

      http://www.springframework.org/schema/context/spring-context-3.0.xsd

 

      http://www.springframework.org/schema/aop

 

      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 

 <!-- 使用XML Schema的p名稱空間配置 -->

 

 <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" 

 

 p:driverClassName="com.mysql.jdbc.Driver" 

 

 p:url="jdbc:mysql://localhost:3306/test"

 

 p:username="root"

 

 p:password="123456" / > 

 

 <!-- 采用property的普通配置 相比之下有點麻煩,但是效果是一樣的哦,-->

 

<!--   

 

 <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

 

  <property name="driverClassName" value="com.mysql.jdbc.Driver" />

 

   <property name="url" value="jdbc:mysql://localhost:3306/test" />

   <property name="username" value="root" />

   <property name="password" value="123456" />

  </bean>

  -->  

</beans> 

 第二種:C3P0數(shù)據(jù)源。

需要使c3p0的核心jar包,我使用的是c3p0-0.9.1.jar,比較穩(wěn)定,推薦使用。一般在下載hibernate的時候都會自帶一個: 我在hibernate-release-4.3.0.Final\lib\optional\c3p0路徑下找到的。

配置文件中如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

 

xmlns:p="http://www.springframework.org/schema/p"

 

 xsi:schemaLocation="

 

     http://www.springframework.org/schema/beans  

 

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

 

      http://www.springframework.org/schema/tx   

 

   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

 

      http://www.springframework.org/schema/context

 

      http://www.springframework.org/schema/context/spring-context-3.0.xsd

 

      http://www.springframework.org/schema/aop

 

      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 

 <!-- 使用XML Schema的p名稱空間配置  -->

 

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 

 

  p:driverClass="com.mysql.jdbc.Driver" 

 

  p:jdbcUrl="jdbc:mysql://localhost:3306/test"

 

  p:user="root"

 

  p:password="123456" >    

 

</bean>  

 

<!-- 采用property的普通配置 相比之下有點麻煩,但是效果是一樣的哦 建議使用上面的-->

 

<!--    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 

 

      <property name="driverClass" value="com.mysql.jdbc.Driver" />  

 

      <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />

 

      <property name="user" value="root" />

 

      <property name="password" value="123456" />

 

      </bean>

 

 -->  

 

 </beans> 

第三種:

使用apache的dbcp插件連接數(shù)據(jù)庫 需要下載的jar包:commons-dbcp.jar,commons-pool.jar,commons-collection.jar

spring的配置文件中如下:

<?xml version="1.0" encoding="UTF-8"?> 

 

<beans xmlns="http://www.springframework.org/schema/beans" 

 

xmlns:aop="http://www.springframework.org/schema/aop"

 

xmlns:tx="http://www.springframework.org/schema/tx"

 

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 

xmlns:context="http://www.springframework.org/schema/context"

 

 xmlns:p="http://www.springframework.org/schema/p" 

 

xsi:schemaLocation="    

 

  http://www.springframework.org/schema/beans 

 

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

 

    http://www.springframework.org/schema/tx   

 

    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  

 

    http://www.springframework.org/schema/context 

 

     http://www.springframework.org/schema/context/spring-context-3.0.xsd

 

      http://www.springframework.org/schema/aop 

 

     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 

 <!-- 使用XML Schema的p名稱空間配置 -->

 

  <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

 

 p:driverClassName="com.mysql.jdbc.Driver" 

 

p:url="jdbc:mysql://localhost:3306/test"

 

 p:username="root"

 

 p:password="123456" > 

 

</bean>

  <!-- 采用property的普通配置 相比之下有點麻煩,但是效果是一樣的哦 建議使用上面的-->

 

<!--    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 

 

  <property name="driverClassName" value="com.mysql.jdbc.Driver" />  

 

 <property name="url" value="jdbc:mysql://localhost:3306/test" />

 

   <property name="username" value="root" /> 

 

  <property name="password" value="123456" /> 

 

  </bean>

  -->  

 </beans> 

第四種:

使用hibernate數(shù)據(jù)源   需要hiberante核心jar包,我使用的hibernate1的版本是hibernate-release-4.3.0.Final 

目前三大框架較流行,spring一般與hiberante做搭檔,數(shù)據(jù)庫連接方式寫在hiberante的配置文件中,在spring管理hibernate中的配置文件

中,直接讀取hibernate核心配置文件即可。在使用hibernate連接數(shù)據(jù)庫的時候需要讀取hibernate.cfg.xml的配置文件和相應(yīng)的實體類,

讀者可參照下面的自己配置一下

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
 <property name="configLocations"> 
  <list> 
   <value>classpath:com/config/hibernate.cfg.xml</value> 
  </list> 
 </property> 
  <property name="mappingLocations">  

<!-- 所有的實體類映射文件 --> 
    <list> 
      <value>classpath:com/hibernate/*.hbm.xml</value> 
    </list> 
</property> 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用PageHelper插件實現(xiàn)Service層分頁

    使用PageHelper插件實現(xiàn)Service層分頁

    這篇文章主要為大家詳細介紹了使用PageHelper插件實現(xiàn)Service層分頁,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java httpClient介紹以及使用示例

    Java httpClient介紹以及使用示例

    這篇文章主要介紹了Java httpClient介紹以及使用示例,幫助大家更好的利用Java實現(xiàn)HTTP請求,感興趣的朋友可以了解下
    2020-10-10
  • 關(guān)于ConditionalOnMissingBean失效問題的追蹤

    關(guān)于ConditionalOnMissingBean失效問題的追蹤

    這篇文章主要介紹了關(guān)于ConditionalOnMissingBean失效問題的追蹤方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java lastIndexOf類使用方法原理解析

    Java lastIndexOf類使用方法原理解析

    這篇文章主要介紹了Java lastIndexOf類使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • java與C 代碼運行效率的對比(整理)

    java與C 代碼運行效率的對比(整理)

    最近和朋友無意間討論起了 有關(guān)java 和C 的 效率問題, (我是java 推介者, 他是 c 語言推介者, 他做的是嵌入式)故,想通過網(wǎng)絡(luò)查詢一下, 總結(jié)一下,兩者到底效率如何,其有何差異,原因又是啥?各種優(yōu)勢有在何處?
    2021-04-04
  • Java實現(xiàn)的迷宮游戲

    Java實現(xiàn)的迷宮游戲

    這篇文章主要介紹了如何用Java實現(xiàn)一個迷宮游戲,本倉庫代碼是經(jīng)過eclipse編譯運行過的,一般情況下將本倉庫代碼下載下來之后,使用eclipse編譯直接可以運行。
    2021-04-04
  • java線程池ExecutorService超時處理小結(jié)

    java線程池ExecutorService超時處理小結(jié)

    使用ExecutorService時,設(shè)置子線程執(zhí)行超時是一個常見需求,本文就來詳細的介紹一下ExecutorService超時的三種方法,感興趣的可以了解一下
    2024-09-09
  • 使用idea搭建一個spring mvc項目的圖文教程

    使用idea搭建一個spring mvc項目的圖文教程

    這篇文章主要介紹了使用idea直接創(chuàng)建一個spring mvc項目的圖文教程,本文通過圖文并茂的方式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot集成Redisson實現(xiàn)分布式鎖的方法示例

    SpringBoot集成Redisson實現(xiàn)分布式鎖的方法示例

    這篇文章主要介紹了SpringBoot集成Redisson實現(xiàn)分布式鎖的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java的StringBuilder在高性能場景下的正確用法

    Java的StringBuilder在高性能場景下的正確用法

    StringBuilder?對字符串的操作是直接改變字符串對象本身,而不是生成新的對象,所以新能開銷小.與StringBuffer相比StringBuilder的性能略高,StringBuilder則沒有保證線程的安全,從而性能略高于StringBuffer,需要的朋友可以參考下
    2023-05-05

最新評論

通江县| 南平市| 揭东县| 东平县| 和硕县| 马公市| 峨边| 漯河市| 新余市| 南开区| 城步| 江川县| 蕉岭县| 张家港市| 宜都市| 伊春市| 临泽县| 沭阳县| 璧山县| 义马市| 教育| 永州市| 出国| 苏尼特右旗| 房产| 嘉善县| 永兴县| 博罗县| 武功县| 葵青区| 宣武区| 汾阳市| 晋中市| 惠安县| 赤壁市| 衡阳市| 贡嘎县| 巴塘县| 广安市| 庆云县| 定安县|