当前位置: 首页 > 编程笔记 >

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

百里承业
2023-03-14
本文向大家介绍详解Spring连接数据库的几种常用的方式,包括了详解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> 

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

 类似资料:
  • 本文向大家介绍用 Python 连接 MySQL 的几种方式详解,包括了用 Python 连接 MySQL 的几种方式详解的使用技巧和注意事项,需要的朋友参考一下 尽管很多 NoSQL 数据库近几年大放异彩,但是像 MySQL 这样的关系型数据库依然是互联网的主流数据库之一,每个学 Python 的都有必要学好一门数据库,不管你是做数据分析,还是网络爬虫,Web 开发、亦或是机器学习,你都离不开要

  • 本文向大家介绍详解Java 连接MongoDB集群的几种方式,包括了详解Java 连接MongoDB集群的几种方式的使用技巧和注意事项,需要的朋友参考一下 先决条件 先运行mongodb肯定是必须的,然后导入以下包: MongoClient MongoClient()实例表示到数据库的连接池; 你将只需要MongoClient类的一个实例,即使有多个线程也是一样。 重要: 通常,您只能为给定的Mo

  • 本文向大家介绍详解Mysql导出数据的几种方式,包括了详解Mysql导出数据的几种方式的使用技巧和注意事项,需要的朋友参考一下 MySQL导出数据的目的有很多种,如数据库备份、表结构导出、表数据导出、分析数据采取等。 Part1 select into outfile 先说最短小精悍的select into outfile, 这是小型数据库分析数据最常用的采集数据方式,具体语法如下: 【selec

  • 本文向大家介绍详解python websocket获取实时数据的几种常见链接方式,包括了详解python websocket获取实时数据的几种常见链接方式的使用技巧和注意事项,需要的朋友参考一下 第一种, 使用create_connection链接,需要pip install websocket-client (此方法不建议使用,链接不稳定,容易断,并且连接很耗时) 第二种,运行效果很不错,很容易

  • 本文向大家介绍详解Spring中bean的几种注入方式,包括了详解Spring中bean的几种注入方式的使用技巧和注意事项,需要的朋友参考一下 首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入。依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖。 Spring容器中支持的依赖注入方式主要有属性注入、构造函数注入、工

  • 本文向大家介绍11种ASP连接数据库的方法,包括了11种ASP连接数据库的方法的使用技巧和注意事项,需要的朋友参考一下 ASP连接数据库的11种方法——本文总结了使用ASP链接各种数据库的方法: 1.Access数据库的DSN-less连接方法: 2.Access OLE DB连接方法: 3.SQL server连接方法: 4.SQL server OLE DB连接方法: 5.Oracle 连接方