007-SpringBoot整合Mybatis---(@Transactional注解事务)

1.@Transactional注解

1.1 spring有编程式事务和声明式事务:

  • 一般都比较推荐使用声明式事务。因为编程式事务与业务代码具有一定的耦合性质,在做改动的时候势必会牵连到主业务,所以一般都会使用推荐的声明式事务,即使用注解的方法来进行结构。这篇只看声明式事务的办法。即@Transactional注解

1.2 @Transactional的用法

使用@Transactional注解都是用在类的方法上。官网也不建议使用在接口类上面,注解肯定都是用到了aop的思想,即使用了动态代理。而如果使用cglib动态代理肯定没有办法代理接口类。所以我们真正使用的时候都在类的方法上面。并且只应用在DML上,DQL不需

2 创建module

2.1 011-springboot-mybatis-transactional

007-SpringBoot整合Mybatis---(@Transactional注解事务)_第1张图片

3.代码编写

3.1 导入依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.5version>
        <relativePath/> 
    parent>
    <groupId>com.zzy.springbootgroupId>
    <artifactId>011-springboot-mybatis-transactionalartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>011-springboot-mybatis-transactionalname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.20version>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

3.2 编写核心配置文件

#设置端口号和上下文根
server.port=8091
server.servlet.context-path=/trans

#配置数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDateTimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

#设置实体类别名
mybatis.type-aliases-package=com.zzy.springboot.pojo
#开启驼峰命名法
mybatis.configuration.map-underscore-to-camel-case=true

#指定 Mybatis 映射文件的路径
mybatis.mapper-locations=classpath:dao/*.xml

3.3 编写实体类

package com.zzy.springboot.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.Alias;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Alias("student")
public class Student {

    private Integer id;

    private String name;

    private Integer age;
}

3.4 编写Dao接口

package com.zzy.springboot.dao;

import com.zzy.springboot.pojo.Student;

import java.util.List;

public interface StudentDao {


    int insertStu(Student student);

    List<Student> findAll();
}

007-SpringBoot整合Mybatis---(@Transactional注解事务)_第2张图片

3.5 编写接口映射xml文件



<mapper namespace="com.zzy.springboot.dao.StudentDao">

  <select id="findAll" resultType="student">
    select * from t_student
  select>

  <insert id="insertStu" parameterType="student">
    insert into t_student values (null,#{name},#{age})
  insert>
mapper>

007-SpringBoot整合Mybatis---(@Transactional注解事务)_第3张图片
注意:接口包结构和映射文件包结构需要一致

3.6 编写servicey以及实现类

package com.zzy.springboot.service;

import com.zzy.springboot.pojo.Student;

import java.util.List;

public interface StudentService {

    public List<Student> findAll();

    int saveStu(Student student);
}

package com.zzy.springboot.service.impl;

import com.zzy.springboot.dao.StudentDao;
import com.zzy.springboot.pojo.Student;
import com.zzy.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;


    @Override
    public List<Student> findAll() {

        return studentDao.findAll();
    }

    @Override
    public int saveStu(Student student) {
        return studentDao.insertStu(student);
    }


}

007-SpringBoot整合Mybatis---(@Transactional注解事务)_第4张图片

3.7 编写Controller

package com.zzy.springboot.web;

import com.zzy.springboot.pojo.Student;
import com.zzy.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/springboot")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/findAll")
    public List<Student> findAll(){
       List<Student> list =  studentService.findAll();
        return list;
    }

    @RequestMapping(value = "/save")
    public int saveStu(){
        Student student = new Student();
        student.setName("james");
        student.setAge(22);
        return studentService.saveStu(student);
    }
}

3.8 运行

在这里插入图片描述

3.9 访问

http://localhost:8091/trans/springboot/findAll
http://localhost:8091/trans/springboot/save

007-SpringBoot整合Mybatis---(@Transactional注解事务)_第5张图片
007-SpringBoot整合Mybatis---(@Transactional注解事务)_第6张图片

你可能感兴趣的:(SpringBoot,java)