springboot对JPA的支持
导入相关pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
application.yml文件配置
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
自动建表相关代码
package com.xzy.springboot01.entity;
import javax.persistence.*;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-13 16:59
*/
@Entity
@Table(name = "t_springboot_book_2019")
public class HBook {
@Id
@GeneratedValue
private Integer bid;
@Column(length = 100)
private String bname;
@Column
private Float price;
}
jpa值增删改查
package com.xzy.springboot01.mapper;
import com.xzy.springboot01.entity.HBook;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-13 16:59
*
* 只要继承JpaRepository,通常所用的增删查改方法都有
* 第一个参数:操作的实体类
* 第二个参数:实体类对应数据表的主键
*/
public interface JpaDao extends JpaRepository<HBook, Integer> {
}
controller层
package com.xzy.springboot01.controller;
import com.xzy.springboot01.mapper.JpaDao;
import com.xzy.springboot01.entity.HBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @author XZY
* @site www.Xzy.com
* @company
* @create 2019-11-13 17:02
*/
public class JpaController {
@Autowired
private JpaDao jpaDao;
@RequestMapping("/add")
public String add(HBook book){
jpaDao.save(book);
return "success";
}
@RequestMapping("/edit")
public String edit(HBook book){
jpaDao.save(book);
return "success";
}
@RequestMapping("/del")
public String del(HBook book){
jpaDao.delete(book);
return "success";
}
@RequestMapping("/getOne")
public HBook getOne(Integer bid){
// 会出现懒加载问题:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
// return jpaDao.getOne(bid);
return jpaDao.findById(bid).get();
}
@RequestMapping("/getAll")
public List<HBook> getAll(){
return jpaDao.findAll();
}
}
Springboot+bootstrap界面版之增删改查及图片上传
工程创建
pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xzy</groupId>
<artifactId>springboot03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot03</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<mysql.version>5.1.44</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml文件配置
server:
servlet:
context-path: /
port: 80
spring:
jp