导入相关pom依赖
org.springframework.boot
spring-boot-starter-data-jpa
application.yml文件配置
spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
自动建表相关代码
package com.xxx.springboot04.entity;
import javax.persistence.*;
/**
* @author Zhang
*/
@Entity
@Table(name = "t_springboot_order")
public class Order {
@Id
@GeneratedValue
private Integer oid;
@Column(length = 100)
private String orderName;
@Column
private Float total;
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public Float getTotal() {
return total;
}
public void setTotal(Float total) {
this.total = total;
}
}
数据库自动建表截图
会创建一个序列以及t_springboot_order表
jpa的增删改查
dao层
* 只要继承JpaRepository,通常所用的增删查改方法都有
* 第一个参数:操作的实体类
* 第二个参数:实体类对应数据表的主键
*/
public interface JpaDao extends JpaRepository {
}
service层
service:
package com.xxx.springboot04.service;
import com.xxx.springboot04.entity.Order;
import java.util.List;
/**
* @author Zhang
*/
public interface OrderService {
public Order save(Order order);
public void deleteById(Integer id);
public List list(Order order);
}
serviceimpl
package com.xxx.springboot04.service.impl;
import com.xxx.springboot04.dao.OrderDao;
import com.xxx.springboot04.entity.Order;
import com.xxx.springboot04.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
/**
* @author Zhang
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Override
public Order save(Order order) {
return orderDao.save(order);
}
@Override
public void deleteById(Integer id) {
orderDao.deleteById(id);
}
controller层
package com.xxx.springboot04.controller;
import com.xxx.springboot04.entity.Order;
import com.xxx.springboot04.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Zhang
*/
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/order/list")
public List list(Order order){
return orderService.list(order);
}
@RequestMapping("/order/add")
public Order add(Order order){
return orderService.save(order);
}
@RequestMapping("/order/edit")
public Order edit(Order order){
return orderService.save(order);
}
@RequestMapping("/order/del")
public String del(Order order){
orderService.deleteById(order.getOid());
return "success";
}
在浏览器中输入请求进行测试
jpa复杂查询
dao层
* 要使用高级查询必须继承
* org.springframework.data.jpa.repository.JpaSpecificationExecutor接口
*/
public interface JpaDao extends JpaRepository, JpaSpecificationExecutor {
}
service层
service
public List list(Order order)
serviceimpl
@Override
public List list(Order order) {
//这个方法就是用来重写他的一个查询,带什么条件都在里面写
return orderDao.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate predicate=criteriaBuilder.conjunction();
if(order!=null&&order.getOrderName()!=null&&!"".equals(order.getOrderName())){
predicate.getExpressions().add(criteriaBuilder.like(root.get("orderName"),"%"+order.getOrderName()+"%"));
}
return predicate;
}
});
}
jpa查单个
service层
service
public Order getOne(Integer id);
serviceimpl
@Override
public Order getOne(Integer id) {
return orderDao.findById(id).get();
}
controller层
@RequestMapping("/order/getOne")
public Order getOne(Order order){
return orderService.getOne(order.getOid());
}
要注意的点:
springboot的jpa可以自动生成表(hibernate本质也是可以的)
jpa如果只是普通的增删查改只要继承jpaRepository就可以了
jpa要用到复杂的查询需要用到jpaSpecificationExecutor
在springboot2之前springboot它的getOne是可以用的
在springboot2之后springboot它的getOne变成懒加载了