Spring connection database and JDBC template (example explanation)
preface
Today's introduction is about the database connection of spring and the API methods related to the JDBC template. Although after learning hibernate, you will know that the database connection is generally implemented using hibernate and other persistence frameworks. However, many times, some things related to transactions cannot be realized by using these frameworks, so we need to combine spring to realize the relevant needs.
1、 Create project and guide package
Before using spring's JDBC template, you also need to import the relevant jar package:
2、 Create relevant beans and write tool classes
2.1 create two tables in the database, and use the API method in spring's JDBC template to operate the two tables
2.2 creating entity beans
2.3 create spring's tool class -- springutil java
2.4 configure the configuration file - spring Configuration of XML file
Because there may be many API methods to be introduced, you can paste all the written XML at one time. Readers can correspond to the corresponding classes according to the corresponding configuration:
2.5 introduces spring's support for JDBC templates
(1) Database connection
The configuration in the corresponding XML is:
Corresponding test class:
Test results:
It can be seen that if the test is successful, you can obtain the corresponding database connection.
(2) The test queries the corresponding data from the created user table
The corresponding XML configuration is:
The corresponding test classes are:
Test results:
Note: this test class uses the common pure JDBC method to obtain data.
(3) Use the class jdbctemplate to get the data to the back end
The configuration of the corresponding XML file is:
Corresponding test class:
Test results:
In this way, we have to create a jdbctemplate instance every time. This is more complex, so consider whether spring can create this class by itself, and then reference the data source in the bean configuration file to achieve the purpose. This is the fourth method to be introduced below.
(4) Configure our template class in the configuration file and let spring generate the required beans
The corresponding XML configuration file is:
The corresponding test classes are:
Test results:
This method still needs to be configured in XML, and a more convenient way in spring is to directly inherit the class jdbcdaosupport. This is the simplest and most commonly used method. The author will briefly introduce the use of this method.
(5) Inherit the jdbcdaosupport class to get the data in the database
The configuration in the corresponding XML file is:
The corresponding test classes are:
The test results are:
(6) Use spring injection to manipulate DDL statements
The configuration in the corresponding XML file is:
The corresponding test classes are:
After testing, DDL operation can be carried out.
(7) DML operation using spring injection
Corresponding configuration in XML file:
Corresponding test class:
After testing, the addition, deletion, modification and query in DML operation can be realized.
(8) Use the named parameters in the jdbctemplate template to manipulate our DML statements
The configuration in the corresponding XML is:
Corresponding test class:
(9) Encapsulation of query statements by jdbctemplate template
Configuration of corresponding XML file:
Corresponding test class:
Today, the author will introduce the jdbctemplate template in spring to this.
The above spring connection database and JDBC template (example explanation) are all the contents shared by Xiaobian. I hope to give you a reference and support programming tips.