Detailed example of database operation object model in Spring jdbc Spring Jdbc database operation objectification Use object-oriented methods to represent relational database operations and implement a thread-safe and reusable object model. Its top-level parent class interface is RdbmsOperation. SqlOperation inherits this interface to implement database operations such as select, update, and call. 1. Query interface: SqlQuery 1) GenericSqlQuery, UpdatableSqlQuery, MappingSqlQueryWithParameter 2) SqlUpdate, BatchSqlUpdate 3) SqlCall 1) Encapsulate the database operation select as an object. The base class of the query operation is SqlQuery. All queries can be represented by this class. Spring JDBC also provides some easier-to-use MappingSqlQueryWithParameters and MappingSqlQuery for mapping result sets to Java objects. The query object class also provides two extensions, UpdatableSqlQuery and SqlFunction. 2) Add, delete and modify operations, encapsulate the database operations insert, update and delete as objects. The base class for add, delete and modify operations is SqlUpdate. Of course, BatchSqlUpdate is also provided for batch processing. 3) Stored procedure and function calls are encapsulated as objects. The base class is the SqlCall class, which provides a StoredProcedure implementation. Database Connection JDBC: Spring jdbc controls the database connection through DataSource, that is, it is obtained through its implementation subclass. 1) DriverManagerDataSource: Simple encapsulation of DriverManager.getConnection() 2) SingleConnectionDataSource: encapsulates a connection internally, will not be closed and cannot be used by multiple threads, used for testing 3) LazyConnectionDataSourceProxy: Encapsulates DataSource and is used to delay the acquisition of database connections. The connection is only obtained when a Statement is actually created. Therefore, in actual projects, the proxy is finally used to wrap the original DataSource so that the connection is only obtained when it is really needed. Vendor: The DataSource implementations provided mainly include C3P0/Proxool/DBCP/, etc. These implementations all have database connection pool capabilities. DataSourceUtils: The Spring JDBC abstract framework obtains database connections through its getConnection(DataSource dataSource) method, releaseConnection(Connection con, DataSource dataSource) is used to release database connections, and DataSourceUtils is used to support Spring managed transactions. Only connections obtained using DataSourceUtils have Spring managed transactions. Spring JDBC provides consistent database access through the DaoSupport abstract class. 1) JdbcDaoSupport: supports consistent JdbcTemplate access 2) NamedParameterJdbcDaoSupport: A subclass of JdbcDaoSupport that provides access to NamedParameterJdbcTemplate 3) SimpleJdbcDaoSupport: JdbcDaoSupport subclass, providing SimpleJdbcTemplate access Because the JdbcTemplate, NamedParameterJdbcTemplate, and SimpleJdbcTemplate classes use DataSourceUtils to obtain and release connections, and the connections are bound to threads, these JDBC template classes are thread-safe, that is, JdbcTemplate objects can be reused in multiple threads. If you have any questions, please leave a message or come to the community to discuss. Thank you for reading and I hope it can help you. Thank you for your support of this site! You may also be interested in:
|
<<: How to use a game controller in CocosCreator
>>: How to create a swap partition file in Linux
Table of contents Binding Class Binding inline st...
Sometimes local development requires debugging of...
COALESCE is a function that refers to each parame...
When using HTML tables, we sometimes need to chan...
Table of contents environment Virtual Machine Ver...
MySQL regular sorting, custom sorting, and sortin...
Apache Tomcat is an open source software that imp...
Table of contents 1. some 2. every 3. find 1. som...
This article summarizes the notes for installing ...
This article shares with you the MySQL 8.0.13 ins...
Encryption and decryption are an important means ...
1. The ul tag has a padding value by default in Mo...
Preface This article introduces the installation ...
Table of contents What are spread and rest operat...
Congratulations on finally convincing your bosses...