编写业务层
UserSerivce接口
import com.example.springdatajpa.entity.UserEntity; import java.util.List; /** * @author : [WangWei] * @version : [v1.0] * @className : UserSerivce * @description : [User业务层接口] * @createTime : [2022/11/19 20:27] * @updateUser : [WangWei] * @updateTime : [2022/11/19 20:27] * @updateRemark : [描述说明本次修改内容] */ public interface UserSerivce { //新增用户 UserEntity insertUser(UserEntity user); //删除用户 void deleteUser(Long id); //修改用户信息 UserEntity updateUser(UserEntity user); //查询所有用户 List<UserEntity> queryUser(); //根据手机号查询用户信息 UserEntity queryUserByPhoneNumber(UserEntity user); }
UserServiceImpl实现类
import com.example.springdatajpa.entity.UserEntity; import com.example.springdatajpa.repository.UserRepository; import com.example.springdatajpa.service.UserSerivce; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @author : [WangWei] * @version : [v1.0] * @className : UserServiceImpl * @description : [User业务实现类] * @createTime : [2022/11/19 20:33] * @updateUser : [WangWei] * @updateTime : [2022/11/19 20:33] * @updateRemark : [描述说明本次修改内容] */ @Service public class UserServiceImpl implements UserSerivce { //User数据访问接口 @Resource UserRepository userRepository; /* * @version V1.0 * Title: insertUser * @author Wangwei * @description 插入用户信息 * @createTime 2022/11/20 8:47 * @param [user] * @return com.example.springdatajpa.entity.UserEntity **/ @Override public UserEntity insertUser(UserEntity user) { return userRepository.save(user); } /* * @version V1.0 * Title: deleteUser * @author Wangwei * @description 根据id删除用户信息 * @createTime 2022/11/20 8:48 * @param [id] * @return void */ @Override public void deleteUser(Long id) { userRepository.deleteById(id); } /* * @version V1.0 * Title: updateUser * @author Wangwei * @description 修改用户信息,save方法是如果存在这条数据则进行修改否则进行新增 * @createTime 2022/11/20 8:49 * @param [user] * @return com.example.springdatajpa.entity.UserEntity **/ @Override public UserEntity updateUser(UserEntity user) { return userRepository.save(user); } /* * @version V1.0 * Title: queryUser * @author Wangwei * @description 查询所有用户 * @createTime 2022/11/20 8:50 * @param [] * @return java.util.List<com.example.springdatajpa.entity.UserEntity> */ @Override public List<UserEntity> queryUser() { return userRepository.findAll(); } /* * @version V1.0 * Title: queryUserByPhoneNumber * @author Wangwei * @description 通过手机号查询用户信息 * @createTime 2022/11/20 8:50 * @param [user] * @return com.example.springdatajpa.entity.UserEntity * */ @Override public UserEntity queryUserByPhoneNumber(UserEntity user) { return userRepository.findUserEntitiesByPhoneNumberAndIsDelete(user.getPhoneNumber(),0); }
创建UserController类
import com.example.springdatajpa.entity.UserEntity; import com.example.springdatajpa.service.UserSerivce; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; /** * @author : [WangWei] * @version : [v1.0] * @className : UserController * @description : [User控制层类] * @createTime : [2022/11/19 20:40] * @updateUser : [WangWei] * @updateTime : [2022/11/19 20:40] * @updateRemark : [描述说明本次修改内容] */ @RestController @RequestMapping("/user") public class UserController { //注入业务接口 @Resource UserSerivce userSerivce; /* * @version V1.0 * Title: insertUser * @author Wangwei * @description 新增用户 * @createTime 2022/11/20 8:54 * @param [user] * @return UserEntity */ @PostMapping("/insert") public UserEntity insertUser(@RequestBody UserEntity user){ return userSerivce.insertUser(user); } /* * @version V1.0 * Title: deleteUser * @author Wangwei * @description 通过id删除用户 * @createTime 2022/11/20 8:56 * @param [id] * @return void */ @DeleteMapping("delete/{id}") public void deleteUser(@PathVariable("id") Long id){ userSerivce.deleteUser(id); } /* * @version V1.0 * Title: updateUser * @author Wangwei * @description:修改用户信息 * @createTime 2022/11/20 8:57 * @param [user] * @return com.example.springdatajpa.entity.UserEntity * */ @PutMapping("/update") public UserEntity updateUser(@RequestBody UserEntity user){ return userSerivce.updateUser(user); } /* * @version V1.0 * Title: queryUser * @author Wangwei * @description 查询所有用户信息 * @createTime 2022/11/20 9:25 * @param [] * @return java.util.List<com.example.springdatajpa.entity.UserEntity> */ @GetMapping("/query") public List<UserEntity> queryUser(){ return userSerivce.queryUser(); } @GetMapping("/query/{phoneNumber}") public UserEntity queryUserByPhoneNumber(@PathVariable("phoneNumber") String phoneNumber){ return userSerivce.queryUserByPhoneNumber(phoneNumber); } }
运行测试
选择使用postman进行调用接口测试
插入用户数据
{ "createdBy": "wangwei", "createdId": 3, "createTime": "2022-11-20T02:20:08.000+00:00", "updateBy": "wangwei", "updateId": 3, "updateTime": "2022-11-20T02:20:08.000+00:00", "isDelete": 0, "remark": null, "id": 1, "userName": "wangwei", "password": "003", "phoneNumber": "110", "sex": "男", "birthday": "2022-11-20T02:19:40.000+00:00" }
删除用户数据
修改数据
查询数据
根据手机号查询
总结
目前只是对Spring Data JPA的基础运用,对于JPA与mybatis这一类的ORM框架之间本质的区别还需要更加深入的学习和理解。