Commit 569f2ed5 authored by 11277's avatar 11277

Update 头行保存.md

parent 71831487
...@@ -26,43 +26,63 @@ ...@@ -26,43 +26,63 @@
| 4 | 2 |画画| 梵高的画也不过如此| | 4 | 2 |画画| 梵高的画也不过如此|
需求是新增一个人的信息以及它的爱好(向hls_person和hls_hobby表新增记录) 需求是新增一个人的信息以及它的爱好(向hls_person和hls_hobby表新增记录)
下面是一个头行保存的列子: 下面是一个头行保存的列子:
#### 准备工作
1. 建表,主键默认自增长
2. 搭建数据模型,根据表建立相应的DTO,即HlsPerson.java和HlsHobby.java,注意HlsPerson.java中应存在List<HlsHobby> hlsHobbyList1属性。
3. 搭建控制层,建立controller
4. 搭建业务逻辑,建立service及其实现类。
5. 搭建数据持久化层,新建mapper.java和mapper.xml
>- 先建立结构,在具体实现功能,以免疏漏。
#### 前台
```javascript ```javascript
<script><![CDATA[ <script><![CDATA[
//提交数据用到的容器 //提交数据用到的容器
var viewModel = kendo.observable({ var viewModel = kendo.observable({
isEnabled: true, isEnabled: true,
data:{}, data: {},
mySubmit: function (e) {
mySubmit:function(e){ if (e) {
if(e){ e.preventDefault();
e.preventDefault(); }
} Hap.submitForm({
Hap.submitForm({ url: '${base.contextPath}/hls/test/hdLnSave',
url : '${base.contextPath}/hls/test/save', formModel: viewModel.data,
formModel : viewModel.data, asArray: false,
asArray:false, grid: {
grid: { "hobby": $("#grid") "hlsHobbyList": $("#grid")
}, },
success:function(){ success: function () {
showInfoDialog("保存成功!"); viewModel.showInfoDialog("保存成功!");
$('#grid').data('kendoGrid').dataSource.page(1);
},
error:function(){
viewModel.showInfoDialog("保存失败!");
} }
}); });
}, },
function:showInfoDialog(pMessage){ showInfoDialog: function(pMessage)
kendo.ui.showInfoDialog({ {
title: $l('提示'), kendo.ui.showInfoDialog({
message: pMessage title: $l('提示'),
}); message: pMessage
} });
}
});
});
//请求处理方法 //请求处理方法
function parameterMap(options, type){ function parameterMap(options, type){
...@@ -77,13 +97,9 @@ function parameterMap(options, type){ ...@@ -77,13 +97,9 @@ function parameterMap(options, type){
return kendo.stringify(datas); return kendo.stringify(datas);
} else if (type === "read") { } else if (type === "read") {
var map = {}; var map = {};
map.page = options.page; map.page = options.page || 1;
map.pagesize = options.pageSize; map.pagesize = options.pageSize|| 5;
for (var k in map) { return kendo.stringify(map);
if (map[k] === '' || map[k] === null || map[k] === undefined)
delete map[k]
}
return Hap.prepareQueryParameter(viewModel.model.toJSON(), options);
} }
} }
...@@ -99,8 +115,8 @@ function dsEditable(field){ ...@@ -99,8 +115,8 @@ function dsEditable(field){
<!--头--> <!--头-->
<h:hlsForm title="个人信息" width="100%"> <h:hlsForm title="个人信息" width="100%">
<h:hlsHBox> <h:hlsHBox>
<h:hlsMaskedTextBox name="name" id="name" bind="enabled: isEnabled, value:model.name" colspan="3" prompt="姓名:" required="true" style="width:100%;"/> <h:hlsMaskedTextBox name="name" id="name" bind="enabled: isEnabled, value:data.name" colspan="3" prompt="姓名:" required="true" style="width:100%;"/>
<h:hlsMaskedTextBox name="age" id="age" bind="enabled: isEnabled, value:model.age" colspan="3" prompt="年龄:" required="true" style="width:100%;"/> <h:hlsMaskedTextBox name="age" id="age" bind="enabled: isEnabled, value:data.age" colspan="3" prompt="年龄:" required="true" style="width:100%;"/>
</h:hlsHBox> </h:hlsHBox>
</h:hlsForm> </h:hlsForm>
...@@ -108,9 +124,8 @@ function dsEditable(field){ ...@@ -108,9 +124,8 @@ function dsEditable(field){
<!--行--> <!--行-->
<h:dataSource id="dataSource" batch="true" pageSize="10" serverPaging="true"> <h:dataSource id="dataSource" batch="true" pageSize="10" serverPaging="true">
<h:transport parameterMap="parameterMap"> <h:transport parameterMap="parameterMap">
<h:read url="${base.contextPath}/hls/test/query" type="GET" dataType="json"/> <h:read url="${base.contextPath}/hls/test/ln/query" type="GET" dataType="json"/>
<h:create url="${base.contextPath}/hls/test/create" type="POST" contentType="application/json" /> <h:destroy url="${base.contextPath}/hls/test/ln/remove" type="POST" contentType="application/json" />
<h:update url="${base.contextPath}/hls/test/save" type="POST" contentType="application/json" />
</h:transport> </h:transport>
<h:schema data="rows" total="total" errors="schemaError"> <h:schema data="rows" total="total" errors="schemaError">
<h:model id="pid" editable="dsEditable"> <h:model id="pid" editable="dsEditable">
...@@ -138,32 +153,119 @@ function dsEditable(field){ ...@@ -138,32 +153,119 @@ function dsEditable(field){
<!--按钮--> <!--按钮-->
<h:hlsToolBar> <h:hlsToolBar>
<h:hlsButton click="mySubmit" text="保存"></h:hlsButton> <h:hlsButton click="viewModel.mySubmit" text="保存"></h:hlsButton>
</h:hlsToolBar> </h:hlsToolBar>
``` ```
#### 后台
保存按钮注册了点了事件,点击保存将数据提交到'${base.contextPath}/hls/test/hdLnSave'
保存按钮注册了点了事件,点击保存将数据提交到'${base.contextPath}/hls/test/save' > 在后端controller通过定义 @RequestMapping("/hls/test/hdLnSave") 注解映射到对应的url,捕获到对应请求。
> 在后端controller通过定义 @RequestMapping("/hls/test/save") 注解映射到对应的url,捕获到对应请求。
后端收到前台传过来的数据,调用service层对数据进行持久化。 后端收到前台传过来的数据,调用service层对数据进行持久化。
controller HlsTESTController.java
```java ```java
@RequestMapping("/hls/test/save") @Controller
public class HlsTESTController extends BaseController {
@Autowired
private HlsPersonService hlsPersonService;
@Autowired
private HlsHobbyService hlsHobbyService;
@RequestMapping(value = "/hls/test/query")
@ResponseBody @ResponseBody
public ResponseData save(HttpServletRequest request,@RequestBody HlsPerson hlsPerson, @RequestParam(defaultValue = DEFAULT_PAGE) final int page, public ResponseData hdQuery(final HlsPerson hlsPerson, @RequestParam(defaultValue = DEFAULT_PAGE) final int page,
@RequestParam(defaultValue = DEFAULT_PAGE_SIZE) final int pagesize){ @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) final int pagesize, final HttpServletRequest request) {
IRequest iRequest = createRequestContext(request); IRequest requestContext = createRequestContext(request);
testService.batchChildUpdate(iRequest, hlsPerson ,hlsPerson.getHlsHobby()); return new ResponseData(hlsPersonService.select(requestContext,hlsPerson,page,pagesize));
return new ResponseData(); }
}
@RequestMapping(value="/hls/test/hdLnSave",method=RequestMethod.POST)
@ResponseBody
public ResponseData hdLnSave(HttpServletRequest request,@RequestBody HlsPerson hlsPerson, @RequestParam(defaultValue = DEFAULT_PAGE) final int page,
@RequestParam(defaultValue = DEFAULT_PAGE_SIZE) final int pagesize){
IRequest iRequest = createRequestContext(request);
hlsPersonService.batchChildUpdate(iRequest, hlsPerson);
return new ResponseData();
}
@RequestMapping(value = "/hls/test/ln/query")
@ResponseBody
public ResponseData lnQuery(final HlsHobby hlsHobby, @RequestParam(defaultValue = DEFAULT_PAGE) final int page,
@RequestParam(defaultValue = DEFAULT_PAGE_SIZE) final int pagesize, final HttpServletRequest request) {
IRequest requestContext = createRequestContext(request);
return new ResponseData(hlsHobbyService.select(requestContext,hlsHobby,page,pagesize));
}
@RequestMapping(value="/hls/test/ln/remove",method=RequestMethod.POST)
@ResponseBody
public ResponseData delete(HttpServletRequest request, @RequestBody List<HlsHobby> hlsHobbyList) {
hlsHobbyService.batchDelete(hlsHobbyList);
return new ResponseData();
}
}
```
> **提示:**
>* 通常头行结构还会涉及对头行数据的删和查,根据需要加入在controller中加入入口。
>* bathUpdate会根据传入的List对象中单个对象的属性值__status判断,如为update即做更新操作,add为插入操作, __status的值由前端paramter函数决定。
>* 这里的select()和batchUpdate()由相应的service继承自IBaseService,自己无需实现,类似方法还有许多,请自行练习使用。
HlsPersonService.java
```java
public interface HlsPersonService extends IBaseService<HlsPerson>,ProxySelf<HlsPersonService>{
void batchChildUpdate(IRequest iRequest, HlsPerson hlsPerson);
}
``` ```
testService的实现类testServiceImpl定义了batchChildUpdate方法:
接口实现类HlsPersonServiceImpl定义了batchChildUpdate方法:
HlsPersonServiceImpl.java
```java
@Service
@Transactional
public class HlsPersonServiceImpl extends BaseServiceImpl<HlsPerson> implements HlsPersonService {
@Autowired
private HlsHobbyService hlsHobbyService;
@Override
public void batchChildUpdate(IRequest iRequest, HlsPerson hlsPerson) {
if(hlsPerson != null ){
hlsPerson.set__status("add");
List<HlsPerson> tempHlsPerson = new ArrayList<HlsPerson>();
tempHlsPerson.add(hlsPerson);
self().batchUpdate(iRequest,tempHlsPerson);
Long pid = hlsPerson.getPid();
List<HlsHobby> hlsHobbyList = hlsPerson.getHlsHobbyList();
for(HlsHobby hh : hlsHobbyList){
hh.setPid(pid);
}
hlsHobbyService.batchUpdate(iRequest,hlsHobbyList);
}
}
}
```
接口实现类testServiceImpl定义了batchChildUpdate方法:
```java ```java
@Override @Override
...@@ -184,4 +286,4 @@ testService的实现类testServiceImpl定义了batchChildUpdate方法: ...@@ -184,4 +286,4 @@ testService的实现类testServiceImpl定义了batchChildUpdate方法:
``` ```
> bathUpdate会根据传入的List对象中单个对象的属性值__status判断,如为update即做更新操作,add为插入操作, __status的值由前端paramter函数决定。
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment