Spring 鏈接數據庫


一、前言

  Spring 現在是我們在做 JavaWeb 開發中,用的最主流的框架。以后是不是我們暫時不知道,但現在是。廢話不多我就介紹 Spring 中。鏈接數據庫的三種方式: git源碼地址 需要的自行下載。

二、Spring 默認鏈接數據庫方式(java 代碼)

  導入的 JAR 有如下:

  

  Spring 默認的鏈接數據庫代碼:

package com.springjdbc.service;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * 默認 spring 鏈接數據庫的方式
 * @author TongZhou
 *
 */
public class SpringJDBCService {

    /**
     * 使用 Spring 默認的數據庫方式
     */
    @Test
    public void JDBCTest(){
        
        //創建數據庫鏈接的數據源
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        
        //設置數據庫的鏈接信息
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///adminmanger");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        jdbcTemplate.execute("create table user(id int primary key auto_increment,name varchar(20))");
    }
}
View Code

  Test效果:

 

  在數據庫中,生成了表:

  

 

 這是 Spring 在內部集成的 所以它應用的 dataSource 的包名是  import org.springframework.jdbc.datasource.DriverManagerDataSource。我們使用了  Spring 。就不用去在 實例化有關類了,配置就好。那么我們用 Spring 的方式去解決問題。

 

三、Spring 默認鏈接數據庫方式(配置文件)

  配置文件如下: 在項目中 src -->applicationContext.xml  內部代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <!-- 事物的配置 -->
    <!-- spring 中的數據持久層的代碼 -->
    <!-- spring 創建dataSource spring內置的連接池-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///adminmanger"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
</beans>

  測試代碼如下:

package com.springjdbc.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 通過配置文件鏈接數據庫
 * @author TongZhou
 *
 */
// 使用 Spring 的 JUnit 的測試
//通過 ContextConfiguration 讀取配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringJDBCService1 {
    
    @Autowired
    //注入 從數據庫中配置 id="jdbcTemplate"
    @Qualifier("jdbcTemplate")
    //數據庫鏈接對象
    private JdbcTemplate jdbcTemplate;
    
    /**
     * 創建數據庫 
     */
    @Test
    public void dome(){
        
        //執行 SQL
        jdbcTemplate.execute("create table dashuju (id int primary key auto_increment,name varchar(20))");
    }
}
View Code

結果如下:

    

 

四、Spring 鏈接數據庫(dbcp連接池)

    為什么要使用連接池?

    Spring 除了自己可以鏈接數據庫以外,他還引入了第三方的插件如 dbcp 、c3p0 連接池。同時這個連接池也借助了 Spring 這個平台在被廣泛使用。

    dbcp 需要引入的 JAR 有:

      1.  com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
      2. com.springsource.org.apache.commons.pool-1.5.3.jar

    在 XML 中的配置如下:

    

<!-- dbcp連接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///adminmanger"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean> 

   dbcp 和默認的 配置的比較如下:

 

      

五、Spring 鏈接數據庫 (C3P0 連接池)

  配置如下:

<!-- c3p0連接池的使用 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///adminmanger"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

  效果截圖:

      

六、總結

  通過學習 Spring 這四種鏈接數據庫做法,感覺受益匪淺。通過 Spring  的管理,我們可以省略了許多的代碼。一個字就是 “爽”。 git源碼地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM