Spring and JDBC Integration in a detailed

Source: Internet
Author: User
Tags stmt

First, a simple example

 Public classMyTemplate {PrivateDataSource DataSource;  PublicDataSource Getdatasource () {returnDataSource; }     Public voidSetdatasource (DataSource DataSource) { This. DataSource =DataSource; }         Public voidInsert (String SQL)throwssqlexception{Connection Conn= This. Datasource.getconnection (); Statement stmt=conn.createstatement ();        Stmt.executeupdate (SQL);        Stmt.close ();    Conn.close (); }}

DAO class

 Public class extends mytemplate{    publicvoidthrows  exception{        this. Insert ("INSERT into person (pid,pname) VALUES (3, ' AAA ')");}    }

Spring configuration file

    <!--introducing the Properties configuration file -    <Beanclass= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        < Propertyname= "Locations">            <value>Classpath:jdbc.properties</value>        </ Property>    </Bean>        <BeanID= "DataSource"Destroy-method= "Close"class= "Org.apache.commons.dbcp.BasicDataSource">        < Propertyname= "Driverclassname"value= "${jdbc.driverclassname}" />        < Propertyname= "url"value= "${jdbc.url}" />        < Propertyname= "username"value= "${jdbc.username}" />        < Propertyname= "Password"value= "${jdbc.password}" />    </Bean>        <BeanID= "MyTemplate"class= "Cn.qjc.jdbc.dao.MyTemplate">        <!--Setter Injection -        < Propertyname= "DataSource">            <refBean= "DataSource"/>        </ Property>    </Bean>        <BeanID= "Persondao"class= "Cn.qjc.jdbc.dao.PersonDao">        < Propertyname= "DataSource">            <refBean= "DataSource"/>        </ Property>    </Bean></Beans>

Test class

 Public class persondaotest {    @Test    publicvoidthrows  exception{          New Classpathxmlapplicationcontext ("Cn/qjc/jdbc/applicationcontext.xml");         = (Persondao) context.getbean ("Persondao");        Persondao.saveperson ();    }}

The above code injects datasource into MyTemplate, and then injects DataSource to Persondao, because Persondao inherits MyTemplate, so it has datasource properties. Since Persondao inherits MyTemplate, Persondao class injection can be changed to

<id= "Persondao"  class= "Cn.qjc.jdbc.dao.PersonDao " Parent= "MyTemplate"></bean>

In the above example, MyTemplate is similar to the template mode in the design pattern is also called template method pattern, template method mode is one of the most common patterns in all patterns, is based on the inheritance of code reuse basic technology.

  Template mode = static code + dynamic variable

In spring, dynamic variables can be given in the form of injections. This way of programming is suitable for packaging into templates. Static code forms a template, while a dynamic variable is a parameter that needs to be passed in.

Spring and JDBC Combine core class JdbcTemplate

1, template-based settings (why can be set as template-based form)

2, completed the creation and release of resources work

3. Simplify the operation of JDBC for us

4, completed the core process of JDBC work, including the creation and execution of SQL statements

5, only need to pass DataSource it can be instantiated

6, JdbcTemplate only need to create once

7. JdbcTemplate is a thread-safe class

Use SPRING+JDBC to modify the above example (MyTemplate class removed)

public class Persondao extends Jdbcdaosupport {public    void Saveperson (String sql) {        this.getjdbctemplate (). Execute (SQL);}    }

Change the spring configuration file to

<BeanID= "Persondao"class= "Cn.qjc.jdbc.dao.PersonDao">        < Propertyname= "DataSource">            <refBean= "DataSource"/>        </ Property></Bean>

JdbcTemplate class structure diagram

Execution process

Description

1, the operation of the implementation of the data is JdbcTemplate

2, the most fundamental step is to inject datasource into the JdbcTemplate

3, by injecting jdbctemplate into the datasource

A, using the form of a constructor to inject

b, using setter method to inject

4, can give Jdbcdaosupport inject datasource

5, can give Jdbcdaosupport inject JdbcTemplate

So there are three ways to integrate spring with JDBC, but in fact the core class is JdbcTemplate

1. Using JdbcTemplate

In the DAO class, JdbcTemplate is used as a property to inject JdbcTemplate with spring. The jdbctemplate is then injected into the datasource.

Note: Why should I just inject datasource on JdbcTemplate?

2. Inheriting Jdbcdaosupport

In the DAO class, inherit Jdbcdaosupport. Since Jdbcdaosupport already has a reference to JdbcTemplate, it is equivalent to having the JdbcTemplate attribute as long as the inheritance jdbcdaosupport.

3. Inheriting JdbcTemplate

Spring also offers other ORM framework integration patterns that are almost entirely straightforward to apply.

Spring+hibernate

Spring+jdo

As a result, the spring IOC and DI are powerful, and the IOC and DI complete the correspondence from the interface to the class. With the Spring container programmer, it is easy to implement interface-oriented programming on the client, and it is easy to assemble the interface, and the structure can be set flexibly. Because the interface is written by itself, the class is also written by itself, configuration files are written by themselves. Spring actually completes the work of creating objects and assemblies, and it automatically corresponds to them.

Spring and JDBC Integration in a detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.