SpringBoot学习笔记三 | 整合JPA

JPA全称(Java Persistence API),JPA 的目标之一是制定一个可以由很多供应商实现的API,不了解的小伙伴...emm虽然没人看,,可以看一下我的JPA系列学习笔记,JPA学习笔记emm..日后再改。
整个pom文件



    4.0.0

    com.yjj
    sb-first
    0.0.1-SNAPSHOT
    jar

    sb-first
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.0.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        

        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



配置文件application.yml加入

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456

  jpa:
    hibernate:
      ddl-auto: update  # 第一次简表create  后面用update
    show-sql: true

注意,如果通过jpa在数据库中建表,将jpa.hibernate,ddl-auto改为create,建完表之后,要改为update,要不然每次重启工程会删除表并新建。

创建实体类

通过@Entity 表明是一个映射的实体类, @Id表明id, @GeneratedValue 字段自动生成

package com.yjj.sbfirst.entity;

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

@Table(name="account")
@Entity
public class Account {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private double money;

    //省略getter setter
}

service层

package com.yjj.sbfirst.repository;
import com.yjj.sbfirst.entity.Account;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AccountRepository extends JpaRepository{

}

Controller层

package com.yjj.sbfirst.controller;

import com.yjj.sbfirst.entity.Account;
import com.yjj.sbfirst.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/account")
public class AccountController {
    @Autowired
    AccountRepository accountRepository;

    @RequestMapping(value="/list")
    public List getAccounts(){
        return accountRepository.findAll();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String getAccountById(@PathVariable("id") int id){
        return accountRepository.findById(0).get().toString();
    }

    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public String updateAccount( @PathVariable("id") int id,@RequestParam(value="name",required = true) String name,
                                @RequestParam(value="money",required = true) double money){
        Account account=new Account();
        account.setMoney(money);
        account.setName(name);
        account.setId(id);
        Account account1=accountRepository.saveAndFlush(account);
        return account1.toString();
    }


}

以上代码均测试过,就是有的没通过。。emm。。findOne和findByID返回Optional是怎么搞啊。。
查询全部和更新是可以的。嗯,暂时到这里

你可能感兴趣的:(SpringBoot学习笔记三 | 整合JPA)