码迷,mamicode.com
首页 > 数据库 > 详细

Springboot使用jdbc以及事务支持

时间:2019-10-19 23:29:26      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:innodb   cto   bean   scope   view   mic   cli   tle   ota   

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

 

Springboot使用jdbc以及事务支持

标签:innodb   cto   bean   scope   view   mic   cli   tle   ota   

原文地址:https://www.cnblogs.com/itivy/p/11706251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!