springboot之JPA的支持

本文介绍了如何在SpringBoot中使用JPA进行数据库操作,包括导入依赖、配置、自动建表,以及实现增删改查功能。同时,详细讲解了结合bootstrap实现界面版的数据操作,并探讨了图片上传的配置与实现。
摘要由CSDN通过智能技术生成

、知识点

1、springboot之jpa支持

2、Springboot+bootstrap界面版之增删改查及图片上传

 

二、实践、操作

springboot之jpa支持

2.1 导入相关pom依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

 

2.2 application.yml文件配置


server:
  port: 80
  servlet:
    context-path: /


spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

  datasource:
    #1.JDBC
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    username: root
    password: 123
    druid:
      #2.连接池配置
      #初始化连接池的连接数量 大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      #配置获取连接等待超时的时间
      max-wait: 60000
      #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 30000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      # 是否缓存preparedStatement,也就是PSCache  官方建议MySQL下建议关闭   个人建议如果想用SQL防火墙 建议打开
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
      #3.基础监控配置
      web-stat-filter:
        enabled: true
        url-pattern: /druid/*
        #设置不统计哪些URL
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        #设置监控页面的登录名和密码
        login-username: admin
        login-password: admin
        allow: 127.0.0.1
        #deny: 192.168.1.100
  thymeleaf:
    cache: false

  # 解决图片上传大小限制问题,也可采取配置类
  servlet:
    multipart:
      max-file-size: 20MB
      max-request-size: 60MB

 

2.3 自动建表相关代码

package com.jy.springboot03.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @author 蒋勇
 * @site www.jy.com
 * @company xxx集团
 * @create  2019-12-01 20:23
 */
@Data
@Table(name="t_springboot_book_2019")
@Entity
public class Book {
    @Id
    @GeneratedValue
    private Integer bid;
    @Column(length = 100)
    private String bname;
    @Column
    private Float price;
}

 

2.4 controller层

package com.jy.springboot03.controller;

import com.jy.springboot03.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 蒋勇
 * @site www.jy.com
 * @company xxx集团
 * @create  2019-12-01 20:23
 */
@RestController
public class BookController {
    @Autowired
    private JpaRepository jpaDao;

    @RequestMapping("/add")
    public String add(Book book) {
        jpaDao.save(book);
        return "success";
    }

    @RequestMapping("/edit")
    public String edit(Book book) {
        jpaDao.save(book);
        return "success";
    }

    @RequestMapping("/del")
    public String del(Book book) {
        jpaDao.delete(book);
        return "success";
    }

    @RequestMapping("/getOne")
    public Book getOne(Integer bid) {
//        会出现懒加载问题:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
//        return jpaDao.getOne(bid);
        return (Book)jpaDao.findById(bid).get();
    }

    @RequestMapping("/getAll")
    public List<Book> getAll() {
        return jpaDao.findAll();
    }
}
2.5 BookRepository
package com.jy.springboot03.repository;

import com.jy.springboot03.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
 * @author 蒋勇
 * @site www.jy.com
 * @company xxx集团
 * @create  2019-12-01 20:23
 */
@Repository
public interface BookRepository extends JpaRepository<Book,Integer> {

}

 

 

 

三、Springboot+b

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值