国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 13:28:32
字體:
來源:轉載
供稿:網(wǎng)友

本文簡單的講解使用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的配置文件和相應的實體類,

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

<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> 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 扎赉特旗| 鹿泉市| 永昌县| 遂昌县| 方城县| 米易县| 濮阳县| 佛山市| 锦屏县| 巩留县| 西峡县| 尤溪县| 古浪县| 海口市| 西吉县| 治县。| 珲春市| 迁西县| 广灵县| 卫辉市| 海阳市| 米林县| 古浪县| 龙泉市| 门头沟区| 芜湖市| 南城县| 香港| 福州市| 温宿县| 武乡县| 新田县| 丰原市| 绥阳县| 木里| 金平| 南乐县| 大悟县| 茌平县| 忻城县| 内黄县|