springboot+jpa+MySQL模拟后端数据接口api
2021/9/21 2:28:02
本文主要是介绍springboot+jpa+MySQL模拟后端数据接口api,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
使用springboot+jpa方法搭建数据访问接口
-
建立实体类,跟数据库表字段一致
-
写具体操作数据库类JpaRepository接口
-
建立Controller类,作数据展示
创建spring工程文件:
勾选需要导入的spring依赖:(7个)
Spring Boot DevTools
Lombok
Spring Web
JDBC API
Spring Data JPA
Spring Data JDBC
MySQL Driver
修改总配置文件后缀为.yml,并且完成基本数据库连接配置。
完成后正常运行
查询操作:
1、创建domain实体类文件(需要和数据库表名一致):
domain\t_user:
package com.lyd.springtestapi.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import lombok.experimental.Accessors; import javax.persistence.Entity; import javax.persistence.Id; @Data @NoArgsConstructor @AllArgsConstructor @ToString @Accessors(chain = true) @Entity //注意声明为实体类 public class t_user { @Id //注意声明主键 private Integer id; private Integer role; private String username; private String email; private String gender; private String createtime; private Integer flag; private String activatecode; }
2、写具体操作数据库类JpaRepository接口
dao\t_userRepository:
package com.lyd.springtestapi.dao; import com.lyd.springtestapi.domain.t_user; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * 增删改查操作示例 */ @Transactional//加上事物声明 @Transactional(readOnly = true) public interface t_userRepository extends JpaRepository<t_user, Integer> { @Query(value = "SELECT * FROM t_user WHERE id=? and role=?;", nativeQuery = true) List<t_user> getAllUser(Integer id, Integer role); // @Transactional//加上事物声明 @Modifying(clearAutomatically = true)//自动清除实体里保存的数据(更新缓存)。 @Query(value = "update t_user set username = ? where id = ?;", nativeQuery = true) int setUserName(String username, Integer id); @Modifying @Query(value = "delete from t_user where id = ?", nativeQuery = true) int deleteUser(Integer id); @Modifying @Query(value = "INSERT INTO t_user VALUES (?, ?, ?, '123456' ,?, ?, ?, ?, ?);", nativeQuery = true) int insertUser(Integer id, Integer role, String username, String email, String gender, String createtime, Integer flag, String activatecode); }
dao\ResultVO:
package com.lyd.springtestapi.domain; import lombok.Data; @Data public class ResultVO<T> { private Integer code; private String msg; private T data; }
3、创建Controller类,作数据展示
controller\t_userController:
package com.lyd.springtestapi.controller; import com.lyd.springtestapi.dao.t_userRepository; import com.lyd.springtestapi.domain.ResultVO; import com.lyd.springtestapi.utils.ResultUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController //声明控制类 public class t_userController { @Resource private t_userRepository t_userRepository; @GetMapping("/getUser") public ResultVO getUser() { return ResultUtils.success(t_userRepository.findAll()); } @PostMapping("/getUser1") //声明不同请求类型 public ResultVO getUser1() { return ResultUtils.success(t_userRepository.findAll()); } @GetMapping("/getAllUser") public ResultVO getAllUser(Integer id, Integer role) { return ResultUtils.success(t_userRepository.getAllUser(id, role)); } /*@RequestMapping("/getAllUser") public List<t_user> getAllUser(Integer id ,Integer role) { return t_userRepository.getAllUser(id,role); }*/ @PostMapping("/setUserName") public ResultVO setUserName(String username, Integer id) { return ResultUtils.success(t_userRepository.setUserName(username, id)); } }
utils\ResultUtils:
package com.lyd.springtestapi.utils; import com.lyd.springtestapi.domain.ResultVO; public class ResultUtils { public static ResultVO success(Object object) { ResultVO resultVO = new ResultVO(); resultVO.setCode(200); resultVO.setMsg("成功"); resultVO.setData(object); return resultVO; } }
标准输出格式:
{ "code": 200, "msg": "成功", "data": [ { "id": 14, "role": 1, "username": "lili", "email": "1365662837@qq.com", "gender": "男", "createtime": "2021-06-23 12:11:55", "flag": 2, "activatecode": "20210623121155770216" } ] }
这篇关于springboot+jpa+MySQL模拟后端数据接口api的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-30java最新版本是什么,有什么特性?-icode9专业技术文章分享
- 2024-11-30[开源]27.8K star!这款 Postman 替代工具太火了!
- 2024-11-30Gzip 压缩入门教程:轻松掌握文件压缩技巧
- 2024-11-29开源工具的魅力:让文档管理更“聪明”
- 2024-11-29Release-it开发入门教程
- 2024-11-29Rollup 插件入门教程:轻松掌握模块打包
- 2024-11-29从零到一,产品经理如何玩转项目管理和团队协作
- 2024-11-29如何通过精益生产管理工具帮助项目团队实现精准进度控制?
- 2024-11-29低代码应用开发课程:新手入门与基础教程
- 2024-11-29入门指南:全栈低代码开发课程