Spring+Mybatis integrates core knowledge

Spring+Mybatis integrates core knowledge points

1. How does Spring integrate with Mybatis?

(1) Configure data sources

(2) configure SqlSessionFactory

(3) Configure Dao

(4) Configure Service

Code case:

  • Configure datasources (data source, connection pool)
<!--Required dependencies: 1 driverClassName 2,url 3,username 4,password-->
<bean class="DruidDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="slightly"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
  • Configure SqlSessionFactory
<!--1,Dependent on data source 2. Dependent on mapper file(It's a path/)-->
<bean class="SqlSessionFactoryBeanName" id="sqlSessionFactory">
<property name="dataSource" rel="dataSource"></property>
<property name="mapperLocations" value="com/tjcu/mapper/*Mapper.xml"></property>
  • Configuration management dao layer (mappeScannerConfigurer: mapping scanner configuration)
<!--1,dao Package 2. Dependent on sqlSessionFactory mapperScannerConfigurer When configuring, the default entity class alias is lowercase -->
<bean class="mapperScannerConfigurer">
<property name="sqlSessionFactory" rel="sqlSessionFactory"></property>
<property name="basePackage" value="com.tjcu.dao">
  • Configure management Service layer
<!--Dependency 1 dao layer-->
<bean class="com.tjcu.service.UserServiceImpl" id=userService>
<propertry name="userDao" rel="userDao"></property>

2. How to control transactions in Spring?

(1) Configure transaction manager (DataSourceTransactionManager)

(2) Configure transaction notification + transaction attribute + transaction isolation level

(3) Configure pointcuts

(4) Assembly section

Code case:

  • Configure transaction manager
<!--Dependent on: 1. Transaction source-->
<bean class="dataSourceTransactionManager" id="transactionManager">
<property name="dataSource" rel="dataSource"></property>
  • Configure transaction notification + transaction attributes
<tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:method name="insert*" propagration="REQUIRED" read-only="true"></tx:method>
   <tx:method name="update" propagration="REQUIRTED" read-only="true"></tx:method>
   <tx:method name="select*" propagration="SUPPORTS"></tx:method>
  • Configure pointcuts
<aop:pointcut id="pointcut" value="execution(* com.tjcu.*ServiceImpl.*(..))"><aop:pointcut>
  • Assembly section
<aop:advisor advice-rel="txAdvice" pointcut-rel="pointcut"></aop:advisor>

  • spring integrates Mybatis standard development steps (DAO layer + Service layer)

3. What are the common database isolation levels and what problems are they solved?

(1) Database transaction isolation level:

Solve the problems of concurrent transactions, such as Tibetan independence, non repeated reading and unreal reading

(2) Isolation level

  • read_UnCommitted: uncommitted transactions: dirty reads occur. If a client adds some data and has not committed, other clients can read uncommitted transactions and will dirty read

  • read_committed: the read has been committed. The default transaction isolation level of Oracle solves the dirty read problem. One client adds some data and has not committed, and other clients cannot read uncommitted transactions

  • Repeatable: repeatable, which solves the problem of dirty reading and non repeatable reading of transactions. mysql's default transaction isolation level

  • serializable: serialized read

(3) Phantom reading

Phantom reading: one transaction adds data, and another transaction queries the data in the table several times, but the query results are inconsistent.

4. What problems does transaction propagation in Spring solve and what are the differences between common values?

(1) Concept

The propagation of transactions solves the problem that transactions cannot be controlled due to transaction nesting.

(2) Common values and differences


Meaning: if there is no external transaction, a new transaction will be opened; if there is an external transaction, it will be integrated into the external transaction.

Use in development: used when adding, modifying and deleting.


Meaning: if there is no external transaction, the transaction will not be opened; if there is an external transaction, it will be integrated into the external transaction.

Use in development: used in query.

Tags: Java Spring

Posted on Tue, 16 Nov 2021 11:49:12 -0500 by yazz