上一章我们将RMS后台管理系统搭建完毕,本章我们就在这个系统上实现录入游戏配置的功能。目前我们需要配置四项,每个等级的人物属性,每个等级的升级经验,游戏地图,地图中的怪物。下面我们以游戏地图配置为例子,实现对它的增删查改功能。
一、数据访问层的实现
首先,我们需要定义地图类,这个类在各个模块通用,因此要定义在facade模块中。我们新建一个包com.idlewow.map.model,在其中新建WowMap类,代码如下:
packagecom.idlewow.map.model;importcom.idlewow.common.model.BaseModel;importlombok.Data;importjava.io.Serializable;
@Datapublic class WowMap extends BaseModel implementsSerializable {privateString name;privateInteger occupy;privateString description;
}
然后,我们在core模块中实现数据访问层的逻辑。新建com.idlewow.map.mapper包,并新建一个接口类WowMapMapper,在接口中定义我们需要用到的一些增删查改方法,代码如下:
packagecom.idlewow.map.mapper;importcom.idlewow.map.model.WowMap;importcom.idlewow.query.model.WowMapQueryParam;importjava.util.List;public interfaceWowMapMapper {intinsert(WowMap wowMap);int batchInsert(Listlist);intupdate(WowMap levelProp);intdelete(String id);
WowMap find(String id);
Listlist(WowMapQueryParam queryParam);intcount(WowMapQueryParam queryParam);
}
Mapper接口类,只定义了方法,具体实现需要我们在该包下新建一个WowMapMapper.xml文件,在这个文件中,通过SQL语句实现接口中的方法:
insert into map (name, occupy, description, create_user)
values (#{name}, #{occupy}, #{description}, #{createUser})
insert into map (name, occupy, description, create_user)
values(#{item.name}, #{item.occupy}, #{item.description}, #{item.createUser})
update map
set name = #{name},
occupy = #{occupy},
description = #{description},
update_user = #{updateUser},
version = version + 1
where id = #{id} and is_delete = 0
update map
set is_delete = 1
where id = #{id}
select *
from map
where id = #{id} and is_delete = 0
select count(1)
from mapis_delete = 0and name like concat('%', #{name}, '%')
select *
from mapis_delete = 0and name like concat('%', #{name}, '%')
limit ${(pageParam.pageIndex - 1) * pageParam.pageSize}, ${pageParam.pageSize}
WowMapMapper.xml
最后,我们新建com.idlewow.map.manager包,新建一个类WowMapManager,此类通过调用mapper来实现具体业务:
packagecom.idlewow.map.manager;importcom.idlewow.common.model.PageList;importcom.idlewow.map.mapper.WowMapMapper;importcom.idlewow.map.model.WowMap;importcom.idlewow.query.model.WowMapQueryParam;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importjava.util.List;
@Componentpublic classWowMapManager {
@Autowired
WowMapMapper wowMapMapper;public voidinsert(WowMap wowMap) {int effected =wowMapMapper.insert(wowMap);if (effected == 0) {throw new RuntimeException("sql effected 0 rows");
}
}public void batchInsert(Listlist) {int splitSize = 100;int index = 0;int total =list.size();while (index <=total) {int end = index +splitSize;if (end >total) {
end=total;
}
List sublist =list.subList(index, end);int effected =wowMapMapper.batchInsert(sublist);if (effected == 0) {throw new RuntimeException("sql effected 0 rows");
}
index+=splitSize;
}
}public voidupdate(WowMap t) {int effected =wowMapMapper.update(t);if (effected == 0) {throw new RuntimeException("sql effected 0 rows");
}
}public voiddelete(String id) {int effected =wowMapMapper.delete(id);if (effected == 0) {throw new RuntimeException("sql effected 0 rows");
}
}publicWowMap find(String id) {
WowMap wowMap=wowMapMapper.find(id);returnwowMap;
}public PageListlist(WowMapQueryParam queryParam) {
PageList pageList = new PageList();int count =wowMapMapper.count(queryParam);
List list =wowMapMapper.list(queryParam);
pageList.setTotalCount(count);
pageList.setData(list);
pageList.setPageParam(queryParam.getPageParam());returnpageList;
}
}
另外,@Componet注解需要依赖spring-context包。还有我们写SQL的xml文件是资源文件,但为了方便查看编辑,我们直接把它放在了代码目录下,所以编译的时候不会被打包,需要在pom中添加配置如下:
........
........
org.springframework
spring-context
5.1.6.RELEASE
src/main/java
**/*.xml
**/*.properties
false
这样,我们的数据访问层逻辑就全部实现了。通过在RMS模块的controller中注入manager,即可实现对底层数据的访问。
二、controller层的实现
在com.idlewow.rms.controller包中,新建类MapController,在Contoller中,我们初步实现了对数据的增删查改。注意,这里list()方法,返回的字符串“/manage/map/list",代表视图所在路径,根据spring-mvc.xml中配置的视图路由解析规则,即返回/WEB-INF/views/manage/map/list.jsp页面。但如果在该方法上加上注解@ResponseBody,则代表直接返回该字符串,不会去寻找视图。在post方法中,我们就是直接返回数据的json串。代码如下:
packagecom.idlewow.rms.controller;importcom.idlewow.common.model.CommonResult;importcom.idlewow.common.model.PageList;importcom.idlewow.map.manager.WowMapManager;importcom.idlewow.map.model.WowMap;importcom.idlewow.query.model.WowMapQueryParam;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/manage/map")public class MapController extendsBaseController {
@Autowired
WowMapManager wowMapManager;
@RequestMapping("/list")publicObject list() {return "/manage/map/list";
}
@ResponseBody
@RequestMapping(value= "/list", method =RequestMethod.POST)public Object list(@RequestParam(value = "page", defaultValue = "1") int pageIndex, @RequestParam(value = "limit", defaultValue = "10") intpageSize, WowMapQueryParam queryParam) {
queryParam.setPage(pageIndex, pageSize);
PageList pageList =wowMapManager.list(queryParam);return this.parseTable(pageList);
}
@RequestMapping("/add")publicObject add() {return "/manage/map/add";
}
@ResponseBody
@RequestMapping(value= "/add", method =RequestMethod.POST)publicObject add(@RequestBody WowMap wowMap) {try{
wowMap.setCreateUser(this.currentUserName());
wowMapManager.insert(wowMap);returnCommonResult.success();
}catch(Exception ex) {returnCommonResult.fail();
}
}
@RequestMapping(value= "/edit/{id}", method =RequestMethod.GET)publicObject edit(@PathVariable String id, Model model) {
WowMap wowMap=wowMapManager.find(id);
model.addAttribute(wowMap);return "/manage/map/edit";
}
@ResponseBody
@RequestMapping(value= "/edit/{id}", method =RequestMethod.POST)publicObject edit(@PathVariable String id, @RequestBody WowMap wowMap) {try{if (!id.equals(wowMap.getId())) {return CommonResult.fail("id不一致");
}
wowMap.setUpdateUser(this.currentUserName());
wowMapManager.update(wowMap);returnCommonResult.success();
}catch(Exception ex) {returnCommonResult.fail();
}
}
@ResponseBody
@RequestMapping(value= "/delete/{id}", method =RequestMethod.POST)publicObject delete(@PathVariable String id) {try{
wowMapManager.delete(id);returnCommonResult.success();
}catch(Exception ex) {returnCommonResult.fail();
}
}
}
MapController.java
其中, CommonResult类是我定义的一个通用的结果返回类,PageList和PageParam是分页相关的辅助类,WowMapQueryParam是列表查询的参数类。
另外,前端列表展示使用了layui的datatable,需要给他返回对应的数据结构。因此,我们定义一个抽象类BaseController,实现对列表数据结构的转换,同时提供查询当前登录用户的方法。其他的Controller如无特殊情况均继承BaseController,代码如下:
packagecom.idlewow.rms.vo;importlombok.Data;importjava.io.Serializable;importjava.util.List;
@Datapublic class LayuiDataTable implementsSerializable {privateInteger code;privateString message;privateInteger count;private Listdata;
}
LayuiDataTable.java
packagecom.idlewow.rms.controller;importcom.idlewow.admin.model.SysAdmin;importcom.idlewow.common.model.PageList;importcom.idlewow.rms.vo.LayuiDataTable;importorg.springframework.beans.factory.annotation.Autowired;importjavax.servlet.http.HttpSession;public abstract classBaseController {private static final String LoginUserKey = "loginuser";
@AutowiredprotectedHttpSession httpSession;protectedSysAdmin currentUser() {return(SysAdmin) httpSession.getAttribute(LoginUserKey);
}protectedString currentUserName() {return this.currentUser().getUsername();
}protectedLayuiDataTable parseTable(PageList pageList) {
LayuiDataTable dataTable= newLayuiDataTable();
dataTable.setMessage("读取成功");
dataTable.setCode(0);
dataTable.setCount(pageList.getTotalCount());
dataTable.setData(pageList.getData());returndataTable;
}
}
BaseController.java
写完控制器,前端页面,我们直接套用x-admin的模板。在/WEB-INF/views/manage/map目录下,创建页面:
/css/font.css">
/css/xadmin.css">
/lib/layui/layui.js"charset="utf-8">
/js/xadmin.js">
首页
后台管理
地图