0
点赞
收藏
分享

微信扫一扫

springdata简介+springboot整合JPA不用编写增删改查数据库


springdata简介+springboot整合JPA不用编写增删改查数据库_spring

springdata简介+springboot整合JPA不用编写增删改查数据库_spring_02

  • 引入spring-boot-starter-data-jpa
  • 配置文件打印SQL语句
  • 创建Entity标注JPA注解
  • 创建Repository接口继承JpaRepository
  • 测试方法

 

1\新建工程

springdata简介+springboot整合JPA不用编写增删改查数据库_mysql_03

springdata简介+springboot整合JPA不用编写增删改查数据库_mysql_04

springdata简介+springboot整合JPA不用编写增删改查数据库_spring_05

application.properties中写入

spring.datasource.url=jdbc:mysql://192.168.15.22/jpa
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

2\新建数据库

springdata简介+springboot整合JPA不用编写增删改查数据库_数据库_06

3\整合spring data jpa

JPA:ORM(Object Relational Mapping)对象关系映射

       1)编写一个实体类(Bean)和数据表进行映射

com.example.springbootdatajpa包中新建entity包User类

springdata简介+springboot整合JPA不用编写增删改查数据库_mysql_07

package com.example.springbootdatajpa.entity;

import javax.persistence.*; //注意

//使用JPA注解配置映射关系
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "tbl_user") //@Table来指定和哪个数据表对应,如果省略默认表明就是user类名小写
public class User {
@Id //这是一个主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键自增
private Integer id;

@Column(name = "last_name",length = 50) //这是和数据表对应的一个列
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;
}
}

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

新建repository包UserRepository接口

package com.example.springbootdatajpa.repository;

import com.example.springbootdatajpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

//继承JpaRepository来完成对数据库的操作,User类,主键类型是Integer
public interface UserRepository extends JpaRepository<User,Integer> {
}

3)基本的配置

application.properties中

# 更新或者创建数据表结构
spring.jpa.hibernate.ddl-auto=update
#控制台显示SQL
spring.jpa.show-sql=true

4)启动

报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

解决方法:需要将application.properties中    com.mysql.jdbc.Driver  改为  com.mysql.cj.jdbc.Driver

然后在最后发现

spring.datasource.url=jdbc:mysql://192.168.3.18/jpa      忘记改成自己的了

springdata简介+springboot整合JPA不用编写增删改查数据库_spring_08

发现数据库中出现tbl_user表

springdata简介+springboot整合JPA不用编写增删改查数据库_mysql_09

5)新建controller包UserController类

package com.example.springbootdatajpa.controller;

import com.example.springbootdatajpa.entity.User;
import com.example.springbootdatajpa.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;

@RestController
public class UserController {

@Autowired
UserRepository userRepository;

//拿到路径变量
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id){
User user = userRepository.getOne(id);
return user;
}

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

6)重启并访问

​​http://localhost:8080/user/1​​

控制台出现语句:Hibernate: select user0_.id as id1_0_0_, user0_.email as email2_0_0_, user0_.last_name as last_nam3_0_0_ from tbl_user user0_ where user0_.id=?

springdata简介+springboot整合JPA不用编写增删改查数据库_mysql_10

举报

相关推荐

0 条评论