Spring boot configuration MyBatis and transaction management __spring

Source: Internet
Author: User
Spring Boot configuration mybatis and transaction management


First, spring boot and MyBatis configuration



1. First, the full dependencies of the spring boot configuration mybatis need are as follows:


<!-Spring Boot startup parent dependency->
<parent>
<groupId> org.springframework.boot </ groupId>
<artifactId> spring-boot-starter-parent </ artifactId>
<version> 1.5.1.RELEASE </ version>
</ parent>
<!-The above content is placed outside the dependencies tag. If you already have a parent tag, paste the above part into the parent project->
<!-The web dependency that must be introduced to start the spring boot project, the following content can be placed in dependency->
<dependency>
     <groupId> org.springframework.boot </ groupId>
     <artifactId> spring-boot-starter-web </ artifactId>
     <version> 1.5.1.RELEASE </ version>
</ dependency>
  <!-Spring Boot Mybatis dependency->
<dependency>
     <groupId> org.mybatis.spring.boot </ groupId>
     <artifactId> mybatis-spring-boot-starter </ artifactId>
     <version> 1.2.0 </ version>
</ dependency>
  <!-Oracle database driver package->
<dependency>
     <groupId> com.oracle </ groupId>
     <artifactId> ojdbc6 </ artifactId>
     <version> 11.2.0.4.0-atlassian-hosted </ version>
</ dependency>

This is mainly to say that the top of the database driver package, Oracle and MySQL introduced different dependencies, don't forget. If you forget to introduce, you will not find a driver class exception.

2. Then, add the following in the Application.properties configuration file:





## Database connection information
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
## Point to the xml file location of mapper
mybatis.mapper-locations = classpath: mapper / * Mapper.xml 


The top of the Spring.datasource beginning with a lot of content, concrete, in the Application.properties file input after looking at the content can be seen.
Springboot By default will spring.datasource the beginning of the information to receive, for DataSource object configuration, and then sqlsessionfactory configuration and other related database connection configuration, so many other people will say what configuration class, those I feel not necessary. (Configuration druid needs to be fitted with a @bean, the next one will say)
Of course, if it is particularly detailed, and in the Application.properties file and through the Spring.datasource point does not have the relevant configuration, it may need to configure the class or something.
The location that mybatis.mapper-locations points to starts with the Src/main/resource, which needs to be preceded by a classpath, which points to the location of your Mapper.xml file placement.

3. I am mybatis through the database table reverse engineering generated entity classes, Mapper, and so on, first of all, the project specific placement structure posted as follows:

the explanation for the above illustration is as follows: the startup class must be placed at the top of the project relative to other classes, and the previous article said that spring boot does not have the <bean> label for the traditional spring project configuration, The way it scans the bean is by scanning the boot class down into the default built-in Tomcat container in turn. The location above the service placement is higher than the placement of the implementation class, if there are two service A and B, the implementation class has Aimpl and Bimpl, if there is a Aimpl call B, it is possible to launch the container "a field named ' B ' not found" Seemingly this error indicates that the container scan sequence is incorrect. The best solution to this situation, should be like I put the service on the high, the container starts first scan into the service, and then scan the Impl, so that in the implementation of class scanning will certainly be able to find service, there will not be such a mistake. Another solution is to add more annotations when introducing a service: @Lazy
@Autowired
@Lazy//This will delay loading, the above figure does not need, here just explain this annotation
Logsuseractivemapper Logsuseractivemapper; In summary, if a call is invoked with a called relationship, it must be remembered that the callee is first scanned into the spring boot built-in container, where the callee's package position is higher (at least for Sianpin).


4. @service annotation needs to be added to the 4.service implementation class. This may not be necessary, I have not tried.

5.spring Boot+mybatis In addition to these requirements, you need to add an annotation @mapperscan to the startup class as follows:





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//If a transaction annotation is added to the service implementation class in MyBatis, you need to add the annotation here
@MapperScan ("Cloud.kafka.mapper")  The package location of namespace pointing to values in Mapper.xml is scanned. Public
class Kafkalogapplication {public
    static void Main (string[] args) { C10/>springapplication.run (Kafkalogapplication.class, args);
    }


The following is the value of namespace in my mapper.xml.





<mapper namespace= "Cloud.kafka.mapper.LogsUserActiveMapper" >

* * This completes the configuration of the MyBatis.


Second, the configuration of the transaction, is to add two annotations on the basis of MyBatis.
1, the need for annotations for @enabletransactionmanagement and @transactional Two, they come from the bottom of this package:


Spring-tx.jar



The package is actually introduced in front of the configuration MyBatis when it is introduced, that is, the bottom one:





<!--Spring Boot mybatis dependent-->
<dependency>
<groupid>org.mybatis.spring.boot</groupid >
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version >
</dependency>


So as long as the above dependencies are introduced, the transaction does not need to be introduced into the package,
2. First, find your service implementation class, plus the @transactional annotation, if you add to the class, then all the methods of that class will be managed by the transaction, if you add to the method, that only the method conforms to the specific transaction. Of course, we are usually added to the method. Because only by adding, deleting and changing will we need business.
For example, one of the following methods of inserting data adds a transaction:





@Override
@Transactional (propagation = Propagation.required,isolation = isolation.default,timeout=36000, Rollbackfor=exception.class) Public
Integer Add (Cbf_jtcy t) {return
    Cbf_jtcymapper.insert (t);
}


If you do not know what is inside, you can view this article, Spring,mybatis Transaction management configuration and @transactional annotation use


3. After configuration, the spring boot startup class must open the transaction, and the annotation that opens the transaction is @enabletransactionmanagement, as follows:





@SpringBootApplication
@EnableTransactionManagement 
@MapperScan ("Microservice.qssj.mapper")//must add this, No error, if not, you can also add @mapper comments on each mapper, and here also to fill out a note, that I forget, I have been using this annotation public
class Qssjserviceapplication {
    public static void Main (string[] args) {
        springapplication.run (qssjserviceapplication.class, args);
    }


This completes the configuration of the transaction.



The next article writes the spring Boot + druid configuration.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.