springboot (Mybatis integration, transaction)

First, integration MyBatis

  • MyBatis integration is the integration of project work must be done, before integration MyBatis need to integrate DataSource, SpringBoot DataSource provides automatic integration program, make configuration easier, for integration in MyBatis only need a few simple steps you can configure.

1. Procedure

  1. Import dependence
  2. DataSource Configuration
  3. Complete basic components to create: domain, mapper mapper, mapper.xml mapping file, service, table
  4. MyBatis configuration
  5. test

1.1. Import dependence

Connection pooling, mysql driven, and MyBatis packages are introduced in, connection pool selected Ali Druid

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mybatis jar包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!--mysql驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>

    </dependencies>

1.2 Manual configuration - not recommended

  • Manually configure four properties
jdbc:
  username: root 
  password: 123456
  url: jdbc:mysql:///ssm
  driver-class-name: com.mysql.jdbc.Driver

Configuration defined DataSource class

@SpringBootApplication
public class ApplicationConfig {

    //手动配置
    @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class, args);
    }
}

1.3 Automatic Configuration

Omitted domain, mapper, service layer
application.yml located:

#配置DataSource
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql:///springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

#配置mybatis
mybatis:
  #sql:xml文件
  mapper-locations: classpath:cn/itsource/mapper/*.xml
  #配置别名
  type-aliases-package: cn.itsource.domain
server:
  port: 8080

ApplicationConfig configuration class

/**
 * @MapperScan("cn.itsource.mapper"):映射器的自动扫描
 */
@SpringBootApplication
@MapperScan("cn.itsource.mapper")
public class ApplicationConfig {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class,args);
    }
}

Second, integration affairs

  • In Spring we can use annotation transaction configuration, you can also use the xml way transaction configuration, can also use these two methods in SpringBoot, just SpringBoot recommended annotation program, so we are here mainly annotation arranged the transaction.

2.1. Annotations are configured Affairs

Open transaction manager ==

  • After importing the MyBatis dependency, transaction manager has existed, we just need to open transaction management can be configured in class
/**
 * @MapperScan("cn.itsource.mapper"):映射器的自动扫描
 * 
 * @EnableTransactionManagement:开启事务管理
 */
@SpringBootApplication
@MapperScan("cn.itsource.mapper")
@EnableTransactionManagement
public class ApplicationConfig {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class,args);
    }
}

Playing tag in the transaction service (@Transactional have given service function Affairs)

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Transactional(readOnly = true)
    @Override
    public List<Employee> findAll() {
        return employeeMapper.findAll();
    }
}

2.2.xml Configuration Affairs

2.2.1. AOP-dependent introduction of

<!--aop依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2.2.2. Configuration affairs xml

<!-- 配置事物管理器 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    
    <aop:config>
        <aop:pointcut expression="execution(* cn.itsource.web.controller.service..*.*(..))" id="coreServicePointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="coreServicePointcut"/>
    </aop:config> <!-- aop应用事务管理 -->


    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

2.2.4. Xml configuration import configuration class

@ImportResource("classpath:applicationContext-service.xml") 
public class ApplicationConfig{}
Published 33 original articles · won praise 0 · Views 400

Guess you like

Origin blog.csdn.net/weixin_45737653/article/details/104929986