概述
该项目包含 springBoot-example-ui 和 springBoot-example, 分别为前端与后端, 前后端分离, 利用 Ajax 交互.
springBoot-example-ui
前端 html
技术: Bootstrap + layer + jQuery + CSS + HTML
该项目 Git 地址:
springBoot-example
后端服务器侧
该项目 Git 地址: https://github.com/jiangcaijun/springBoot-example
注意: 涉及跨域, 故 springBoot-example 在 controller 类上添加类了 @CrossOrigin, 以此支持跨域请求
springBoot-example-ui(前端)
首页 (路径为 springBoot-example-ui\index.HTML)(这里项目名称为 zeus-ui, 下同)
首页请求分析
以删除为例:
Request Method:DELETE (RESTFUL 风格)
后端地址为 Request URL:
springBoot-example(后端)
1, 技术架构
后端以 springboot,maven 多模块为基础框架, 数据库为 MySQL+Redis, 实现简单的 CRUD 功能. 前后端以 RESTFUL 风格的 Ajax 请求来进行交互.
2, 项目分层
springBoot-API 控制层, 主要是各类 controller
实现对 MySQL 常见的 CRUD 请求 (PUT,DELETE,PATCH,POST,GET 等), 以自定义的 Response 来返回至客户端 (主要体现在 RedisExampleController.java 类中)
实现 SpringBoot 下 Redis 的 set 与 get(主要体现在 RedisExampleController.java 类中)
springBoot-base 接口层, 包含 service 接口和 entiy 实体类
springBoot-util 工具类层
项目代码总体结构如下:
3, 项目启动
项目成功启动时, 控制台:
4,springboot + Redis 相关
代码如下:
- @RestController
- public class RedisExampleController {
- @Autowired
- private IRedisService redisService;
- @RequestMapping("/redis/set")
- public Object redisSet(@RequestParam("value")String value){
- boolean isOk = redisService.setString("name", value);
- if(isOk){
- return new XPFSingleResponse("redis 新增成功");
- }else{
- return new XPFBadRequestException("redis 新增失败");
- }
- }
- @RequestMapping("/redis/get")
- public Object redisGet(){
- String name = redisService.getString("name");
- return new XPFSingleResponse("redis 获取:" + name);
- }
- }
配置如下 (路径在 springBoot-API\src\main\resources\application-dev.properties):
- #Redis
- # Redis 数据库索引 (默认为 0)
- spring.Redis.database=0
- # Redis 服务器地址
- spring.Redis.host=127.0.0.1
- # Redis 服务器连接端口
- spring.Redis.port=6379
- # Redis 服务器连接密码 (默认为空)
- spring.Redis.password=
- # 连接池最大连接数 (使用负值表示没有限制)
- spring.Redis.pool.max-active=8
- # 连接池最大阻塞等待时间 (使用负值表示没有限制)
- spring.Redis.pool.max-wait=-1
- # 连接池中的最大空闲连接
- spring.Redis.pool.max-idle=8
- # 连接池中的最小空闲连接
- spring.Redis.pool.min-idle=0
- # 连接超时时间 (毫秒)
- spring.Redis.timeout=0
Redis 赋值测试 (项目名这里定义成 zeus , 下同): http://localhost:7500/zeus/Redis/set?value=vic
Redis 取值测试: http://localhost:7500/zeus/Redis/get
来源: http://www.bubuko.com/infodetail-2907301.html