Spring: integrate JDBC in Spring;

Source: Internet
Author: User

Spring clearly divides the fixed and variable parts of the data access process into two different classes: template and callback ).
A template class processes a fixed part of data access-thing control, resource management, and exception handling.


Spring database access template:


jca.cci.support.CciDaoSupport JCA CCI connectionsjdbc.core.support.JdbcDaoSupport JDBC connectionsjdbc.core.namedparam.NamedParameterJdbcDaoSupport JDBC connections with support for named parametersjdbc.core.simple.SimpleJdbcDaoSupport JDBC connections, simplified with Java 5 constructsorm.hibernate.support.HibernateDaoSupport  Hibernate 2.x sessions orm.hibernate3.support.HibernateDaoSupport Hibernate 3.x sessionsorm.ibatis.support.SqlMapClientDaoSupport  iBATIS SqlMap clientsorm.jdo.support.JdoDaoSupport Java Data Object implementationsorm.jpa.support.JpaDaoSupport Java Persistence API entity managers


Configure the Data source:


For applications that are about to be released to the production environment, we recommend that you obtain the connected data source from the connection pool. If possible, I prefer to obtain the data source from the pool through the JNDI of the application server.

Spring applications are often deployed on java ee application servers, such as websphere jboss or web containers like tomcat. These servers allow you to obtain data sources through JNDI configuration. The advantage of this configuration is that the data source can be fully managed out of the application. In this way, the application only needs to access the data source when accessing the database. In addition, the data sources managed by the Application Server are usually organized in a pool to provide better performance and support system administrators to perform hot switching.


  

In this way, it can be assembled into Spring.


Use the data source connection pool:

 
  
  
  
  
  
  
 

Below are the connection pool attributes:
initialSize The number of connections created when the pool is started.maxActive The maximum number of connections that can be allocatedfrom the pool at the same time. If zero, there’s no limit.maxIdle The maximum number of connections that can be idle in thepool without extras being released. If zero, there’s no limit.maxOpenPreparedStatementsThe maximum number of prepared statements that can be allo-cated from the statement pool at the same time. If zero,there’s no limit.maxWait How long the pool will wait for a connection to be returned tothe pool (when there are no available connections) before anexception is thrown. If -1, wait indefinitely.minEvictableIdleTimeMillis How long a connection can remain idle in the pool before it’s eligible for eviction.minIdle The minimum number of connections that can remain idle inthe pool without new connections being created.poolPreparedStatements Whether or not to pool prepared statements (Boolean).


Spring -------- JDBC Template

 
 
  
 Public templates {... privateSimpleJdbcTemplatejdbcTemplate; public voidsetJdbcTemplate (SimpleJdbcTemplatejdbcTemplate) {this. jdbcTemplate = jdbcTemplate ;}}
 
 
  
 

// When the code is updated, the connection is obtained, the statement is created, and sqlpublic voidaddSpitter (Spitterspitter) {jdbcTemplate is executed. update (SQL _INSERT_SPITTER, spitter. getUsername (), spitter. getPassword (), spitter. getFullName (), spitter. getEmail (), spitter. isUpdateByEmail (); spitter. setId (queryForIdentity ();} public SpittergetSpitterById (longid) {return jdbcTemplate. queryForObject (SQL _SELECT_SPITTER_BY_ID, new ParameterizedRowMapper
 
  
() {Public SpittermapRow (ResultSetrs, introwNum) throws SQLException {Spitter spitter = newSpitter (); spitter. setId (rs. getLong (1); spitter. setUsername (rs. getString (2); spitter. setPassword (rs. getString (3); spitter. setFullName (rs. getString (4); return spitter ;}, id );}
 

Note: The queryForObject method has three parameters: 1. string, which contains the SQL statement to be searched from the database. 2. ParameterizedRowMapper callback method 3. Variable Parameter List, which lists the index parameter values to be bound to the query.


Use named parameters:

private static final String SQL_INSERT_SPITTER="insert into spitter(username,password,fullname)"+"values(:username,:password,:fullname)";public void addSpitter(Spitter spitter){Map
 
  params=new HashMap
  
   ();params.put("username",spitter.getUsername());params.put("password",spitter.getPassword());params.put("fullname",spitter.getFullName());jdbcTemplate.update(SQL_INSERT_SPITTER,params);spitter.setId(queryForIdentity());}
  
 


SimpleJdbcDaoSupport can easily access SimpleJdbcTemplete through getSimpleJdbcTemplate:

public voidaddSpitter(Spitterspitter){getSimpleJdbcTemplate().update(SQL_INSERT_SPITTER,spitter.getUsername(),spitter.getPassword(),spitter.getFullName(),spitter.getEmail(),spitter.isUpdateByEmail());spitter.setId(queryForIdentity());}


 
 
  
 



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.