springBoot 搭建web項目(前后端分離,附項目源代碼地址)
概述
該項目包含springBoot-example-ui 和 springBoot-example,分別為前端與后端,前后端分離,利用ajax交互。
springBoot-example-ui
- 前端html
- 技術:
BootStrap
+layer
+jquery
+css
+html
- 該項目git地址:https://github.com/jiangcaijun/springBoot-example-ui
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:http://localhost:7500/zeus/specialManagements?idSelections=B3C36EAEC69243B7A9723EAB90150512
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