6_5.springboot2.x数据整合springData JPA

1、配置文件

pom.xml


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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

 

2、application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      # 更新或者创建数据表
      ddl-auto: update
      # 展示sql
    show-sql: true

  main:
    allow-bean-definition-overriding: true

3、新建pojo

使用JPAz注解配置映射关系

package com.spoot.springboot.pojo;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;

//使用JPAz注解配置映射关系
//@Entity告诉JPA这是一个实体类和数据表映射的类 @Table指定和哪个表对应;如果省略表明就是user
@Entity
@Table(name = "tb_user")
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class User {
    //这是一个主键.同时标注这是一个自增主键
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "last_name",length = 255)
    private String lastName;
    @Column
    private String email;

    public Integer getId() {
        return id;
    }

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

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })

fasterxml.jackson将对象转换为json

4、编写repository接口

package com.spoot.springboot.repository;

import com.spoot.springboot.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;

//继承JpaRepository完成数据的操作 JpaRepository<User,Integer> 泛型操作的类和主键
public interface UserRepository extends JpaRepository<User,Integer> {



}

继承JpaRepository完成数据的操作 JpaRepository<User,Integer> 泛型操作的类和主键

5、controller

package com.spoot.springboot.controller;

import com.spoot.springboot.pojo.User;
import com.spoot.springboot.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping("/user/{id}")
    public User getUset(@PathVariable("id") Integer id){
        User user = userRepository.getOne(id);
        return user;
    }

    @GetMapping("/user")
    public User getUset(User user){
        User save = userRepository.save(user);
        return save;
    }
}

在yml中配置 ddl-auto: update会自动更新和创建表

测试:

OK!

posted @ 2019-10-06 13:12  杰醍奋  阅读(138)  评论(0编辑  收藏  举报