Today is the 125th day that Liu Xiaoai has taught herself Java.

Thank you for watching. Thank you.


Today’s learning schedule is as follows:

  • JdbcTemplate configuration and use in Spring.
  • Spring’s transaction management mechanism.
  • I wrote a transfer case and configured the transaction with XML.

Spring JdbcTemplate configuration

1 The original test

That is, code without the Spring framework:


① Configuring a Data source

Nothing more than the four parameters of the database setting.

We used to use C3p0 or druids, and here we use built-in data sources.

② Create the jdbcTemplate object

Use the database to initialize the jdbcTemplate object, and then through it to achieve the database add, delete, change, query.

Of course, these are the most primitive test methods, we now learn the Spring framework, see the new keyword, to think of the use of Spring.

2Spring configuration information


① Data source configuration

The DriverManagerDataSource is managed by the Spring container and the four parameter properties are set.

(2) jdbcTemplate configuration

In the same way that you used to need to new an object, you can now configure it as a bean in Spring.

The object requires a data source as a parameter, so DI dependency injection is required.

Junit testing for 3Spring

The test code automatically turns on annotation scanning, so we can use annotations even if we haven’t configured them in the Spring configuration file, as we explained yesterday.


The test code annotations are in a fixed format.

  • RunWith: Integrate Spring with junit.
  • @contextConfiguration: Specifies the location of Spring.
  • Autowired Annotation: Inject objects to be tested.

Running code can also query data in the database.

Use druid data sources

We can set the four database parameters in the Properties configuration file and then import the external file.


① Import external files

  • Property-placeholder: Translation stands for property placeholders through which to import external files.

  • Location: indicates the path of an external file.

② Configure a druid connection pool

Introduce attributes in the configuration file as ${}.

③ Configure the jdbcTemplate object

Inject the druid connection pool into the jdbcTemplate object.

Spring JdbcTemplate

After using the Spring configuration, test it in the DAO layer.

1dao layer code writing and configuration


(1) the XML configuration

Inject the jdbcTemplate object into the userDao, or use @AutoWired if you use an annotation.

Use both XML and annotations to deepen your understanding of them.

② Query users by ID

The result of the query is a user object.

2 Test Methods


The test method simply calls the method using the userDao object, using the @AutoWired annotation to do the injection.

Running the code yields user data with the corresponding ID.

3DAO layer code optimization

To facilitate the injection of JDBCTemplates into daOs, Spring provides support classes for each persistence technique.


1) JdbcDaoSupport class

Inherit the JdbcDaoSupport class wrapped by the Spring framework to obtain the jDBcTemplate object operation database.

The object is obtained using the getJdbcTemplate method.

② Inject data source

The JDBCTemplate object is obtained by injecting the data source into the userDao object in the XML file.

Why is that?

JdbcDaoSupport = JdbcDaoSupport = JdbcDaoSupport = JdbcDaoSupport


It has a setDataSource method that retrieves a jdbcTemplate object by injecting the data source.

Third, Spring transaction management mechanism

The Spring transaction management high-level abstraction consists of three interfaces that do most of Spring’s transactions together:

1PlatformTransactionManager

Transaction manager, which is used to manage platform-related transactions, is easy to remember despite its long name:

  • Platform: Platform.
  • A Transaction.
  • Manager: Manager.

Spring provides different transaction manager interface implementations for different persistence frameworks.


DataSourceTransactionManager is we use the transaction manager, use the Connection control:

  • Start transaction: connection.setautoCommit (false)
  • Commit transaction: connection.com MIT ()
  • Rollback transaction: connection.rollback()

2TransactionDefinition

Transaction definition information, used to define transaction-related attributes, for use by the transaction manager.

This interface provides the following methods:

  • GetIsolationLevel: Obtains the isolation level.
  • GetPropagationBehavior: Obtains propagationBehavior.
  • GetTimeout: obtains the timeout period.
  • IsReadOnly: indicates whether it is read-only.

There are four isolation levels: read uncommitted, read Committed, repeatable read, and serialization.

I have explained it in detail in my notes when STUDYING transactions and will not repeat it again.

There are three main categories of communication behavior acquisition:

  • REQUIRED: Only one transaction (default, recommended)
  • REQUIRES_NEW: Two transactions exist. If one exists, suspend the transaction and restart a new one.
  • NESTED: a transaction can set a savepoint, roll back to the savepoint, and select commit or rollback.

3TransactionStatus

The running status of a transaction is the status information of a transaction at each point in time in the process of transaction management.

XML configuration transaction management

The above are all conceptual points, which are difficult to remember, so I will directly use a transfer case to illustrate:

1Service layer code writing


Since it is a transfer, three parameters should be clear: who is paying? Who is the payer? How much is it?

① Transfer the money out first

Turn the money that correspondence comes out from give money namely.

② Then transfer the money

The money to be transferred is transferred to the receiver.

2DAO layer code writing


Inherit the jdbcDaoSupport class and get the jdbcTemplat object from its parent class, as I mentioned earlier

(3) the money into

The amount of money added to the recipient’s account is the amount of money transferred.

(4) money transfer

The amount of money in the recipient’s account is reduced by the amount of money transferred.

3XML configures transactions

We hand both layers of objects to the Spring container, but such code is problematic: roll-in and roll-out either succeed or fail at the same time, requiring transaction management.


① Configure the transaction manager

You can simply select the transaction manager for the framework and hand it over to the Spring container for management while injecting the data source.

② Configure transaction management

Transaction notification is configured using the TX: Advice tag, and the transaction manager is described using the property transaction-Manager.

The child tag tx:method corresponds to the four methods in the TransactionDefinition interface above:

  • Read-only: corresponds to isReadOnly().
  • Timeout: corresponds to getTimeout().
  • Propagation: Corresponding getPropagationBehavior().
  • Isolation: corresponds to getIsolationLevel().

(3) the aop configuration

Configure the pointcut, where *Service indicates that any ID or name ending in Service is intercepted.

Configuring the aspect adds configured transaction notification to the corresponding pointcut, which adds a transaction capability to the pointcut.

4 test


Set up a run exception as a test in the roll-in business.

If transactions are not configured:

  • Successful transfer of business, the corresponding account money reduced.
  • Failed to transfer business, the corresponding account money did not increase.

If a transaction is configured, as long as one side fails, it all fails and the data does not change.

The last

In terms of annotations, it’s easy to use annotations when you know how to use XML. In the future, if you have a similar need to use annotations, you won’t have to write an article. Tomorrow, you’ll start learning about the springMVC framework.

Thanks for watching.

If you can, please give it a thumbs up. Thank you.