SpringBoot 数据库操作、事务

Spring-Data_Jpa


1、在pom.xml中加入:

                
			org.springframework.boot
			spring-boot-starter-data-jpa
		

		
			mysql
			mysql-connector-java
		
引入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{
    public List 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 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 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插件测试



你可能感兴趣的:(Java)