1.play项目的结构
app中主要的包有controllers,filters,models,service,utils.
- 其中controllers是控制器,相当于springMVC中的Controller
- filter是拦截器,可以理解为springMVC中的HandlerInterceptor
- models相当去SSM框架的dao层,持久层
- services相当于SSM框架的service层,业务层
conf的文件主要有mysql.conf,application.conf,routes;
- mysql.conf主要配置一些与MySQL数据库有关的设置
- application.conf配置一些redis,cache,h2db之类的配置
- routes这个配置文件相当去springmvc的前端处理器,比如
POST /v1/tasks/:taskId/reward controllers.TaskApplicantsController.reward(taskId : Int)
分别对应http方法 url 类中的方法
play2框架的构建工具用的是sbt,build.sbt是其构建工具
2.play框架的增删改查
- 增
@Inject private MyCacheApi cache; public Result create() { JsonNode body = request().body().asJson(); String title = ApplicationHelper.getParamsInPath(body, Constants.TITLE, true); Users user = Users.getUser(cache.getUserId(request())); try { checkPermission(user); } catch (Exception e) { return badRequest(ApplicationHelper.getErrorJson(CodeConstants.INVALID_PARAM_ERROR, e.getMessage())); } Council council = new Council(); council.setTitle(title); council.insert(); return ok(Json.toJson(council)); }
- 删
-
public Result delete(int sectionId) { Sections sections = Sections.getSection(sectionId); if (sections != null) { sections.delete(); } return ok(); }
- 改
-
public Result update(Integer councilId) { JsonNode body = request().body().asJson(); String title = null; try { title = ApplicationHelper.getParamsInPath(body, Constants.TITLE, true); } catch (Exception e) { return badRequest(ApplicationHelper.getErrorJson(CodeConstants.INVALID_PARAM_ERROR, e.getMessage())); } Council council = Council.get(councilId); council.setTitle(title); council.update(); return ok(Json.toJson(council)); }
- 查
-
public Result get(Integer level) { Levels levels = Levels.get(level); try { return ok(Json.toJson(levels)); } catch (Exception e) { return badRequest(ApplicationHelper.getErrorJson(CodeConstants.INVALID_PARAM_ERROR, e.getMessage())); } }
时间: 2024-11-05 17:24:35