SpringBoot数据库增删改查 事务管理


数据库依赖


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

		
			mysql
			mysql-connector-java
		



application.xml


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


ddl-auto: create
每次都会重新创建表 如果之前存在该表 则会先删除


Hibernate: drop table if exists girl
Hibernate: create table girl (id integer not null auto_increment, age integer, cup_size varchar(255), primary key (id))



ddl-auto: update
只是更新 之前的数据并不会消失


ddl-auto: create-frop
应用停下来的时候将会删除表


ddl-auto: none
默认 什么都不做


ddl-auto: validate
验证类里面的熟悉跟表结构是否一致 如果不一致的话会报错

实体类

Girl.java

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

/**
 * Created by Jeremy_Lin on 2017/6/17.
 */
@Entity
public class Girl {

    @Id
    @GeneratedValue
    private Integer id;

    private String cupSize;

    private Integer age;

    public Girl() {

    }

    public Integer getId() {
        return id;
    }

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

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

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

}


GirlRepository.java

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

import java.util.List;

public interface GirlRepository extends JpaRepository {

    // 通过年龄来查询
    // 有可能查询到多个
    // 所以返回List
    // 方法名不能乱写
    // 有格式要求
    public List findByAge(Integer age);

}



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Jeremy_Lin on 2017/6/18.
 */
@Service
public class GirlService {

    @Autowired
    private GirlRepository girlRepository;

    // 事务管理
    @Transactional
    public void insertTwo() {

        Girl girlA = new Girl();
        girlA.setCupSize("A");
        girlA.setAge(18);
        girlRepository.save(girlA);

        Girl girlB = new Girl();
        girlB.setCupSize("BBBB");
        girlB.setAge(19);
        girlRepository.save(girlB);

    }

}





GirlController.java

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

import java.util.List;

/**
 * Created by Jeremy_Lin on 2017/6/17.
 */
@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    /**
     * 查询所有女生列表
     * @return
     */
    @GetMapping(value="/girls")
    public List girlList() {
        return girlRepository.findAll();
    }

    /**
     * 添加一个女生
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping(value="/girls")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
                          @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);

    }

    // 查询一个女生
    @GetMapping(value="/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id) {
        return girlRepository.findOne(id);
    }

    // 更新
    @PutMapping(value="girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                            @RequestParam("cupSize") String cupSize,
                            @RequestParam("age") Integer age) {

        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);

    }

    // 删除
    @DeleteMapping(value="girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id) {
        girlRepository.delete(id);
    }

    // 通过年龄查询女生列表
    @GetMapping(value="/girls/age/{age}")
    public List girlListByAge(@PathVariable("age") Integer age) {
        return girlRepository.findByAge(age);
    }

    @PostMapping(value="/girls/two")
    public void girlTwo() {
        girlService.insertTwo();
    }

}



代码地址:http://download.csdn.net/detail/duanliuchang/9873773










你可能感兴趣的:(SpringBoot)