springboot-data-jpa

创建一个springboot项目
在这里插入图片描述
创建一个实体类

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”})注解即可解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值