欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解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)文章

  • mybatis QueryWrapper的條件構(gòu)造之a(chǎn)pply、last、select解析

    mybatis QueryWrapper的條件構(gòu)造之a(chǎn)pply、last、select解析

    這篇文章主要介紹了mybatis QueryWrapper的條件構(gòu)造之a(chǎn)pply、last、select,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 基于Java編寫一個粽子大作戰(zhàn)小游戲

    基于Java編寫一個粽子大作戰(zhàn)小游戲

    端午節(jié),又稱龍舟節(jié)、重午節(jié),是中國的傳統(tǒng)節(jié)日之一,每年農(nóng)歷五月初五慶祝,雖然端午假期已經(jīng)過去了,小編還是用Java編寫了一個粽子大作戰(zhàn)小游戲,感興趣的可以了解一下
    2023-06-06
  • java編程進階小白也能手寫HashMap代碼

    java編程進階小白也能手寫HashMap代碼

    這篇文章是一篇java小白進階篇本文教大家手寫一個HashMap實現(xiàn)的示例代碼,有需要的朋友可以借鑒參考下,希望對大家能夠有所進益,祝大家早日升職加薪
    2021-10-10
  • springboot整合mybatis plus與druid詳情

    springboot整合mybatis plus與druid詳情

    這篇文章主要介紹了springboot整合mybatis plus與druid詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的下伙伴可以參考一下
    2022-09-09
  • Java類加載器ClassLoader源碼層面分析講解

    Java類加載器ClassLoader源碼層面分析講解

    ClassLoader翻譯過來就是類加載器,普通的java開發(fā)者其實用到的不多,但對于某些框架開發(fā)者來說卻非常常見。理解ClassLoader的加載機制,也有利于我們編寫出更高效的代碼。ClassLoader的具體作用就是將class文件加載到j(luò)vm虛擬機中去,程序就可以正確運行了
    2022-09-09
  • java實現(xiàn)代碼統(tǒng)計小程序

    java實現(xiàn)代碼統(tǒng)計小程序

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)代碼統(tǒng)計小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • SpringBoot 集成 ShedLock 分布式鎖的示例詳解

    SpringBoot 集成 ShedLock 分布式鎖的示例詳解

    ShedLock是一個在分布式環(huán)境中使用的定時任務(wù)框架,用于解決在分布式環(huán)境中的多個實例的相同定時任務(wù)在同一時間點重復(fù)執(zhí)行的問題,本文重點給大家介紹SpringBoot 分布式鎖ShedLock的相關(guān)知識,感興趣的朋友一起看看吧
    2021-08-08
  • Java基礎(chǔ)入門之switch怎么使用枚舉

    Java基礎(chǔ)入門之switch怎么使用枚舉

    在Java開發(fā)中,switch語句是一種常用的流控制語句,而當(dāng)使用枚舉類型作為條件時,我們常常會遇到報錯問題,那么該如何解決呢,下面就來詳細(xì)講講
    2023-06-06
  • 關(guān)于json序列化(javaBean轉(zhuǎn)Json的細(xì)節(jié)處理)

    關(guān)于json序列化(javaBean轉(zhuǎn)Json的細(xì)節(jié)處理)

    這篇文章主要介紹了關(guān)于json序列化(javaBean轉(zhuǎn)Json的細(xì)節(jié)處理),具有很好的參考價值,希望對大家有所幫助。
    2022-03-03
  • eclipse端口被占用問題的解決方法

    eclipse端口被占用問題的解決方法

    這篇文章主要給大家介紹了關(guān)于eclipse端口被占用問題的解決方法,文中通過圖文以及命令代碼介紹的非常詳細(xì),對遇到這個問題的朋友們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07

最新評論