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

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

 

1\新建工程

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\新建数据库

3\整合spring data jpa

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

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

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

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      忘记改成自己的了

发现数据库中出现tbl_user表

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=?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值