• 新版SpringBoot-Spring-Mybatis事务控制


    快速创建SpringBoot+Spring+Mybatis项目

    https://start.spring.io

    删除pom中mysql依赖的runtime

    pom.xml中添加druid依赖

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.23</version>
            </dependency>

    数据库连接配置文件

    application.properties

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/cybclass?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    spring.datasource.username=root
    spring.datasource.password=root
    # 使用阿里巴巴druid数据源,默认使用自带
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    #开启控制台打印sql
    mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

    启动类上添加扫描注解

    UserController.java

    package net.ybclass.demo.controller;
    
    import net.ybclass.demo.domain.User;
    import net.ybclass.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api/v1/user")
    public class UserController {
        @Autowired(required = false)
        private UserService userService;
        @RequestMapping("save")
        public Object save(){
            User user=new User();
            user.setId(11);
            user.setName("cyb");
            user.setPwd("423");
            user.setPhone("123456789");
            userService.save(user);
            return user;
        }
    }

    User.java

    package net.ybclass.demo.domain;
    
    public class User {
        private int id;
        private String name;
        private String pwd;
        private String phone;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }

    UserMapper.java

    package net.ybclass.demo.mapper;
    
    import net.ybclass.demo.domain.User;
    import org.apache.ibatis.annotations.Insert;
    import org.springframework.stereotype.Repository;
    
    @Repository //让spring扫描到
    public interface UserMapper {
        @Insert("INSERT INTO user (name,pwd,phone) VALUES (#{name},#{pwd},#{phone})")
        int save(User user);
    }

    UserService.java

    package net.ybclass.demo.service;
    
    import net.ybclass.demo.domain.User;
    
    public interface UserService {
        int save(User user);
    }

    UserServicceImpl.java

    package net.ybclass.demo.service.impl;
    
    import net.ybclass.demo.domain.User;
    import net.ybclass.demo.mapper.UserMapper;
    import net.ybclass.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service //给Spring扫描
    public class UserServiceImpl implements UserService {
        @Autowired(required = false)
        private UserMapper userMapper;
        @Override
        public int save(User user) {
    
            return userMapper.save(user);
        }
    }

    演示

    开始事务

    在启动类上加注解:@EnableTransactionManagement

    在业务类上加:@Transactional

  • 相关阅读:
    poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)
    Git-删除本地文件夹的repository(本地仓库)
    Unique path ii
    jeecms使用小结
    jeecms9自定义标签以及使用新创建的数据库表
    Jeecms网站直接访问html静态页面
    jeecms系统使用介绍——jeecms中的内容、栏目、模型之间的关系
    jeecms内容管理系统使用了哪些技术
    jeecms附件标签用法
    jeecms v8 网站访问量配置
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13311267.html
Copyright © 2020-2023  润新知