This is the ninth day of my participation in the First Challenge 2022. For details: First Challenge 2022.
preface
Hello everyone, I'm GW_gw. I'm glad to learn and progress with you.Copy the code
The following content is from the network, if there is any infringement, please contact me to delete, this article is only used for learning exchange, not for any commercial purposes.
Abstract
This article focuses on the basic concepts of JDBCTemplate in action, as well as several methods we should focus on.Copy the code
1. Basic concepts
The JDBCTemplate object is the packaged JDBC provided by the Spring framework. Using JDBCTemplate can help us to connect and close resources and so on. We only need to provide SQL statements and process the final results. Compared with the previous, it reduces a lot of operations and also reduces the probability of errors.
2. Specific use
2.1 create JDBCTemplate
-
Download the JAR package and import it.
https://jar-download.com/artifacts/org.springframework/spring-jdbc Copy the code
- Get the JdbcTemplate object
Public class JdbcTemplateDemo01 {public static void main(String[] args) {JdbcTemplate object JdbcTemplate JdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource()); String SQL = "Update dept set name=' dept 'WHERE id=?" ; Int count = jdbctemplate. update(SQL, 1); System.out.println(count); }}Copy the code
Jdbcutils.getdatasource (); jdbCutils.getdatasource (); jdbCutils.getdatasource (); jdbCutils.getdatasource (); jdbCutils.getdatasource ()
2.2 JDBCTemplate method
-
Update (): Adds, deletes, and modifies data.
String SQL = "Update dept set name=' dept. 'WHERE id =?" ; int count = jdbcTemplate.update(sql, 1); String SQL = "insert into dept values(null,?) "; Int I = jdbctemplate.update (SQL, "SQL "); String SQL = "delete from dept where id=?" ; int i = jdbcTemplate.update(sql, 5);Copy the code
The first parameter in the SQL statement is the same as the following parameter in the SQL statement: Placeholder. The same goes for the following method.
-
QueryForList (): Query results are stored using the List collection.
String sql = "select * from dept"; List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql); Copy the code
The name of each field is the key, the value is the value, and the whole is a map object. The list is a collection of map objects.
-
QueryForMap (): Query results are stored using the Map collection.
String sql = "select * from dept where id=?" ; Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql, 1);Copy the code
The name of each field is the key, the value is the value, and the whole is a map object.
-
Query (): Encapsulates the query result into a JavaBean object.
String sql = "select * from dept"; List<Dept> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Dept>(Dept.class)); Copy the code
The first parameter is an SQL statement, and the second parameter is an object of type RowMapper. Dept corresponds to the entity class (JavaBean) of the DATABASE Dept table.
-
QueryForObject (): Encapsulates the query result as an Object.
String sql = "select count(id) from dept"; int i = jdbcTemplate.queryForObject(sql,Integer.class); Copy the code
This method is typically used when executing aggregate functions.
summary
That’s all about the enhanced JDBCUtil, which provides a lot of convenience and is often used with database connection pools. Hope the above content is helpful to the reader, if there is something wrong, welcome to comment.