欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot JPA使用

程序员文章站 2022-04-25 08:25:25
...

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