Spring-Data-Jpa
JPA(Java Persistence API)定义了一系列对象持久化的标准,
目前实现这一规范的产品有Hibernate、TopLink等。
Spring Data JPA 框架,主要针对的就是 Spring 唯一没有简化到的业务逻辑代码,至此,开发者连仅剩的实现持久层业务逻辑的工作都省了,唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!
下面我们来演示下SpringBoot下 Spring-Data-Jpa的使用
本贴就简单演示下 配置 以及自动生成表
首先第一步,引入jpa和mysql驱动支持
还是前面的方式 进入pom.xml,alt+/ 进入编辑视图
yml配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
选择jpa和mysql
<
dependency
>
<
groupId
>mysql</
groupId
>
<
artifactId
>mysql-connector-java</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-data-jpa</
artifactId
>
</
dependency
>
配置application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_book
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
properties形式的配置有点冗余,改成主流的yml形式------
yml格式有个注意点 冒号后面一定要加个空格
还有我们把context-path改成/方便开发应用
server:
port: 80
context-path: /
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_book
username: root
password:
jpa:
hibernate:
ddl-auto: update
show-sql: true
新建一个Book实体创建数据表
package
com.huawei.entity;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.GeneratedValue;
import
javax.persistence.Id;
import
javax.persistence.Table;
@Entity
@Table
(name=
"t_book"
)
public
class
Book {
@Id
@GeneratedValue
private
Integer id;
@Column
(length=
100
)
private
String bookName;
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id = id;
}
public
String getBookName() {
return
bookName;
}
public
void
setBookName(String bookName) {
this
.bookName = bookName;
}
}
新建BookDao接口实现JpaRepository
package
com.huawei.dao;
import
org.springframework.data.jpa.repository.JpaRepository;
import
com.huawei.entity.Book;
/**
* 图书Dao接口
* @author user
*
*/
public
interface
BookDao
extends
JpaRepository<Book, Integer>{
}
新建一个BookController类
package
com.huawei;
import
javax.annotation.Resource;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.PostMapping;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.servlet.ModelAndView;
import
com.huawei.dao.BookDao;
import
com.huawei.entity.Book;
/**
* Book控制类
* @author user
*
*/
@Controller
@RequestMapping
(
"/book"
)
public
class
BookController {
@Resource
private
BookDao bookDao;
/**
* 查询所有图书
* @return
*/
@RequestMapping
(value=
"/list"
)
public
ModelAndView list(){
ModelAndView mav=
new
ModelAndView();
mav.addObject(
"bookList"
, bookDao.findAll());
mav.setViewName(
"bookList"
);
return
mav;
}
/**
* 添加图书
* @param book
* @return
*/
@RequestMapping
(value=
"/add"
,method=RequestMethod.POST)
public
String add(Book book){
bookDao.save(book);
return
"forward:/book/list"
;
}
@GetMapping
(value=
"/preUpdate/{id}"
)
public
ModelAndView preUpdate(
@PathVariable
(
"id"
) Integer id){
ModelAndView mav=
new
ModelAndView();
mav.addObject(
"book"
, bookDao.getOne(id));
mav.setViewName(
"bookUpdate"
);
return
mav;
}
/**
* 修改图书
* @param book
* @return
*/
@PostMapping
(value=
"/update"
)
public
String update(Book book){
bookDao.save(book);
return
"forward:/book/list"
;
}
/**
* 删除图书
* @param id
* @return
*/
@RequestMapping
(value=
"/delete"
,method=RequestMethod.GET)
public
String delete(Integer id){
bookDao.delete(id);
return
"forward:/book/list"
;
}
}
实现了 CRUD
这里的@GetMapping(value="xxx") 类似 @RequestMapping(value="xxx",method=RequestMethod.GET)
以及@PostMapping(value="xxx") 类似 @RequestMapping(value="xxx",method=RequestMethod.POST)
bookList.ftl 展示数据
<!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >图书管理页面</ title > </ head > < body > < a href = "/bookAdd.html" >添加图书</ a > < table > < tr > < th >编号</ th > < th >图书名称</ th > < th >操作</ th > </ tr > <#list bookList as book> < tr > < td >${book.id}</ td > < td >${book.bookName}</ td > < td > < a href = "/book/preUpdate/${book.id}" >修改</ a > < a href = "/book/delete?id=${book.id}" >删除</ a > </ td > </ tr > </#list> </ table > </ body > </ html >
|
bookAdd.html 图书添加页面
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >图书添加页面</ title > </ head > < body > < form action = "book/add" method = "post" > 图书名称:< input type = "text" name = "bookName" />< br /> < input type = "submit" value = "提交" /> </ form > </ body > </ html > |
bookUpdate.ftl图书修改页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >图书更新页面</ title > </ head > < body > < form action = "/book/update" method = "post" > < input type = "hidden" name = "id" value = "${book.id}" /> 图书名称:< input type = "text" name = "bookName" value = "${book.bookName}" />< br /> < input type = "submit" value = "提交" /> </ form > </ body > </ html > |