Spring boot JPA使用

controller的代码
@RestController
@RequestMapping("/label")
@CrossOrigin //解决跨域问题
public class LabelController {

@Autowired
LabelService labelService;

@RequestMapping(method = RequestMethod.GET)
public Result findAll(){
    List<Label> labelList = labelService.findAll();
    return new Result(true, StatusCode.OK,"查询所有标签信息成功",labelList);
}
@RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
public Result findById(@PathVariable String labelId){
    Label label = labelService.findById(labelId);
    return new Result(true,StatusCode.OK,"根据id查询成功",label);
}
@RequestMapping(method = RequestMethod.POST)
public Result save(@RequestBody Label label){
    labelService.save(label);
    return new Result(true, StatusCode.OK,"标签保存成功");
}

@RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
public Result updateById(@PathVariable String labelId,@RequestBody Label label){
    labelService.updateById(labelId,label);
    return new Result(true, StatusCode.OK,"标签修改成功");
}

@RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
public Result deleteById(@PathVariable String labelId){
    labelService.deleteById(labelId);
    return new Result(true, StatusCode.OK,"标签修改成功");
}

@RequestMapping(value="/search" , method = RequestMethod.POST)
public Result  findSearch(@RequestBody Map<String,String> map ){
    List<Label> labelList = labelService.findSearch(map);
    return new Result(true, StatusCode.OK,"查询所有标签信息成功",labelList);
}
@RequestMapping(value="/search/{page}/{size}",method = RequestMethod.POST)
public Result findByPage(@PathVariable int page, @PathVariable int size,@RequestBody Map<String,String> map){
    List<Label> labelList = labelService.findByPage(page,size,map);
    return new Result(true, StatusCode.OK,"查询所有标签信息成功",labelList);
}

}

Service 的基本代码
@Service
public class LabelService {
@Autowired
private LabelDao labelDao;

@Autowired
private IdWorker idWorker;

public List<Label> findAll(){
    return labelDao.findAll();
}


public Label findById(String id) {
    Optional<Label> label = labelDao.findById(id);
    return label.get();
}


public void save(Label label) {
     //分布式系统id
    label.setId(idWorker.nextId()+"");
    labelDao.save(label);
}

public void updateById(String labelId, Label label) {
    label.setId(labelId);
    labelDao.save(label);
}

public void deleteById(String labelId) {
    labelDao.deleteById(labelId);
}

public List<Label> findSearch(Map map){
    Specification specification = new Specification<Label>() {
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
            //构建查询
            ArrayList<Predicate> predicateList = new ArrayList<>();
            if(map.get("labelname")!=null&&!"".equals(map.get("labelname"))){
                predicateList.add(cb.like(root.get("labelname").as(String.class),"%"+(String)map.get("labelname")+"%" ));
            }
            if(map.get("state")!=null && !"".equals(map.get("state"))){
                predicateList.add(cb.equal( root.get("state").as(String.class), (String)map.get("state") ) );
            }
            if(map.get("recommend")!=null && !"".equals(map.get("recommend"))) {
                predicateList.add(cb.equal(root.get("recommend").as(String.class), (String) map.get("recommend")));
            }
            return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
        }
    };
    return labelDao.findAll(specification);
}

public List<Label> findByPage(int page, int size, Map<String, String> map) {
    Pageable pageable = PageRequest.of(page - 1, size);
    Specification specification = new Specification<Label>() {
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
            //构建查询
            ArrayList<Predicate> predicateList = new ArrayList<>();
            if(map.get("labelname")!=null&&!"".equals(map.get("labelname"))){
                predicateList.add(cb.like(root.get("labelname").as(String.class),"%"+(String)map.get("labelname")+"%" ));
            }
            if(map.get("state")!=null && !"".equals(map.get("state"))){
                predicateList.add(cb.equal( root.get("state").as(String.class), (String)map.get("state") ) );
            }
            if(map.get("recommend")!=null && !"".equals(map.get("recommend"))) {
                predicateList.add(cb.equal(root.get("recommend").as(String.class), (String) map.get("recommend")));
            }
            return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
        }
    };
    Page pagelist = labelDao.findAll(specification, pageable);
    List<Label> list = (List<Label>)pagelist.getContent();
    return list;
}

}

dao的代码
public interface LabelDao extends JpaRepository<Label,String>, JpaSpecificationExecutor

}

关联查询 jpql 和原生sql 如果要做更新操作的话,需要添加@Tractional,@Modifying

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值