How to configure mybatis and transaction management in springboot
1. Configuration of spring boot and mybatis
1. First of all, all the dependencies required for spring boot to configure mybatis are as follows:
<!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <!--以上内容放在dependencies标签外,如果你有已经有父标签,那将以上部分粘贴到父项目中--> <!-- spring boot 项目启动必须引入的web依赖,以下内容放在dependency即可 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.1.RELEASE</version> </dependency> <!-- Spring Boot Mybatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- oracle的数据库驱动包 --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.4.0-atlassian-hosted</version> </dependency>
Here we mainly talk about the above database driver package, oracle and Mysql introduces different dependencies, don’t forget them. If you forget to introduce it, a driver class not found exception will be reported.
2. Then, add the following content to the application.properties
configuration file:
##数据库连接信息 spring.datasource.url=jdbc:oracle:thin:@//192.168.1.171:1521/orcl spring.datasource.username=znxd spring.datasource.password=znxd spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver ##指向mapper的xml文件位置 mybatis.mapper-locations=classpath:mapper/*Mapper.xml ##指向实体类的位置 mybatis.type-aliases-package=cloud.user.models.*
The content starting with spring.datasource
above actually has There are many, for details, just enter them in the application.properties
file and see what you can click on.
By default, springboot will collect the information starting with spring.datasource, configure the DataSource object, and then configure the sqlSessionFactory and other related database connection configurations, so many other people will talk about what configuration class to write. , ones that I feel are unnecessary. (Configuring druid requires a @Bean)
Of course, if it is particularly detailed, and the relevant configuration cannot be clicked through spring.datasource
in the application.properties file , then you may need to configure classes or something.
mybatis.mapper-locations
The location pointed to starts from src/main/resource. Classpath needs to be added in front. It points to the location where your mapper.xml file is placed. .
3. I am the entity class, mapper, etc. generated by mybatis through reverse engineering of the database table. First, I will post the specific placement structure of the project as follows:
The explanation for the above picture is as follows:
The startup class must be placed at the highest point in the project relative to other classes. As mentioned in the previous article, spring boot does not have the traditional spring project configuration In the above picture, the service is placed higher than its implementation class. If there are two services A and B, and there are Aimpl and Bimpl in the implementation class, if Aimpl calls B, it may be in When starting the container, "a field named ‘B’ not found" appears. This error appears, indicating that the container scanning sequence is wrong. The best solution to this situation is to put the service at a high place like I did. When the container starts, it scans the service first, and then scans the impl, so that it can be found when implementing class scanning. service, this error will not occur. Another solution is to add an additional annotation when introducing the service: @Lazy In short, if there is a relationship between calling and being called, you must remember that the callee must first be scanned into the spring boot built-in container , that is, the callee's package position should be placed higher (the positions must be at least equal). 4. A @Service annotation needs to be added to the service implementation class. This may not be necessary, I haven't tried it yet. 5.spring boot mybatis In addition to these requirements, you also need to add an annotation @MapperScan to the startup class, as follows: The following is the value of namespace in my mapper.xml **This completes the configuration of mybatis. is to add two annotations on the basis of mybatis 1. The required annotations are spring-tx.jar This package is actually in When configuring mybatis to introduce dependencies, they have been automatically introduced, which is the following: So as long as the above dependencies are introduced, the transaction does not need to introduce the package again, 2. First, Find your service implementation class and add the For example, the following method of inserting data adds a transaction: If you don’t know what is inside, you can check this article, spring, mybatis transaction management configuration and the use of @Transactional annotation 3. After configuration, the spring boot startup class must enable transactions, and the annotation used to enable transactions is This is done Transaction configuration. The above is the detailed content of How to configure mybatis and transaction management in springboot. For more information, please follow other related articles on the PHP Chinese website!@Autowired
@Lazy //这样就会延迟加载,上图不需要,这里只是解释这注解
LogsUserActiveMapper logsUserActiveMapper;
package cloud.kafka;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement //如果mybatis中service实现类中加入事务注解,需要此处添加该注解
@MapperScan("cloud.kafka.mapper") //扫描的是mapper.xml中namespace指向值的包位置
public class KafkaLogApplication {
public static void main(String[] args){
SpringApplication.run(KafkaLogApplication.class, args);
}
}
<mapper namespace="cloud.kafka.mapper.LogsUserActiveMapper" >
2. Transaction configuration
@ EnableTransactionManagement
and @Transactional
are two, they come from the following package: <!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
@Transactional
annotation. If you add it to the class, then all methods of the class will be managed by transactions. If you add it to the method, then only the method conforms to the specific affairs. Of course, we usually add it to the method. Because only adding, deleting, and modifying will require transactions. @Override
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public Integer add(Cbf_jtcy t) {
return cbf_jtcyMapper.insert(t);
}
@EnableTransactionManagement
, as follows: @SpringBootApplication
@EnableTransactionManagement
@MapperScan("microservice.qssj.mapper")//必须加这个,不加报错,如果不加,也可以在每个mapper上添加@Mapper注释,并且这里还要多填一个注释,那个我忘了,我一直用这个注解
public class QssjServiceApplication {
public static void main(String[] args) {
SpringApplication.run(QssjServiceApplication.class, args);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

Several ways to implement batch deletion statements in MyBatis require specific code examples. In recent years, due to the increasing amount of data, batch operations have become an important part of database operations. In actual development, we often need to delete records in the database in batches. This article will focus on several ways to implement batch delete statements in MyBatis and provide corresponding code examples. Use the foreach tag to implement batch deletion. MyBatis provides the foreach tag, which can easily traverse a set.

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

JPA and MyBatis: Function and Performance Comparative Analysis Introduction: In Java development, the persistence framework plays a very important role. Common persistence frameworks include JPA (JavaPersistenceAPI) and MyBatis. This article will conduct a comparative analysis of the functions and performance of the two frameworks and provide specific code examples. 1. Function comparison: JPA: JPA is part of JavaEE and provides an object-oriented data persistence solution. It is passed annotation or X

Detailed explanation of how to use MyBatis batch delete statements requires specific code examples. Introduction: MyBatis is an excellent persistence layer framework that provides rich SQL operation functions. In actual project development, we often encounter situations where data needs to be deleted in batches. This article will introduce in detail how to use MyBatis batch delete statements, and attach specific code examples. Usage scenario: When deleting a large amount of data in the database, it is inefficient to execute the delete statements one by one. At this point, you can use the batch deletion function of MyBatis

Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? During the development process, efficient data access has always been one of the focuses of programmers. For persistence layer frameworks like MyBatis, caching is one of the key methods to improve data access efficiency. MyBatis provides two caching mechanisms: first-level cache and second-level cache. The first-level cache is enabled by default. This article will introduce the mechanism of MyBatis first-level cache in detail and provide specific code examples to help readers better understand

MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.

Analysis of MyBatis' caching mechanism: The difference and application of first-level cache and second-level cache In the MyBatis framework, caching is a very important feature that can effectively improve the performance of database operations. Among them, first-level cache and second-level cache are two commonly used caching mechanisms in MyBatis. This article will analyze the differences and applications of first-level cache and second-level cache in detail, and provide specific code examples to illustrate. 1. Level 1 Cache Level 1 cache is also called local cache. It is enabled by default and cannot be turned off. The first level cache is SqlSes
