SpringBoot 数据库操作、事务

Spring-Data_Jpa


1、在pom.xml中加入:

                <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
引入spring-data-jpa和mysql驱动


2、修改配置文件

spring:
  profiles:
    active: dev
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/chenjie
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true


3、新建实体类

package cn.edu.shu.ces_chenjie;



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Integer id;
    private Integer age;
    private String name;

    public Person() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}


4、新建类

PersonRepository
package cn.edu.shu.ces_chenjie;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface PersonRepository extends JpaRepository<Person,Integer>{
    public List<Person> findByName(String name);
 }


5、新建类(

@Transactional
使用到了事务)

package cn.edu.shu.ces_chenjie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;

@Component
public class PersonService {

    @Autowired
    private PersonRepository repository;

    @Transactional
    public void insertTwo(){
        Person personA = new Person();
        personA.setName("renbo");
        personA.setAge(18);
        repository.save(personA);

        Person personB = new Person();
        personB.setName("bobo");
        personB.setAge(20);
        repository.save(personB);

    }
}

6、新建类

package cn.edu.shu.ces_chenjie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class PersonController {
    @Autowired
    private PersonRepository repository;

    @Autowired
    private PersonService personService;

    @GetMapping("/persons")
    public List<Person> personList(){
        return repository.findAll();
    }

    @PostMapping()
    public String personAdd(@RequestParam("age") Integer age,@RequestParam("name") String name){
        Person person = new Person();
        person.setAge(age);
        person.setName(name);
        return repository.save(person).toString();
    }

    @GetMapping("persons/{name}")
    public List<Person> personListByName(@PathVariable("name")String name){
        return repository.findByName(name);
    }

    @PostMapping("persons/two")
    public void personTwo(){
        personService.insertTwo();
    }
}

7、使用IDEA 自带的 Tools->Test RESTful web service工具或者chrome postman插件测试



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值