springboot整合JPA

1.编写一个实体类bean和数据表进行映射,并且配置好映射关系

package com.seegot.springboot06datajpa.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @program: springboot-06-data-jpa
 * @description:
 * @author: PP Zhang
 * @create: 2020-04-23 10:09
 */
// 配置映射关系
@Data
@Entity // 告诉JPA这是一个实体类(和数据库表映射的类)
@Table(name = "tb_user") // @Table 是来制定和哪个数据库表对应,如果省略默认表名就是类名的小写user;
public class User {
    @Id // 这是一个主键
    @GeneratedValue(strategy =  GenerationType.IDENTITY) // 自增主键
    private Integer id;
    @Column(name = "last_name",length = 50) // 这是和数据表对应的一个列,列明为last_name 长度为50
    private String lastName;
    @Column //省略@Column 的时候 列明就是属性名。
    private String email;
}

2.编写一个Dao接口来操作实体类对应的数据表(Repository)

package com.seegot.springboot06datajpa.repository;

import com.seegot.springboot06datajpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @program: springboot-06-data-jpa
 * @description:
 * @author: PP Zhang
 * @create: 2020-04-23 10:18
 */
// 继承JpaRepository,来完成对数据库的操作。
// 其中两个泛型,User是我们要操作的模型,Integer是要操作模型所对应的的表的主键类型。
public interface UserRepository extends JpaRepository<User,Integer> {
}

3.基本的配置(参照 JpaProperties)

spring:
  datasource:
    url: jdbc:mysql://192.168.100.158/jps
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      # 更新 或者 创建数据库表,如果模型更新,那么启动程序后,数据库表会跟着改变,如果是新创建的模型,那么启动后会自动创建对应的数据库表
      ddl-auto: update
      # 每一个sql都显示出来,每次进行增删改查的时候在控制台显示。
    show-sql: true

4.Controller调用jpa

package com.seegot.springboot06datajpa.controller;

import com.seegot.springboot06datajpa.entity.User;
import com.seegot.springboot06datajpa.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;

/**
 * @program: springboot-06-data-jpa
 * @description:
 * @author: PP Zhang
 * @create: 2020-04-23 10:54
 */
@RestController
public class UserController {
    @Autowired
    UserRepository userRepository;
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
        User user = userRepository.findById(id).orElse(null);
        return  user;
    }
    @GetMapping("/user")
    public User insertUser(User user){
        userRepository.save(user);
        return  user;
    }
}

添加:http://localhost:8080/user/?lastName=lisi&email=bb

查询:http://localhost:8080/user/1

感觉好简洁,我都没有编写任何增删改查的语句,就直接实现了相关操作。

1.编写一个实体类bean和数据表进行映射,并且配置好映射关系

package com.seegot.springboot06datajpa.entity;

import lombok.Data;

import javax.persistence.*;

/**
* @program: springboot-06-data-jpa
* @description:
* @author: PP Zhang
* @create: 2020-04-23 10:09
*/
// 配置映射关系
@Data
@Entity // 告诉JPA这是一个实体类(和数据库表映射的类)
@Table(name = "tb_user") // @Table 是来制定和哪个数据库表对应,如果省略默认表名就是类名的小写user;
public class User {
   @Id // 这是一个主键
   @GeneratedValue(strategy =  GenerationType.IDENTITY) // 自增主键
   private Integer id;
   @Column(name = "last_name",length = 50) // 这是和数据表对应的一个列,列明为last_name 长度为50
   private String lastName;
   @Column //省略@Column 的时候 列明就是属性名。
   private String email;
}

 

2.编写一个Dao接口来操作实体类对应的数据表(Repository)

package com.seegot.springboot06datajpa.repository;

import com.seegot.springboot06datajpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
* @program: springboot-06-data-jpa
* @description:
* @author: PP Zhang
* @create: 2020-04-23 10:18
*/
// 继承JpaRepository,来完成对数据库的操作。
// 其中两个泛型,User是我们要操作的模型,Integer是要操作模型所对应的的表的主键类型。
public interface UserRepository extends JpaRepository<User,Integer> {
}

3.基本的配置(参照 JpaProperties)

spring:
datasource:
  url: jdbc:mysql://192.168.100.158/jps
  username: root
  password: root
  driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
  hibernate:
     # 更新 或者 创建数据库表,如果模型更新,那么启动程序后,数据库表会跟着改变,如果是新创建的模型,那么启动后会自动创建对应的数据库表
    ddl-auto: update
     # 每一个sql都显示出来,每次进行增删改查的时候在控制台显示。
  show-sql: true

4.Controller调用jpa

package com.seegot.springboot06datajpa.controller;

import com.seegot.springboot06datajpa.entity.User;
import com.seegot.springboot06datajpa.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;

/**
* @program: springboot-06-data-jpa
* @description:
* @author: PP Zhang
* @create: 2020-04-23 10:54
*/
@RestController
public class UserController {
   @Autowired
   UserRepository userRepository;
   @GetMapping("/user/{id}")
   public User getUser(@PathVariable("id") Integer id){
       User user = userRepository.findById(id).orElse(null);
       return  user;
  }
   @GetMapping("/user")
   public User insertUser(User user){
       userRepository.save(user);
       return  user;
  }
}

 

posted @ 2020-04-23 11:39  。低调ヽ继续  阅读(244)  评论(0编辑  收藏  举报