Spring boot configuration mybatis and transaction management

Source: Internet
Author: User

Spring Boot configuration mybatis and transaction management

First, spring boot and MyBatis configuration

1. First, the full dependencies required for spring boot configuration mybatis are as follows:

<!--Spring Boot starts the parent dependency--<Parent><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-parent</Artifactid><Version>1.5.1.release</Version></Parent><!--above the dependencies tag, if you have a parent tag, paste the above part into the parent project--<!--the spring boot project launches the Web dependency that must be introduced, and the following content is dependency-<Dependency><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-web</Artifactid><Version>1.5.1.release</Version></Dependency><!--Spring Boot Mybatis Dependent--<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>   

?? here is the main point of the above database driver package , Oracle and MySQL to introduce different dependencies, do not forget. If you forget to introduce, you will not find the driver class exception.
2. Then, add the following in the application.properties configuration file:

# #数据库连接信息spring .datasource.url=jdbc:oracle:thin:@//192.168.1.171:1521/orclspring.datasource< Span class= "Hljs-preprocessor" >.username=znxdspring.datasource.password=znxdspring.datasource .driver-class-name=oracle.jdbc.driver# #指向mapper的xml文件位置mybatis /*mapper.xml      

?? The top to spring.datasource the beginning of the content in fact there are many, specific, in the application.properties file after the input to see what can be ordered to know.
?? Springboot By default will be Spring.datasource the beginning of the information storage, 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 no must It's going to be. (Configuration druid need to be a @bean, the next one will be said)
?? Of course, if it is particularly detailed, and in the Application.properties file through spring.datasource the point of the relevant configuration, it may need to configure the class or something.
??mybatis.mapper-locations The location you point to is starting with Src/main/resource, and you need to add classpath to the front, which points to where your mapper.xml file is placed.
3. I am mybatis through the database table reverse engineering generated entity classes, Mapper, etc., first put the project specific placement structure is as follows:

The explanation for this is as follows:

    1. The startup class must be placed at the top of the project relative to other classes, and the preceding article says that spring boot does not have a label for the traditional spring project configuration <bean> , and that it scans the bean in turn from the startup class down to the default built-in Tomcat container.
    2. Service placement is located higher than its implementation class placement, if there are two service A and B, the implementation class has Aimpl and Bimpl, if there is Aimpl call B, it is possible to start the container reported "a field named ' B ' not Found" Seemingly this error indicates that the container scan sequence is incorrect.
    3. The best solution to this situation should be as I put the service on high, the container starts scanning into the service first, then scans the impl, so that when the class scan is implemented, the service will certainly be found, and there will be no such error.
    4. Another workaround is to add an additional note when introducing the service: @Lazy
      @Autowired 
      @Lazy //这样就会延迟加载,不需要,这里只是解释这注解 
      LogsUserActiveMapper logsUserActiveMapper;
    5. In short, if there is a call and a call relationship, be sure to remember that the callee is first scanned into the spring boot built-in container, that is, the callee's package location to be placed higher (at least cephalic).

? an annotation needs to be added to the 4.service implementation class @Service . This may not be necessary, I have not tried.
? 5.spring Boot+mybatis In addition to these requirements, you need to add an annotation to the startup class @MapperScan 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  "Cloud.kafka.mapper") //scanning Mapper.xml namespace point to the value of the package location class kafkalogapplication {public static void main (string[] args) {Springapplication.run (kafkalogapplication.class, args);}}   

Here's the value of namespace in my mapper.xml.

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

* * This completes the configuration of the MyBatis.

second, the disposition of the affairs, is to add two annotations on the basis of MyBatis.
1, the required annotations are @EnableTransactionManagement and @Transactional two, they come from the following package:

Spring-tx.jar

The package is actually introduced in front of the configuration MyBatis introduction dependency, which is the following:

<!-- Spring Boot Mybatis 依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version></dependency>

So as long as the upper dependency is introduced, the transaction does not need to be introduced into the package,
2. First, find your service implementation class, plus @Transactional annotations, if you add to the class, then all the methods of the class will be managed by the transaction, if you add to the method, then only the method conforms to the specific transaction. Of course, we are generally added to the method. Because only the increase, deletion and change will require business.
For example, the following method 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 the configuration is complete, the spring boot startup class must open the transaction, and the annotations that open the transaction are @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); }}

This completes the configuration of the transaction.

Next write Spring boot + druid configuration.

Spring boot configuration mybatis and transaction management

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.