Detailed spring and JDBC integration Operations _java

Source: Internet
Author: User
Tags stmt

First, a simple example

public class MyTemplate {

  private DataSource DataSource;

  Public DataSource Getdatasource () {return

  public void Setdatasource (DataSource DataSource) {
    this.datasource = DataSource;
  public void Insert (String sql) throws sqlexception{
    Connection conn = This.dataSource.getConnection ();
    Statement stmt = Conn.createstatement ();
    Stmt.executeupdate (SQL);
    Stmt.close ();
    Conn.close ();

DAO class

public class Persondao extends mytemplate{public

  void Saveperson () throws exception{This.insert
    ("Insert into person (pid,pname) VALUES (3, ' AAA ') ");

Spring configuration file

 <!--introduce properties profile--> <bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderCo Nfigurer "> <property name=" Locations "> <value></value> &LT;/PR operty> </bean> <bean id= "DataSource" destroy-method= "Close" class= "Org.apache.commons.dbcp.BasicDataSo" Urce "> <property name=" driverclassname "value=" ${jdbc.driverclassname} "/> <property name=" url "value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.username}"/> <property name= "password" value  = "${jdbc.password}"/> </bean> <bean id= "myTemplate" class= "Cn.qjc.jdbc.dao.MyTemplate" > <!-- Setter injection--> <property name= "DataSource" > <ref bean= "dataSource"/> </property> </ bean> <bean id= "Persondao" class= "Cn.qjc.jdbc.dao.PersonDao" > <property name= "DataSource" > & Lt;ref bean= "DataSource"/> </property> </bean> </beans>


Test class

public class Persondaotest {

  @Test public
  void Testpersondao () throws exception{
    ApplicationContext context = new Classpathxmlapplicationcontext ("Cn/qjc/jdbc/applicationcontext.xml");
    Persondao Persondao = (Persondao) context.getbean ("Persondao");
    Persondao.saveperson ();

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

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

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

  Template pattern = static code + dynamic variable

In spring, a dynamic variable can be given in the form of an injection. This is a good way to package a template. Static code forms a template, whereas a dynamic variable is a parameter that needs to be passed in.

Spring and JDBC Combine core class JdbcTemplate

1, based on the template settings (why can be set to a template based on the form)

2, the completion of 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 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 Remove)

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

The spring configuration file is changed to

<bean id= "Persondao" class= "Cn.qjc.jdbc.dao.PersonDao" >
    <property name= "DataSource" >
      <ref bean= "DataSource"/>

JdbcTemplate class structure diagram

Execution process


1, the operation of the data is JdbcTemplate

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

3, through to JdbcTemplate injection datasource

A, using the form of constructor injection

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. Use JdbcTemplate

In the DAO class, JdbcTemplate is injected with spring for JdbcTemplate as a property. The JdbcTemplate is then datasource injected.

Note: Why do you just inject datasource into the JdbcTemplate?

2. Inherit Jdbcdaosupport

In the DAO class, inherits the Jdbcdaosupport. Because Jdbcdaosupport already has a jdbctemplate reference, as long as the inheritance Jdbcdaosupport is equivalent to having the JdbcTemplate attribute.

3. Inherit JdbcTemplate

Spring also provides the same kind of other ORM framework consolidation patterns that are completely straightforward to apply.



From this we can see that the spring IOC and DI are powerful, and the IOC and Di complete the correspondence from interface to class. With spring container programmers it is easy to implement interface-oriented programming on the client side, 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 written by itself, and the configuration file is written by itself. Spring actually completes the work of creating objects and assemblies, and it automatically corresponds.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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