Java Spring-JdbcTemplate

來源:互聯網
上載者:User

標籤:orm   rem   配置   dem   new   協助   arch   junit   代碼   

2017-11-10 22:55:45

Spring 對持久層支援人員 :

  • JDBC : org.springframework.jdbc.core.JdbcTemplate
  • Hibernate3.0 : org.springframework.orm.hibernate3.HibernateTemplate
  • IBatis(MyBatis) : org.springframework.orm.ibatis.SqlMapClientTemplate
  • JPA : org.springframework.orm.jpa.JpaTemplate
public class Jdbc1 {    @Test    public void demo(){        // 建立串連池        DriverManagerDataSource dataSource = new DriverManagerDataSource();        // 配置參數        dataSource.setDriverClassName("com.mysql.jdbc.Driver");        dataSource.setUrl("jdbc:mysql://localhost:3306/testdb");        dataSource.setUsername("root");        dataSource.setPassword("hy1102");        // 使用Jdbc模板        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);        jdbcTemplate.execute("create table user2 (id int primary key auto_increment,name varchar(20))");    }}

顯然每次在程式中用代碼建立串連池是很不方便的,於是我們可以使用Spring來協助我們對這個類進行配置。

常用的資料來源有三種:

  • spring資料來源實作類別,DriverManagerDataSource;
  • DBCP資料來源,BasicDataSource;
  • C3P0資料來源,ComboPooledDataSource;

一、DriverManagerDataSource的配置

設定檔:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--配置串連池-->    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://localhost:3306/testdb"/>        <property name="username" value="host"/>        <property name="password" value="hy1102"/>    </bean>    <!--定義模板-->    <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="datasource"/>    </bean></beans>

測試類別:

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:config4.xml")public class Jdbc2 {    @Resource(name = "jdbctemplate")    private JdbcTemplate jt;    @Test    public void demo(){        jt.execute("...");    }}

 

二、DBCP資料來源,BasicDataSource的配置

首先需要引入兩個jar包,也就是commons.dbcp 和 commons.pool。和上一種方法略有區別,但差別很小。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--配置串連池-->    <!--<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->        <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->        <!--<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>-->        <!--<property name="username" value="host"/>-->        <!--<property name="password" value="hy1102"/>-->    <!--</bean>-->    <!-- 配置DBCP串連池 -->    <bean id="datasource" class=" org.apache.commons.dbcp.BasicDataSource ">        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>        <property name="url" value="jdbc:mysql://localhost:3306/testdb"/>        <property name="username" value="host"/>        <property name="password" value="hy1102"/>    </bean>    <!--定義模板-->    <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="datasource"/>    </bean></beans>

 

三、C3P0資料來源,ComboPooledDataSource的配置

首先先引入jar包c3p0-0.9.1.2.jar。

C3P0的配置參數和前兩個不太一樣,這個要注意。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--配置串連池-->    <!--<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->    <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->    <!--<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>-->    <!--<property name="username" value="host"/>-->    <!--<property name="password" value="hy1102"/>-->    <!--</bean>-->    <!-- 配置DBCP串連池 -->    <!--<bean id="datasource" class=" org.apache.commons.dbcp.BasicDataSource ">-->        <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->        <!--<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>-->        <!--<property name="username" value="host"/>-->        <!--<property name="password" value="hy1102"/>-->    <!--</bean>-->    <!--  配置 c3p0 串連池 -->    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name=" driverClass " value="com.mysql.jdbc.Driver"/>        <property name=" jdbcUrl " value="jdbc:mysql://localhost:3306/testdb"/>        <property name=" user " value="host"/>        <property name=" password " value="hy1102"/>    </bean>    <!--定義模板-->    <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="datasource"/>    </bean></beans>

 

四、將參數設定寫到屬性檔案中

方法一:

在src檔案夾下建立jdbc.properties檔案

jdbc.driver = com.mysql.jdbc.Driverjdbc.url = jdbc:mysql://localhost:3306/testdbjdbc.user = rootjdbc.password = hy1102

設定檔:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--配置串連池-->    <!--<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->    <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->    <!--<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>-->    <!--<property name="username" value="host"/>-->    <!--<property name="password" value="hy1102"/>-->    <!--</bean>-->    <!-- 配置DBCP串連池 -->    <!--<bean id="datasource" class=" org.apache.commons.dbcp.BasicDataSource ">-->        <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->        <!--<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>-->        <!--<property name="username" value="host"/>-->        <!--<property name="password" value="hy1102"/>-->    <!--</bean>-->    <!--引入該屬性檔案-->    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">        <property name="location" value="classpath:jdbc.properties"/>    </bean>    <!--  配置 c3p0 串連池 -->    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name=" driverClass " value="${jdbc.driver}"/>        <property name=" jdbcUrl " value="${jdbc.url}"/>        <property name=" user " value="${jdbc.user}"/>        <property name=" password " value="${jdbc.password}"/>    </bean>    <!--定義模板-->    <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="datasource"/>    </bean></beans>

 方法二:

使用context標籤。

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <!--配置串連池-->    <!--<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->    <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->    <!--<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>-->    <!--<property name="username" value="host"/>-->    <!--<property name="password" value="hy1102"/>-->    <!--</bean>-->    <!-- 配置DBCP串連池 -->    <!--<bean id="datasource" class=" org.apache.commons.dbcp.BasicDataSource ">-->        <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->        <!--<property name="url" value="jdbc:mysql://localhost:3306/testdb"/>-->        <!--<property name="username" value="host"/>-->        <!--<property name="password" value="hy1102"/>-->    <!--</bean>-->    <!--引入該屬性檔案-->    <!--<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">-->        <!--<property name="location" value="classpath:jdbc.properties"/>-->    <!--</bean>-->    <!--使用context標籤引入屬性檔案-->    <context:property-placeholder location="classpath:jdbc.properties"/>    <!--  配置 c3p0 串連池 -->    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name=" driverClass " value="${jdbc.driver}"/>        <property name=" jdbcUrl " value="${jdbc.url}"/>        <property name=" user " value="${jdbc.user}"/>        <property name=" password " value="${jdbc.password}"/>    </bean>    <!--定義模板-->    <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="datasource"/>    </bean></beans>

 

Java Spring-JdbcTemplate

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.