Spring连接数据库的几种常用方法


本文简单的讲解使用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
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
<?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数据源。

需要使c3p0的核心jar包,我使用的是c3p0-0.9.1.jar,比较稳定,推荐使用。一般在下载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
<?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插件连接数据库 需要下载的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
<?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数据源   需要hiberante核心jar包,我使用的hibernate1的版本是hibernate-release-4.3.0.Final  

目前三大框架较流行,spring一般与hiberante做搭档,数据库连接方式写在hiberante的配置文件中,在spring管理hibernate中的配置文件

中,直接读取hibernate核心配置文件即可。在使用hibernate连接数据库的时候需要读取hibernate.cfg.xml的配置文件和相应的实体类,

读者可参照下面的自己配置一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值