Spring: conquer databases (1), and spring conquer Databases
Strictly speaking, here we conquer relational databases. I will take MongoDB as an example to provide non-relational database solutions.
Get connection, operation, close, and zookeeper exceptions... yes, you have enough. When you use JDBC only, 80% of the code you write when accessing the database is repeated. In this case, Spring provides the template + callback solution. A template is a fixed operation when accessing the database, and a callback is a changed part.
You can select different templates for different persistence platforms. If you are using JDBC, you can select JdbcTemplate. If you are using a specific ORM framework, you can choose either HibernateTemplate or SqlMapClientTemplate.
You need to weigh which JDBC template to select, because with Spring updates, the API changes may lead to different considerations. For example, in Spring2.5, The NamedParameterJdbcTemplate function is incorporated into SimpleJdbcTemplate. We generally choose SimpleJdbcTemplate. But after Spring 3.1, SimpleJdbcTemplate is discarded. The official explanation is: since Spring 3.1 in favorJdbcTemplate
AndNamedParameterJdbcTemplate
. The JdbcTemplate and NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.That is, we officially recommend using JdbcTemplate and NamedParameterJdbcTemplate because they have all the functions of SimpleJdbcTemplate.
There are too many APIs for various templates. You can check the APIS if necessary.
It seems a little bloated to use MySql during the program development process. You think, "I just want to see if this data access is successful. I have to configure a MySQL instance, which is too troublesome ", indeed. Spring provides an embedded Java database engine to address this issue, and supports HSQL, H2, and Derby.
Let's see if it configures the data source,
<jdbc:embedded-database id="dataSource"> <jdbc:script location="classpath:schema.sql"/> <jdbc:script location="classpath:test-data.sql"/> </jdbc:embedded-database>
HSQL database is enabled by default. You can also set the type attribute to customize, as shown in figure
<jdbc:embeded-database id="dataSouce" type="H2" >
The following two rows are the script with the creation mode and the script with the raw data respectively. The file is placed in the class loading path, and the file name is not important. But pay attention to the order, that is, there is data after the first mode.
Of course, to use H2 normally, you need to put the H2 driver Jar under Build Path. If you are using Maven, you only need to add the dependency:
<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>1.4.181</version></dependency>
One headache when using Spring is that the XML configuration file is cumbersome. Although automatic assembly and automatic detection can be achieved now, we recommend that you separate beans of different purposes. For example, the data source is data-related and can be opened separately.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"><jdbc:embedded-database id="dataSource" type="H2"><jdbc:script location="classpath:schema.sql" /><jdbc:script location="classpath:data.sql" /></jdbc:embedded-database></beans>
Put the above file in the dataSourc-context.xml file (in the class load path, the file name is not important), and finally only need to import in the main XML.
<import resource="classpath:dataSource-context.xml"/>
The main XML can be used to assemble the template,
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource" /> </bean>
Inject this jdbcTemplate into the custom Dao to access the database.
In addition, Spring provides Dao support classes based on templates and callbacks and can inherit its own Dao. Therefore, the whole process of accessing the database is as follows:
The official statement on JdbcDaoSupport is:
Convenient super class for JDBC-based data access objects. Requires a DataSource to be set, providings a JdbcTemplate based on it to subclasses through the getJdbcTemplate () method.
Yes, it has many sub-classes, corresponding to different templates.
What are the benefits of using it?
Previously, each Dao had to have a JdbcTemplate object with the corresponding setter method. If there are many Dao statements, it is unnecessary to repeat the same code. If you inherit a subclass of JdbcDaoSupport, you can directly use getJdbcTemplate ().
How to use it?
1. Let your Dao inherit a subclass of JdbcDaoSupport
2. the parameter is injected with datasource. (This attribute is jdbcDaoSupport and the name is datasoruce)
3. You can use getJdbcTemplate () in the code to obtain the template.
However, as the size of the Code increases, it is still difficult to maintain the JDBC Method. At this time, The ORM framework is generally used to address this challenge.
A small problem in querying databases in spring code
The number of username entries.
How can I configure two databases in spring? If one fails, I will not access that data to access another database.
Write two databases... The same operation is actually used for backup ..
Still unable to solve the data consistency problem
If a success or failure occurs, what should you do? If you continue to wait, it is no different from a database. If it does not wait, data will be inconsistent.
We recommend that you use a cluster