1、springboot之jpa支持
导入相关pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.yml文件配置
server:
port: 80
servlet:
context-path: /
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
datasource:
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:
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
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
filter:
stat:
merge-sql: true
slow-sql-millis: 5000
web-stat-filter:
enabled: true
url-pattern: /*
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
thymeleaf:
cache: false
servlet:
multipart:
max-file-size: 20MB
max-request-size: 60MB
自动建表相关代码
如果数据库没有这张表,就会创建表
如果有,就不会创建
如果数据库列段和实体类列段不一致,会更新数据库的列段
package com.zyc.springboot03.entity;
import lombok.Data;
import javax.persistence.*;
/**
* @author 简单定义存在
* @site https://i.csdn.net/#/uc/profile
* @company xxx公司
* @create 2019-12-01 10:43
*
* 如果数据库没有这张表,就会创建表
* 如果有,就不会创建
* 如果数据库列段和实体类列段不一致,会更新数据库的列段
*/
@Data
@Table(name = "t_springbook_book")
@Entity
public class Book {
@Id //标记为表的主键
@GeneratedValue //自增长
private Integer id;
@Column(length = 100) //表的一个列段
private String name;
@Column
private Float price;
}
jpa值增删改查
只要继承JpaRepository,通常所用的增删查改方法都有
第一个参数:操作的实体类
第二个参数:实体类对应数据表的主键
BookDao
package com.zyc.springboot03.repository;
import com.zyc.springboot03.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author 简单定义存在
* @site https://i.csdn.net/#/uc/profile
* @company xxx公司
* @create 2019-12-01 10:54
*/
@Repository
public interface BookRepository extends JpaRepository<Book,Integer> {
}
controller层
BookController
package com.zyc.springboot03.controller;
import com.zyc.springboot03.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author 简单定义存在
* @site https://i.csdn.net/#/uc/profile
* @company xxx公司
* @create 2019-12-01 10:58
*/
@RestController
@RequestMapping("/book")
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(){