Spring Boot学习之整合JPA实现CRUD

1、什么是JPA?

JPA全称Java Persistence API,是一组用于将数据存入数据库的类和方法的集合。JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

2、JPA与Hibernate之间的关系

JPA是一种规范,旨在规范、简化Java对象的持久化工作。 JPA是开源API,各企业经营商Oracle, Redhat, Eclipse等,提供各种有特色的JPA产品,其中包括: Hiberate, Eclipselink, Toplink, Spring Data JPA等等。Hibernate就是JPA Provider中很强的一个。JPA和Hibernate之间的关系,可以简单的理解为JPA是标准接口,Hibernate是实现。

3、结合JPA实现增删改查

下面以图书管理实现增删改查,新建book对象

@Entity
@Table(name="t_book")
public class Book {

	@Id
	@GeneratedValue
	private Integer bookid;
	
	@Column(length=50)
	private String bookname;
	
	@Column(length=30)
	private String author;
	
	
	public Integer getBookid() {
		return bookid;
	}
	public void setBookid(Integer bookid) {
		this.bookid = bookid;
	}
	public String getBookname() {
		return bookname;
	}
	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	
	
}
public interface BookDao extends JpaRepository,JpaSpecificationExecutor{


}
BookController

@Controller
@RequestMapping("/book")
public class BookController {


	@Resource
	private BookDao bookDao;
	
	@RequestMapping("/booklist")
	public ModelAndView bookList() {
		ModelAndView model = new ModelAndView();
		model.addObject("booklist",bookDao.findAll());
		model.setViewName("booklist");
		return model; 
	}
	
	/**
	 * 带条件的动态查询
	 * @return
	 */
	@RequestMapping("/listDynamicQuery")
	public ModelAndView listDynamicQuery(Book book) {
		ModelAndView model = new ModelAndView();
		bookDao.findAll(new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query,CriteriaBuilder cb) {
				Predicate predicate = cb.conjunction();
				if (book!=null) {
					if (book.getBookname()!=null&&!"".equals(book.getBookname())) {
						predicate.getExpressions().add(cb.like(root.get("bookname"), "%"+book.getBookname()+"%"));
					}
					if (book.getAuthor()!=null&&!"".equals(book.getAuthor())) {
						predicate.getExpressions().add(cb.like(root.get("author"), "%"+book.getAuthor()+"%"));
					}
				}
				
				return null;
			}
		});
		model.addObject("booklist",bookDao.findAll());
		model.setViewName("booklist");
		return model;
	}
	
	@Transactional
	@RequestMapping(value="/addbook",method=RequestMethod.POST)
	public String addBook(Book book) {
		bookDao.save(book);
		return "forward:/book/booklist";
	}
	
	@RequestMapping("/preUpdate/{bookid}")
	public ModelAndView preUpdate(@PathVariable("bookid") Integer bookid) {
		ModelAndView model = new ModelAndView();
		model.addObject("book", bookDao.getOne(bookid));
		model.setViewName("bookupdate");
		return model;
	}
	
	@PostMapping(value="/updatebook")
	public String updateBook(Book book) {
		bookDao.save(book);
		return "forward:/book/booklist";
	}
	
	@GetMapping("/delbook")
	public String delBook(Integer bookid) {
		bookDao.delete(bookid);
		return "forward:/book/booklist";
	}
	
	
}
application.yml配置文件

server:
    port: 80
    context-path: /


spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root    
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true   
booklist.ftl





图书管理


新增
图书名称:
图书作者:
<#list booklist as book>
序号 图书名称 图书作者 操作
${book.bookid} ${book.bookname} ${book.author} 修改 删除
新增书籍页面 addbook.html





新增书籍



	
图书名称:
图书作者:
编辑书籍页面 bookupdate.ftl





新增书籍



	
图书名称:
图书作者:














你可能感兴趣的:(Spring,Boot)