springboot-data-jpa

创建一个springboot项目
springboot-data-jpa_第1张图片
创建一个实体类

package com.gm.bean;

import javax.persistence.*;

/**
 * @Author Administrator
 * @Date 2020/6/19 15:54
 **/


/*
* @Table 指定和哪个数据表进行映射,如果没有数据表的情况下
* 就是指定创建的一个表名,如果不给值,表明默认使用类名,并且类名首字母小写
* */



//使用JPA注解来去配置映射关系的实体
@Entity  //告诉JPA这是一个实体类,和数据表关系类映射的实体
@Table(name = "tb_User")
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})
public class User {

    @Id //这是一个主键
    @GeneratedValue(strategy = GenerationType.IDENTITY) //主键自增
    private Integer id;

    @Column(name = "user_name", length = 30)//这是和数据库表字段相对应的属性
    private String userName;

    @Column //省略参数,默认使用属性名作为数据表的类名
    private String password;

    @Column
    private String address;

    @Column
    private String birth;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", address='" + address + '\'' +
                ", birth='" + birth + '\'' +
                '}';
    }

    public User() {
    }

    public User(Integer id, String userName, String password, String address, String birth) {
        this.id = id;
        this.userName = userName;
        this.password = password;
        this.address = address;
        this.birth = birth;
    }

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }
}

创建application.yml文件

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/db_gm?useSSL=true&characterEncoding=utf-8&&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver


  jpa:
    hibernate:
      #更新或者创建数据表
      ddl-auto: update
    #控制台显示sql
    show-sql: true

写一个继承JpaRepository的接口

package com.gm.repository;

import com.gm.bean.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @Author Administrator
 * @Date 2020/6/19 16:22
 **/
public interface UserRepository extends JpaRepository<User,Integer> {

}

写一个controller层(调用增加,查询方法):

package com.gm.controller;

import com.gm.bean.User;
import com.gm.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.RestController;

/**
 * @Author Administrator
 * @Date 2020/6/19 16:24
 **/
@RestController
public class UserController {
    @Autowired
    UserRepository userRepository;


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

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

}

注:初次调用根据id查询单个,会报错,JSON错误,在实体类加一个@JsonIgnoreProperties(value = { “hibernateLazyInitializer”})注解即可解决

你可能感兴趣的:(springboot-data-jpa)