一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 詳解Spring連接數(shù)據(jù)庫的幾種常用的方式

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

2020-07-11 12:12@ 小浩 JAVA教程

本篇文章主要介紹了Spring連接數(shù)據(jù)庫的幾種常用的方式,具有一定的參考價值,有需要的可以了解一下。

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

測試主類為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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   配置文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?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路徑下找到的。

配置文件中如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?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的配置文件中如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?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的配置文件和相應的實體類,

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/xiohao/p/3663619.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 动漫人物差差差动漫人物免费观看 | 99视频久久 | 色综合图区 | 97国产自拍 | 美女被躁了在线观看视频 | 成人福利网 | 男人与雌性宠物交啪啪小说 | 欧美高清videosex极品 | 四虎成人永久地址 | 国内精品中文字幕 | 手机看片国产免费久久网 | 日本免费观看的视频在线 | www.色小妹 | 亚洲免费小视频 | 青春草视频在线免费观看 | 日韩免费在线看 | 亚洲国产精品综合久久一线 | 九九99热久久精品在线6 | chinese老太granny chinese国产人妖hd | 亚洲精美视频 | 小小水蜜桃视频高清在线观看免费 | 成人国产精品一级毛片视频 | 欧美综合亚洲图片综合区 | 国产91精选在线观看麻豆 | 亚洲第一男人网站 | 欧美成人在线影院 | a免费看 | 精品亚洲永久免费精品 | 精品免费看 | 国产午夜久久精品 | 毛片啪啪视频 | 经典三级四虎在线观看 | 无套日出白浆在线播放 | 午夜福利视频极品国产83 | 放荡护士玩3p口述 | 大陆国产精品视频 | 亚洲第99页 | 成人影院免费看 | 欧美ⅹxxxx视频 | chinese国产老太性 | 国内精品久久久久小说网 |