详解Spring连接数据库的几种常用的方式

 更新时间:2016年12月09日 09:31:57   作者:@ 小浩  
本篇文章主要介绍了Spring连接数据库的几种常用的方式,具有一定的参考价值,有需要的可以了解一下。

本文简单的讲解使用Spring连接数据库的几种常用方法:

测试主类为:

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数据源。

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

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

中,直接读取hibernate核心配置文件即可。在使用hibernate连接数据库的时候需要读取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> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • mybatis QueryWrapper的条件构造之apply、last、select解析

    mybatis QueryWrapper的条件构造之apply、last、select解析

    这篇文章主要介绍了mybatis QueryWrapper的条件构造之apply、last、select,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 基于Java编写一个粽子大作战小游戏

    基于Java编写一个粽子大作战小游戏

    端午节,又称龙舟节、重午节,是中国的传统节日之一,每年农历五月初五庆祝,虽然端午假期已经过去了,小编还是用Java编写了一个粽子大作战小游戏,感兴趣的可以了解一下
    2023-06-06
  • java编程进阶小白也能手写HashMap代码

    java编程进阶小白也能手写HashMap代码

    这篇文章是一篇java小白进阶篇本文教大家手写一个HashMap实现的示例代码,有需要的朋友可以借鉴参考下,希望对大家能够有所进益,祝大家早日升职加薪
    2021-10-10
  • springboot整合mybatis plus与druid详情

    springboot整合mybatis plus与druid详情

    这篇文章主要介绍了springboot整合mybatis plus与druid详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的下伙伴可以参考一下
    2022-09-09
  • Java类加载器ClassLoader源码层面分析讲解

    Java类加载器ClassLoader源码层面分析讲解

    ClassLoader翻译过来就是类加载器,普通的java开发者其实用到的不多,但对于某些框架开发者来说却非常常见。理解ClassLoader的加载机制,也有利于我们编写出更高效的代码。ClassLoader的具体作用就是将class文件加载到jvm虚拟机中去,程序就可以正确运行了
    2022-09-09
  • java实现代码统计小程序

    java实现代码统计小程序

    这篇文章主要为大家详细介绍了java实现代码统计小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • SpringBoot 集成 ShedLock 分布式锁的示例详解

    SpringBoot 集成 ShedLock 分布式锁的示例详解

    ShedLock是一个在分布式环境中使用的定时任务框架,用于解决在分布式环境中的多个实例的相同定时任务在同一时间点重复执行的问题,本文重点给大家介绍SpringBoot 分布式锁ShedLock的相关知识,感兴趣的朋友一起看看吧
    2021-08-08
  • Java基础入门之switch怎么使用枚举

    Java基础入门之switch怎么使用枚举

    在Java开发中,switch语句是一种常用的流控制语句,而当使用枚举类型作为条件时,我们常常会遇到报错问题,那么该如何解决呢,下面就来详细讲讲
    2023-06-06
  • 关于json序列化(javaBean转Json的细节处理)

    关于json序列化(javaBean转Json的细节处理)

    这篇文章主要介绍了关于json序列化(javaBean转Json的细节处理),具有很好的参考价值,希望对大家有所帮助。
    2022-03-03
  • eclipse端口被占用问题的解决方法

    eclipse端口被占用问题的解决方法

    这篇文章主要给大家介绍了关于eclipse端口被占用问题的解决方法,文中通过图文以及命令代码介绍的非常详细,对遇到这个问题的朋友们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07

最新评论