"Original" uses Spring's own jdbctemplate.

Source: Internet
Author: User

Using spring's own jdbctemplate, you can simplify the operation of the database, it is very convenient to use. With the configuration of a few steps, you can use JdbcTemplate.

(1) Configure the spring data source, add the MySQL driver jar package, configure the database properties file.

To join the MySQL driver jar package, the steps to configure the database's properties file are omitted, just give the configuration of the C3P0 data, as shown below.

1     <BeanID= "C3p0datasource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"2 Destroy-method= "Close"depends-on= "Propertyconfigurer">3         < Propertyname= "Driverclass"value= "${jdbc.driverclass}" />4         < Propertyname= "Jdbcurl"value= "${jdbc.url}" />5         < Propertyname= "User"value= "${jdbc.username}" />6         < Propertyname= "Password"value= "${jdbc.password}" />7 8         <!--The number of connections obtained at initialization, and the value should be between Minpoolsize and Maxpoolsize. Default:3 -9         < Propertyname= "Initialpoolsize"value= "Ten" />Ten         <!--the minimum number of connections that are kept in the connection pool.  - One         < Propertyname= "Minpoolsize"value= "5" /> A         <!--the maximum number of connections that are kept in the connection pool. Default:15 - -         < Propertyname= "Maxpoolsize"value= "+" /> -         <!--when the connection in the connection pool runs out, c3p0 the number of connections that are fetched at one time. Default:3 - the         < Propertyname= "Acquireincrement"value= "5" /> -         <!--maximum idle time, unused in 10 seconds, the connection is discarded. If 0, it will never be discarded. default:0 - -         < Propertyname= "MaxIdleTime"value= "Ten" /> -         <!--the standard parameters of JDBC to control the number of preparedstatements loaded within the data source. However, because the pre-cached statements belong to a single connection instead of the entire connection pool. So setting this parameter takes into account a variety of factors.  + if both maxstatements and maxstatementsperconnection are 0, the cache is closed. default:0 - -         < Propertyname= "Maxstatements"value= "0" /> +         <!--The time, in milliseconds, that the client is waiting to get a connection after the connection pool is exhausted when the customer calls Getconnection (). SQLEXCEPTION will be thrown after timeout, and if set to 0, wait indefinitely. default:0 - A         < Propertyname= "Checkouttimeout"value= "30000" /> at     </Bean>

(2) in the spring configuration file, such as the Applicationcontext.xml file, configure Jdbctempalte this bean resource.

1 <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate"2 Abstract= "false"Lazy-init= "false"Autowire= "Default">3     < Propertyname= "DataSource">4         <refBean= "C3p0datasource" />5     </ Property>6 </Bean>    

(3) in a Xxxservice class, inject the JdbcTemplate resource you want to use

1  Public class extends baseservice{2     3     @Resource 4     Private jdbctemplate JdbcTemplate; 5 6        // other code omitted   7 }

(4) Use Jdbctempalte for related database operations such as CRUD.

"Original" uses Spring's own jdbctemplate.

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.