对Position类进行基本的CURD功能
1、实体类
position数据库

2、PositionRepository(Dao层)
public interface PositionRepository extends JpaRepository<Position,Integer> {
//利用Id查找是否存在
boolean existsById(Integer id);
//批量删除
@Modifying //一般在删除或者修改的时候用 用来标明是一个删除或者修改的标识
@Transactional
@Query("delete from Position where id in (:id)")
Integer deletePosition(@Param("id") List<Integer> id);
}
- 在@Query注解中编写JPQL实现DELETE和UPDATE操作时候必须加上@Modifying注解,通知SpringData这是一个delete或者updata操作
- deletePosition这个方法可以有返回值也可以没有返回值
3、PositionService类
public interface PositionService {
//增加数据
public boolean addPosition(Position position);
//修改数据
public boolean updatePosition(Position position);
//删除数据
public boolean deletePosition(Integer id);
}
@Service
public class PositionServiceImpl implements PositionService {
@Autowired
PositionRepository positionRepository;
//增加数据
@Override
public boolean addPosition(Position position) {
position.setEnabled(true);
position.setCreatedate(new Date());
Position position1 = positionRepository.save(position);
return positionRepository.existsById(position1.getId());
}
//修改数据
@Override
public boolean updatePosition(Position position) {
String name = position.getName();
position = positionRepository.findById(position.getId()).get();
position.setName(name);
positionRepository.save(position);
return positionRepository.existsById(position.getId());
}
//删除数据
@Override
public boolean deletePosition(Integer id) {
positionRepository.deleteById(id);
return positionRepository.existsById(id);
}
}
4、PositionController类
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {
@Autowired
PositionRepository positionRepository;
@Autowired
PositionService positionService;
//查询所有
@GetMapping("/")
public List<Position> getAllPositions(){
return positionRepository.findAll();
}
//添加数据
@PostMapping("/")
public RespBean addPosition(@RequestBody Position position){
if (positionService.addPosition(position) == true) {
return RespBean.ok("添加成功!");
}
return RespBean.error("添加失败!");
}
//修改数据
@PutMapping("/")
public RespBean updatePosition(@RequestBody Position position){
if (positionService.updatePosition(position) == true){
return RespBean.ok("修改成功!");
}
return RespBean.error("修改失败!");
}
//删除数据
@DeleteMapping("/{id}")
public RespBean deletePositionById(@PathVariable Integer id){
if (positionService.deletePosition(id) == false){
return RespBean.ok("删除成功!");
}
return RespBean.error("删除失败!");
}
//批量删除
@DeleteMapping("/")
public RespBean deletePositionsByIds(@RequestBody List<Integer> ids){
if (positionRepository.deletePosition(ids) == ids.size()) {
return RespBean.ok("删除成功");
}
return RespBean.error("删除失败");
}
}
5、测试
添加


修改


删除


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return Objects.equals(name, position.name);
}
注意:
因为我在Position类里设置了name字段不可以重复,所以不能添加相同name。
测试批量删除


本文详细介绍了基于Position类实现增删改查(CRUD)功能的过程。包括实体类定义、Repository层实现、Service层逻辑及Controller层接口设计,并通过测试验证了功能的正确性。
1845

被折叠的 条评论
为什么被折叠?



