Explain several common ways to connect Spring to a database

  • 2020-05-19 04:58:57
  • OfStack

This article briefly explains several common ways to connect to a database using Spring:

The main test class is:


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(" The user is called :"); 
   System.out.println(rs.getString(2)); 
  }         
}

} 

Type 1: use spring's built-in DriverManagerDataSource configuration file as follows:


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

 

 <!--  use XML Schema the p Namespace configuration  -->

 

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

 

 <!--  using property Common configuration   It's a bit of a hassle , But the effect is 1 Kind of oh ,-->

 

<!--   

 

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

Type 2: C3P0 data source.

Need to make c3p0 core jar package, I used c3p0-0.9.1.jar is relatively stable, recommended to use. 1. Generally, I will bring one with me when downloading hibernate: I found it under the path of hibernate-release-4.3.0. Final\lib\optional\c3p0.

The configuration file is as follows:


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

 

 <!--  use XML Schema the p Namespace configuration   -->

 

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

 

<!--  using property Common configuration   It's a bit of a hassle , But the effect is 1 Kind of oh   The above is recommended -->

 

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

Third:

To connect to the database using apache's dbcp plug-in, you need to download the jar package: commons-dbcp.jar, commons-pool.jar, commons-collection.jar

The configuration file for spring is as follows:


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

 

 <!--  use XML Schema the p Namespace configuration  -->

 

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

  <!--  using property Common configuration   It's a bit of a hassle , But the effect is 1 Kind of oh   The above is recommended -->

 

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

4 types:

Using hibernate data sources requires the hiberante core jar package, and the version of hibernate1 I used is hibernate-release-4.3.0.Final

Currently, the three frameworks are more popular, spring1 and hiberante as partners, database connection is written in hiberante's configuration file, and spring manages hibernate's configuration file

Directly read the hibernate core configuration file. The configuration file of hibernate.cfg.xml and the corresponding entity class need to be read when using hibernate to connect to the database.

Readers can refer to the following configuration 1


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

<!--  All entity class mapping files  --> 
    <list> 
      <value>classpath:com/hibernate/*.hbm.xml</value> 
    </list> 
</property> 


Related articles: