2012年3月21日星期三

Monster Beats7

7.2 JDBC 7.2.1 Spring JDBC template class overview abstract framework core package provides the JDBC template classes ,where JdbcTemplate is the core packet core classes ,so other template classes are based on its package is completed ,the JDBC template classes is the first working model .
JdbcTemplate through the template design pattern helps us to eliminate the redundant code ,only need to do (or variable section ) ,and help us to do what the fixed part ,such as the connection creation and closure .
The JdbcTemplate class to the variable part of the callback interface implementation, such as ConnectionCallback via the callback interface to return to the user a link ,which can use the connection to do anything ,StatementCallback through the callback interface to return to the user a Statement ,which can use the Statement to do anything and so on ,there are some other callback interface as shown in figure 7-3 .
Figure 7-3 JdbcTemplate support callback interface Spring in addition to providing JdbcTemplate core classes ,are also provided based on the realization of the JdbcTemplate NamedParameterJdbcTemplate class is used to support named arguments bound ,SimpleJdbcTemplate used to support Java5& # 43 ;variable parameters and automatic boxing and other characteristics .
7.2.3 traditional JDBC programming alternatives that we have used the traditional JDBC programming way, let us look at the Spring JDBC framework provides a better solution .1) prepare the jar package and added to the class path :2) traditional JDBC programming alternatives :in the use of JdbcTemplate template class must pass through a DataSource database connection ,Spring JDBC provides the DriverManagerDataSource implementation ,it through the pack DriverManager.
getConnection for the database connection ,please refer to the relevant specific DataSource 7.5.1 control database connection .Let us next specific analysis :1) JDBC: HSQLDB: MEM: Test: using HSQLDB memory database ,database named test .
2) public static void setUpClass ( ) :JUnit @ BeforeClass annotations, expressed in so test method before execution, and performed only once .In this method defines the DataSource and use the DataSource object to create a JdbcTemplate object .
The JdbcTemplate object is thread safe .3) JdbcTemplate implementation process: first of all, the definition of SQL ,then call the JdbcTemplate method to perform SQL ,then RowCallbackHandler ResultSet result set callback processing .
Spring JDBC solution compared with the traditional JDBC programming way is it right? How simple ,Lady Gaga Heartbeats,is it right? Only the variable part need us to do, the rest by the Spring JDBC framework to achieve the .
Let us next depth JdbcTemplate and extended .7.2.4 JdbcTemplate first let how to use JdbcTemplate to achieve the additions and deletions to search .One ,first create the table structure :1) org.
junit bag under the @ Before and @ After respectively before and after the test method for performing the method ,each testing method will be executed once ;2) create memory table test create HSQLDB memory table ,contains two fields of ID and name ,where id is increment function key ,if you are not familiar with the HSQLDB can be changed into knowledge database .
Two ,define test framework ,the testing method is used to realize the additions and deletions to search test :three ,the new test: four ,delete the test :five ,:six ,update the test query test :after reading the above example ,whether we think JdbcTemplate simplifies many of our labor ?Let us next in-depth study provided by the JdbcTemplate method .
JdbcTemplate provides the following five methods :class JdbcTemplate support callback class :PreparedStatementCreator :JdbcTemplate Connection provides access through the callback ,by the users to use the Conncetion to create the PreparedStatement ;CallableStatementCreator: JdbcTemplate Connection provides access through the callback ,by the users to use the Conncetion to create the CallableStatement ;PreparedStatementSetter: JdbcTemplate PreparedStatement provides access through the callback ,by the user to the corresponding pre-compiled statements corresponding parameter set & # ;20540 ;BatchPreparedStatementSetter :20284 ;# & ;in PreparedStatementSetter ,but used for batch processing ,need to specify the batch size ;ConnectionCallback: JdbcTemplate Connection provides access through the callback ,the user can be in the Connection to implement any number of operations ;StatementCallback: by callback for JdbcTemplate Statement ,the users can Statement to implement any number of operations ;PreparedStatementCallback: through the callback gets a JdbcTempl Ate PreparedStatement ,the users can PreparedStatement to implement any number of operations ;CallableStatementCallback: JdbcTemplate CallableStatement provides access through the callback ,the users can CallableStatement to implement any number of operations ;RowMapper: for each row of data conversion result set to the desired type ,the user needs to realize the method mapRow ( ResultSet RS ,int rowNum ) to complete each line of data is converted to a corresponding type .
RowCallbackHandler: ResultSet for processing each line ,the user needs to realize the method processRow ( ResultSet RS ) to complete the processing ,in the callback method without the need to perform rs.
next ( ) ,Coach Bag,the operation is performed by the JdbcTemplate ,users only need to follow the line of data acquisition and processing .ResultSetExtractor: for the result set data extraction ,the user needs to realize the method extractData ( ResultSet RS ) to process the result set ,the user must handle the entire result set ;then let us look at specific examples ,in the sample can not be introduced to all of the JdbcTemplate method and callback type to use ,we just introduce the representative ,the rest of the use is # 20284 & ;;1) pre-compiled statements and stored procedures ,user-defined function create a callback callback is used : the first use of PreparedStatementCreator to create a pre-compiled statements ,followed by JdbcTemplate through PreparedStatementCallback callback returns ,determined by the user how to perform the PreparedStatement .
Here we use the execute method .2) the precompiled statement set & # callback :20540 ;use by JdbcTemplate int update ( String SQL ,PreparedStatementSetter PSS ) execute pre-compiled SQL ,wherein SQL parameters for insert into test ( name ) values ( ?) ,the SQL has a placeholder is required in the execution of the front set & # 20540 ,PreparedStatementSetter ;implementation is to set up & # ,20540 ;setValues ( PreparedStatement pstmt ) callback method & # 20540 ;the corresponding placeholder position & # 20540 ;.
JdbcTemplate also provides a simpler way to update ( String SQL ,args Object ... ) to achieve a & # 20540 ;,so when using the way it did not satisfy the demand to PreparedStatementSetter should be used .
3) a result set processing callback :RowMapper interface with mapRow ( ResultSet RS ,Beats By Dre,int rowNum ) method the results of each line is converted to a Map ,of course, can be converted to other classes ,such as table object painting form .
RowCallbackHandler interface also provides for methods of processRow ( ResultSet RS ) ,the result set row are converted to form .ResultSetExtractor uses a callback method ( ResultSet extractData RS ) provides to the user the result set ,allowing the user to determine how to process the result set .
Of course, JdbcTemplate provides a simple queryForXXX method ,to simplify the development of :3) the stored procedure and function callback :first, modify the JdbcTemplateTest setUp method ,the modified as follows :CREATE FUNCTION FUNCTION_TEST used to create custom functions ,CREATE PROCEDURE PROCEDURE_TEST used to create the stored procedure ,pay attention to the create statement is the related database ,in this example statement is only for HSQLDB database .
Secondly, modify the JdbcTemplateTest tearDown method ,the modified as follows :the drop statement is used to delete a stored procedure created ,custom function and database table .Then look at how HSQLDB call a custom function :Note: because HSQLDB { ?= call .
.. } and {call ... } the meaning is the same ,so call the custom function returns an ResultSet containing the results .Finally, let us example MySQL how to call a custom function :the last look at how how to call a stored procedure :class JdbcTemplate also provides many convenient method ,Moncler UK,this is not the one one introduced ,but these methods is made to follow the law, the first is to provide a callback interface to allow the user to decide what to do ,second can be considered a convenient method ( such as queryForXXX ) ,for those simple operation .
7.2.4 NamedParameterJdbcTemplate NamedParameterJdbcTemplate is based on JdbcTemplate ,and subjecting it to a package to support named parameters .NamedParameterJdbcTemplate provides the following three methods :Execute method ,query and queryForXXX methods ,update and batchUpdate method .
Let us first look at the example :then let us analyze the code it :1) NamedParameterJdbcTemplate initialization :you can use the DataSource or JdbcTemplate objects as parameters to the constructor initialization ;2) insert into test ( name ) ( values :name ) : the Name :is named parameters ;3) update ( insertSql ,paramMap ) :wherein paramMap is an Map type ,which contains the key to ,& # 20540 ; button & # 20540 ;for ,is also named for parameter setting & # 20540 ;data ;4) query ( selectSql ,paramMap ,RowCallbackHandler ( New ) .
.. ... # ) :& 20284 ;JdbcTemplate introduced, the only difference is the need to introduce paramMap to named parameters set & # ;20540 ;5 ) update ( deleteSql ,paramSource ) :# & 20284 ;in update ( insertSql ,paramMap ) ,but use the SqlParameterSource parameter to set up & # named parameters 20540 ;here ,the use of MapSqlParameterSource to achieve, it is a simple java.
util.Map package .Class NamedParameterJdbcTemplate as named parameters set & # 20540 ;there are two ways :java.util.Map and SqlParameterSource: 1 ) java.util.Map: Map key data to named parameters ,while Map& # 20540 ;data is used to set up & # ;20540 ;2 ) SqlParameterSource: you can use the SqlParameterSource implementation as to achieve as named parameters set & # 20540 ;by default ,MapSqlParameterSource and BeanPropertySqlParameterSource ;MapSqlParameterSource implementation is very simple, just a package of java.
util.Map ;and BeanPropertySqlParameterSource encapsulates an JavaBean object ,through the JavaBean object attributes to decide named parameters & # 20540 ;.We can see that BeanPropertySqlParameterSource use can reduce a lot of work ,but named parameters must be and JavaBean attribute names corresponding to .
7.2.5 SimpleJdbcTemplate SimpleJdbcTemplate is also based on JdbcTemplate ,Monster Beats,but the use of Java5& # 43 ;variable argument list and automatic boxing and unboxing in order to gain more concise code .
The SimpleJdbcTemplate provides two kinds of methods: query and queryForXXX methods ,update and batchUpdate method .Let us first look at the example :1) SimpleJdbcTemplate initialization :you can use the DataSource ,JdbcTemplate or NamedParameterJdbcTemplate objects as parameters to the constructor initialization ;2) update ( 10 ,insertSql ," ;name5" ;Java5& ) : the use of # 43 ;variable argument list to replace new Object {10 ," ;name5" ;} ;3) query ( selectSql ,mapper ,10 ," ;name5" ;Java5& ) :use # 43 ;variable argument lists and RowMapper callback and utilization of generic properties to specify the return & # type 20540 ;( List< ;UserModel> ;) .
SimpleJdbcTemplate also supports named parameters ,such as queryForList ( String SQL ,SqlParameterSource args ) and queryForList ( String SQL ,Map< ;String ,> ;args ?) ,# & 20284 ;in use in NamedParameterJdbcTemplate ,this is not introduced .
Related articles:

没有评论:

发表评论