Talking about JdbcTemplate and MyBatis

Source: Internet
Author: User
Tags aop connection pooling

Spring's operations on the database have been deeply encapsulated in JDBC, which is the tool class JdbcTemplate

Role:

1: It provides an AOP-style transaction management

AOP-type things management: in the previous transaction management is to be fused in the logic code, in the logic code determines whether the transaction commits or rolls back, it is easy to cause the code is difficult to maintain, code redundancy
However, with spring's declarative transactions, the operation can be managed by simply annotating the transaction on the database processing method, and the transaction settings and logic code are separate and easy to maintain.

Re-encapsulating existing components without modifying the original code is similar to: FileWrite FW = new FileWrite () printwrinter pw = new Prinwriter (FW), 2:spring provides uniform exception handling, and the framework handles exceptions. Regardless of what technology the DAO layer uses to implement the error, all encapsulated into datyaaccessexception how to use 1 to introduce the corresponding spring jar + database driver package 2 in the Spring master configuration file configuration JdbcTemplate
<!--Defining Template Components -    <BeanID= "template"class= "Org.springframework.jdbc.core.JdbcTemplate">        <!--Inject connection Information -        < Propertyname= "DataSource"ref= "BONECP">        </ Property>    </Bean>
When defining a component, template requires a data connection pool to manage database connection data connection pooling There are many kinds of
Com.alibaba.druid.pool.DruidDataSource
<!--DataSource Data source Connection pool storage manage a large number of links popular dbcp C3p0,proxool -        <!--data source configuration, using the BONECP database connection pool -    <BeanID= "BONECP"class= "Com.alibaba.druid.pool.DruidDataSource"Init-method= "Init"Destroy-method= "Close">         <!--The data source driver class is not writable, and Druid is automatically identified by default according to the URL driverclass -        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver" />                <!--Basic Property URL, user, password -        < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/medicine?useunicode=true&amp;characterencoding=utf-8" />        < Propertyname= "username"value= "root" />        < Propertyname= "Password"value= "root" />                <!--Configure the time to get the connection wait timeout -        < Propertyname= "Maxwait"value= "60000" />                <!--How often the configuration interval is detected to detect idle connections that need to be closed, in milliseconds -        < Propertyname= "Timebetweenevictionrunsmillis"value= "60000" />                <!--configures the minimum lifetime of a connection in a pool, in milliseconds -        < Propertyname= "Minevictableidletimemillis"value= "300000" />                    < Propertyname= "Testwhileidle"value= "true" />        < Propertyname= "Testonborrow"value= "false" />        < Propertyname= "Testonreturn"value= "false" />                                <!--Configuring filters for monitoring statistics interception -        < Propertyname= "Filters"value= "Stat" />     </Bean>

The next one is to write the entity class according to the table

Entity class

 Packagecom.mxp.jdbc.entity;Importjava.io.Serializable; Public classUserImplementsSerializable {/**     *      */    Private Static Final LongSerialversionuid = 1L; PrivateString ID; PrivateString UserName;  PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }        }

You also need to write a rowmapper based on the entity class

Note that this is going to inherit

Org.springframework.jdbc.core.RowMapper, and then override the Maprow method
 Packagecom.mxp.jdbc.entity;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportOrg.springframework.jdbc.core.RowMapper;/*** Each entity class has this encapsulation component * Encapsulates a user record into a user object *@authorAdministrator **/ Public classUserrowmapperImplementsRowmapper<user>{    /*** Arg1: The first few lines of records*/@Override PublicUser Maprow (ResultSet arg0,intARG1)throwsSQLException {User User=NewUser (); User.setid (Arg0.getstring ("id")); User.setusername (Arg0.getstring ("User_name")); returnuser; }}

Talking about JdbcTemplate and MyBatis

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.