Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use jdbcTemplate

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about how to use jdbcTemplate. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The Spring JDBC abstract framework core package provides JDBC template classes, in which JdbcTemplate is the core class of the core package, so other template classes are encapsulated based on it, and the JDBC template class is the first working mode.

Through the template design pattern, the JdbcTemplate class helps us eliminate lengthy code, do only what needs to be done (that is, variable parts), and help us do fixed parts, such as connection creation and closure.

If the DataSource configured in application-mybatis.xml is injected into JdbcTemplate, the JdbcTemplate template can be used.

Add the following to the configuration file:

Java file:

@ Autowired @ Qualifier ("dataSource") public DataSource dataSource; public JdbcTemplate getJdbcTemplate () {JdbcTemplate jdbcTemplate = new JdbcTemplate (this.dataSource); return jdbcTemplate;}

At this point, there will be relevant database basic parameter configuration information in the jdbcTemplate variable.

There are the following methods in the jdbcTemplate type

JdbcTemplate mainly provides the following five types of methods:

Execute method: can be used to execute any SQL statement, generally used to execute DDL statements

Update method and batchUpdate method: update method is used to execute new, modified, deleted and other statements; batchUpdate method is used to execute batch-related statements

Query method and queryForXXX method: used to execute query-related statements

/ / 1. Query a row of data and return int result jdbcTemplate.queryForInt ("select count (*) from test"); / / 2. Query a row of data and convert it to Map and return jdbcTemplate.queryForMap ("select * from test where name='name5'"); / / 3. Query a row of any type of data, and the last parameter specifies the return result type jdbcTemplate.queryForObject ("select count (*) from test", Integer.class); / / 4. Query a batch of data and convert each row of data to Map jdbcTemplate.queryForList ("select * from test") by default; / / 5. Query only one list of data. The column type is String, and the column name is name jdbcTemplate.queryForList ("select name from test where name=?", new Object [] {"name5"}, String.class); / / 6. Query a batch of data and return SqlRowSet, similar to ResultSet, but no longer bind to the connection SqlRowSet rs = jdbcTemplate.queryForRowSet ("select * from test")

Call method: used to execute stored procedures and function-related statements.

The following callback APIs are supported:

Callback classes supported by the JdbcTemplate class:

Precompiled statement and stored procedure creation callback: used to create the corresponding statement based on the connection provided by JdbcTemplate

PreparedStatementCreator: obtain the Connection provided by JdbcTemplate through callback, and the user uses this Conncetion to create the relevant PreparedStatement

CallableStatementCreator: obtain the Connection provided by JdbcTemplate through callback, and the user uses this Conncetion to create the relevant CallableStatement

Callback of precompiled statement setting value: used to set values for corresponding parameters of precompiled statement

PreparedStatementSetter: obtain the PreparedStatement provided by JdbcTemplate through callback, and the user sets the corresponding parameters of the corresponding precompiled statement.

BatchPreparedStatementSetter:; is similar to PreparedStatementSetter, but for batch processing, you need to specify the batch size

Custom function callback: provides the user with an extension point that allows the user to perform any number of operations required at a specified type of extension point; ConnectionCallback: the Connection provided by JdbcTemplate is obtained through callback, and the user can perform any number of operations on the Connection

StatementCallback: get the Statement provided by JdbcTemplate through callback, and users can perform any number of operations on this Statement

PreparedStatementCallback: get the PreparedStatement provided by JdbcTemplate through callback, and users can perform any number of operations on this PreparedStatement

CallableStatementCallback: get the CallableStatement provided by JdbcTemplate through callback, and users can perform any number of operations on this CallableStatement

Result set handles callbacks: processes ResultSet through callbacks or converts ResultSet to the desired form

RowMapper: used to convert each row of the result set to the desired type, the user needs to implement the method mapRow (ResultSet rs, int rowNum) to complete the conversion of each row of data to the corresponding type.

RowCallbackHandler: used to process each row result of ResultSet, the user needs to implement the method processRow (ResultSet rs) to complete the processing. There is no need to execute rs.next () in this callback method. The operation is performed by JdbcTemplate. The user only needs to get the data by row and process it.

ResultSetExtractor: for result set data extraction, the user needs to implement the method extractData (ResultSet rs) to process the result set, and the user must process the entire result set

JdbcTemplate execution process: first define SQL, then call the JdbcTemplate method to execute SQL, and finally process the ResultSet result set through RowCallbackHandler callback.

The Spring JDBC solution is much simpler than the traditional JDBC programming. Only the variable part needs to be done, and the rest is implemented by the Spring JDBC framework.

Thank you for reading! This is the end of the article on "how to use jdbcTemplate". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report