Spring uses jdbctemplate_spring through XML configuration c3p0 connection pooling and DAO layer annotation injection

Source: Internet
Author: User
Spring uses annotations to configure C3P0 connection pooling and DAO use JdbcTemplate

1.Spring Configuration c3p0 Connection pool

Step one: Import the C3P0 jar package

Jar Package Download

Step Two: Create the spring configuration file and configure the connection pool

This is what we write when we write the C3P0 connection pool:

        Combopooleddatasource datasource=new Combopooleddatasource ();
        Datasource.setdriverclass ("Com.mysql.jdbc.Driver");
        Datasource.setjdbcurl ("Jdbc:mysql://localhost:3306/template");
        Datasource.setuser ("root");
        Datasource.setpassword ("");

Now that we have spring, we are managing JavaBean objects through Spring's container (IOC thought, control reversal).

Spring's configuration file:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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 http://www.springframework.org/schema/ AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--configuring C3P0 connection pool--> <bean id=" Datasourc E "class=" Com.mchange.v2.c3p0.ComboPooledDataSource > <!--injected property value--> <property name= "Drivercla SS "Value=" Com.mysql.jdbc.Driver "></property> <property name=" Jdbcurl "value=" Jdbc:mysql://localhost:3 306/template "></property>;p roperty name= "user" value= "root" ></property> <property name= "password" "value=" "></property> </bean> </beans>

2. At the DAO layer, use the JdbcTemplate

Since JdbcTemplate is used inside the DAO layer. So now define a complete, structure, including DAO layer, service layer example:

DAO Layer:

objects are created by annotations, and objects are managed by spring.
@Component (value= "Userdao") public
class Userdao {

    //annotation Method Injection Object
    @Resource (name= "JdbcTemplate")
    private JdbcTemplate JdbcTemplate;
    Public user Showuser (String name) {
        User user=jdbctemplate.queryforobject ("select * from User where name=?", New rowma Pper<user> () {

            @Override public
            User Maprow (ResultSet rs, int rownum) throws SQLException {
                User user= New User (rs.getstring ("name"), rs.getstring ("password"));
                return user;
            }, name);
        return user;
    }

Service Layer:

objects are created by annotations, and objects are managed by spring.
@Service (value= "UserService") public
class UserService {
    //injection object by annotation
    @Resource (name= "Userdao")
    private Userdao Userdao;

    Public User Showuser (String name) {return
        userdao.showuser (name);
    }
}

Spring's core configuration file

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
    Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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 http://www.springframework.org/schema/ AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--open javabean annotation scan--> <context:compo Nent-scan base-package= "Cn.domarvel" ></context:component-scan> <!--Create the C3P0 Connection pool object and initialize the C3P0 connection pool--> &L T;bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverclass" value
= "Com.mysql.jdbc.Driver" ></property>        <property name= "Jdbcurl" value= "Jdbc:mysql://localhost:3306/template" ></property> <property Name= "User" value= "root" ></property> <property name= "password" value= "" ></property> < /bean> <!--create JdbcTemplate, and inject c3p0 connection pool objects through the IOC's dependency injection method, then get to JdbcTemplate to directly use--> <bean id= "jdbc Template "class=" org.springframework.jdbc.core.JdbcTemplate "> <constructor-arg name=" dataSource "ref=" Dataso Urce "></constructor-arg> </bean> </beans>

One thing to note here: When we are constructing injection, the value of the Name property here is the name defined by the constructor pass, not the name of the member variable.

For example, I am here to use the JdbcTemplate method of parametric construction.

This is JdbcTemplate's constructor source code public
    JdbcTemplate (DataSource DataSource) {
        setdatasource (DataSource);
        Afterpropertiesset ();
    }

The Name property value in spring's core configuration file is also DataSource, because the local parameter name for the argument is datasource

Now let's talk about the implementation logic above:

I want to find information through JdbcTemplate, where the connection pool is c3p0.

I defined a Userdao class to look for specific information about the user in the database.

To use JdbcTemplate in Userdao, you must first create a C3P0 connection pool, create a C3P0 connection pool in the spring core configuration, and start creating c3p0 when you have a jdbctemplate connection pool. The JdbcTemplate is created by constructing a method to inject the C3P0 connection pool object. After the JdbcTemplate is created, the jdbctemplate is defined in the Userdao, and the JdbcTemplate object is injected, and after injection, the information of the database can be found through the JdbcTemplate object. The following is the creation of Userdao through annotations, creating userservice, and injecting Userdao into the userservice and using it. Creating a JavaBean object using annotations also opens a JavaBean annotation scan.

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.