java spring cloud version b2b2c social electricity supplier spring cloud distributed micro-services (seven) springboot open declarative transaction

springboot open transaction is very simple, just a comment @Transactional it. Because springboot trouble Affairs has been enabled by default for jpa, jdbc, mybatis, they rely on the introduction of the time, things just turned on by default. Of course, if you need to use other orm, such as beatlsql, it needs its own configuration-related things Manager.

Preparation Phase

Code article above as an example, that springboot integration mybatis, the article is to make data access layer mybatis based annotation, based on the article to achieve xml and open declarative transaction.

Environmental dependent

Mybatis introduced in the pom file depend on startup:

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
</dependency>

Mysql dependent on the introduction of

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

Database initialization script

-- create table `account`
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');

Configuration data source

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
mybatis.type-aliases-package=com.forezp.entity

Mapper to indicate by configuring mybatis.mapper-locations xml file location, I was placed in the resources / mybatis file. Where mybatis.type-aliases-package package to identify and map database entity.

After the above steps, springboot can access the database via mybatis.

Creating the Entity Classes

public class Account {
    private int id ;
    private String name ;
    private double money;
 
    getter..
    setter..
 
  }

Social e-commerce platform source code, please add penguin beg: 3536247259

 

Guess you like

Origin www.cnblogs.com/springfresh/p/10996034.html