zoukankan      html  css  js  c++  java
  • Springboot使用jdbc以及事务支持

    1.依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    View Code

    2.配置属性

    spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.username=ivy
    spring.datasource.password=*******
    View Code

    3.数据表结构

    CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `age` int(11) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8
    View Code

    4.项目结构

    项目结构

    5.源码

    (1).Domain

    package com.example.demo.jdbc_tx.domain;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private Integer id;
    
        private String name;
    
        private Integer age;
    
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
    View Code

    (2).Mapper

    package com.example.demo.jdbc_tx.mapper;
    
    
    import com.example.demo.jdbc_tx.domain.User;
    import org.springframework.jdbc.core.RowMapper;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class UserMapper implements RowMapper {
        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            return new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getInt("age"));
        }
    }
    View Code

    (3).Service接口

    package com.example.demo.jdbc_tx.mapper;
    
    
    import com.example.demo.jdbc_tx.domain.User;
    import org.springframework.jdbc.core.RowMapper;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class UserMapper implements RowMapper {
        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            return new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getInt("age"));
        }
    }
    View Code

    (4).Service实现

    package com.example.demo.jdbc_tx.service.impl;
    
    import com.example.demo.jdbc_tx.domain.User;
    import com.example.demo.jdbc_tx.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImp implements UserService {
        private final JdbcTemplate jdbcTemplate;
    
        @Autowired
        public UserServiceImp(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        @Override
        public void save(User user) {
            jdbcTemplate.update("insert into user(name, age) values (?, ?)", user.getName(), user.getAge());
            throw new RuntimeException("事务异常,回滚!");
        }
    }
    View Code

     (5).Controller测试

    package com.example.demo.jdbc_tx.controller;
    
    import com.example.demo.jdbc_tx.domain.User;
    import com.example.demo.jdbc_tx.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("user")
    public class UserController {
        private final UserService userService;
    
        @Autowired
        public UserController(UserService userService) {
            this.userService = userService;
        }
    
        @GetMapping("save")
        public void save(User user) {
            userService.save(user);
        }
    }
    View Code
  • 相关阅读:
    使用protobuf生成代码import包找不到
    SQL 对邮箱数据的处理(分类统计)
    Hive 集合函数 collect_set() collect_list()
    菜谱分享网站微信小程序开发说明(1)-介绍与运行
    windows 下查看端口占用
    Windows下安装Maven自定义仓库配置阿里下载源,配置Ecplise、IDEA
    Windows下安装Node.js完整详细教程
    开启Centos8的SSH服务
    Openwrt与IPTV之二----udpxy
    Openwrt与IPTV之一----igmpproxy
  • 原文地址:https://www.cnblogs.com/itivy/p/11706251.html
Copyright © 2011-2022 走看看