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
推荐阅读
-
解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."
-
什么是Spring Boot
-
Spring集成jedis的配置与使用简单实例
-
Spring-Boot 集成Solr客户端的详细步骤
-
Spring Boot实现图片上传功能
-
详解Spring Boot 使用Spring security 集成CAS
-
用Spring Boot进行后端开发(二):与微信小程序的交互,在微信小程序端获取数据并显示
-
Spring Boot 发送邮件功能案例分析
-
Spring.Net控制反转IoC入门使用
-
详解配置spring-boot-actuator时候遇到的一些小问题