Commit 619d9fcb authored by 11277's avatar 11277

Update 头行保存.md

parent 869ca623
## 头行保存 ## 头行保存
头行保存在实用中经常用到,头行保存指的是头表和行表(一对多)的数据同时插入,如中途失败则这次事务不进行提交。 头行保存在实用中经常用到,头行保存指的是头表和行表(一对多)的数据同时插入,如中途失败则这次事务不进行提交。
假设我们有两张表 假设我们有两张表
<style>
**hls_person(头表)** table th:first-of-type {
pid | name | age | width: 100px;
-------- | ----- | -----| }
1|张三|21 </style>
2|李四|36
**hls_person(头表)**
*hls_hobby(行表)**
hid | pid |hobby| cause| |pid|name|age |
-------- | -----|----- |-----| |:---: | :---: | :---:|
1|1|篮球| 因为篮球好玩 |1| 张三 | 21 |
2|1|游泳| 因为游泳好玩 | 2 | 李四| 36 |
3|1|动画片|最爱看大头儿子
4|2|画画|梵高的画也不过如此
*hls_hobby(行表**
|hid| pid |hobby| cause|
需求是新增一个人的信息以及它的爱好(向hls_person和hls_hobby表新增记录) |:--------: | :-----:|:-----: |:-----:|
下面是一个头行保存的列子: |1|1|篮球| 因为篮球好玩|
|2|1|游泳| 因为游泳好玩|
```javascript |3|1|动画片|最爱看大头儿子|
|4|2|画画|梵高的画也不过如此|
<script><![CDATA[
//提交数据用到的容器
var viewModel = kendo.observable({ 需求是新增一个人的信息以及它的爱好(向hls_person和hls_hobby表新增记录)
isEnabled: true, 下面是一个头行保存的列子:
data:{},
```javascript
mySubmit:function(e){
if(e){ <script><![CDATA[
e.preventDefault(); //提交数据用到的容器
} var viewModel = kendo.observable({
Hap.submitForm({ isEnabled: true,
url : '${base.contextPath}/hls/test/save', data:{},
formModel : viewModel.data,
asArray:false, mySubmit:function(e){
grid: { "hobby": $("#grid") if(e){
e.preventDefault();
}, }
success:function(){ Hap.submitForm({
showInfoDialog("保存成功!"); url : '${base.contextPath}/hls/test/save',
} formModel : viewModel.data,
}); asArray:false,
grid: { "hobby": $("#grid")
},
function:showInfoDialog(pMessage){ },
kendo.ui.showInfoDialog({ success:function(){
title: $l('提示'), showInfoDialog("保存成功!");
message: pMessage }
}); });
}
},
function:showInfoDialog(pMessage){
}); kendo.ui.showInfoDialog({
title: $l('提示'),
//请求处理方法 message: pMessage
function parameterMap(options, type){ });
if (type !== "read" && options.models) { }
var datas = options.models
if (type == 'create' || type == 'update') {
datas = options.models.map(function(data) { });
data['__status'] = (type == 'create' ? 'add' : 'update');
return data; //请求处理方法
}) function parameterMap(options, type){
} if (type !== "read" && options.models) {
return kendo.stringify(datas); var datas = options.models
} else if (type === "read") { if (type == 'create' || type == 'update') {
var map = {}; datas = options.models.map(function(data) {
map.page = options.page; data['__status'] = (type == 'create' ? 'add' : 'update');
map.pagesize = options.pageSize; return data;
for (var k in map) { })
if (map[k] === '' || map[k] === null || map[k] === undefined) }
delete map[k] return kendo.stringify(datas);
} } else if (type === "read") {
return Hap.prepareQueryParameter(viewModel.model.toJSON(), options); var map = {};
} map.page = options.page;
} map.pagesize = options.pageSize;
for (var k in map) {
if (map[k] === '' || map[k] === null || map[k] === undefined)
//属性列是否可编辑 delete map[k]
function dsEditable(field){ }
return true; return Hap.prepareQueryParameter(viewModel.model.toJSON(), options);
} }
]]></script> }
```
```xml //属性列是否可编辑
<!--头--> function dsEditable(field){
<h:hlsForm title="个人信息" width="100%"> return true;
<h:hlsHBox> }
<h:hlsMaskedTextBox name="name" id="name" bind="enabled: isEnabled, value:model.name" colspan="3" prompt="姓名:" required="true" style="width:100%;"/> ]]></script>
<h:hlsMaskedTextBox name="age" id="age" bind="enabled: isEnabled, value:model.age" colspan="3" prompt="年龄:" required="true" style="width:100%;"/> ```
</h:hlsHBox>
</h:hlsForm> ```xml
<!--头-->
<h:hlsForm title="个人信息" width="100%">
<!--行--> <h:hlsHBox>
<h:dataSource id="dataSource" batch="true" pageSize="10" serverPaging="true"> <h:hlsMaskedTextBox name="name" id="name" bind="enabled: isEnabled, value:model.name" colspan="3" prompt="姓名:" required="true" style="width:100%;"/>
<h:transport parameterMap="parameterMap"> <h:hlsMaskedTextBox name="age" id="age" bind="enabled: isEnabled, value:model.age" colspan="3" prompt="年龄:" required="true" style="width:100%;"/>
<h:read url="${base.contextPath}/hls/test/query" type="GET" dataType="json"/> </h:hlsHBox>
<h:create url="${base.contextPath}/hls/test/create" type="POST" contentType="application/json" /> </h:hlsForm>
<h:update url="${base.contextPath}/hls/test/save" type="POST" contentType="application/json" />
</h:transport>
<h:schema data="rows" total="total" errors="schemaError"> <!--行-->
<h:model id="pid" editable="dsEditable"> <h:dataSource id="dataSource" batch="true" pageSize="10" serverPaging="true">
<h:fields> <h:transport parameterMap="parameterMap">
</h:fields> <h:read url="${base.contextPath}/hls/test/query" type="GET" dataType="json"/>
</h:model> <h:create url="${base.contextPath}/hls/test/create" type="POST" contentType="application/json" />
</h:schema> <h:update url="${base.contextPath}/hls/test/save" type="POST" contentType="application/json" />
</h:dataSource> </h:transport>
<h:hlsGridBox hlsGridId="grid" hlsBtnType="add"> <h:schema data="rows" total="total" errors="schemaError">
<h:hlsGrid id="grid" dataSource="dataSource" selectable="" hlsBtnType="ADD,DELETE" editable="true" height="240"> <h:model id="pid" editable="dsEditable">
<h:pageable pageSizes="5,10,20,50" buttonCount="2" refresh="true"> <h:fields>
</h:pageable> </h:fields>
<h:columns> </h:model>
<h:column field="hobby" title='爱好' width="150"> </h:schema>
<h:headerAttributes style="text-align:center"/> </h:dataSource>
<h:attributes style="text-align:center"/> <h:hlsGridBox hlsGridId="grid" hlsBtnType="add">
</h:column> <h:hlsGrid id="grid" dataSource="dataSource" selectable="" hlsBtnType="ADD,DELETE" editable="true" height="240">
<h:column field="cause" title='原因'> <h:pageable pageSizes="5,10,20,50" buttonCount="2" refresh="true">
<h:headerAttributes style="text-align:center"/> </h:pageable>
<h:attributes style="text-align:center"/> <h:columns>
</h:column> <h:column field="hobby" title='爱好' width="150">
</h:columns> <h:headerAttributes style="text-align:center"/>
</h:hlsGrid> <h:attributes style="text-align:center"/>
</h:hlsGridBox> </h:column>
<h:column field="cause" title='原因'>
<!--按钮--> <h:headerAttributes style="text-align:center"/>
<h:hlsToolBar> <h:attributes style="text-align:center"/>
<h:hlsButton click="mySubmit" text="保存"></h:hlsButton> </h:column>
</h:hlsToolBar> </h:columns>
``` </h:hlsGrid>
</h:hlsGridBox>
保存按钮注册了点了事件,点击保存将数据提交到'${base.contextPath}/hls/test/save' <!--按钮-->
<h:hlsToolBar>
> 在后端controller通过定义 @RequestMapping("/hls/test/save") 注解映射到对应的url,捕获到对应请求。 <h:hlsButton click="mySubmit" text="保存"></h:hlsButton>
</h:hlsToolBar>
```
后端收到前台传过来的数据,调用service层对数据进行持久化。
controller 保存按钮注册了点了事件,点击保存将数据提交到'${base.contextPath}/hls/test/save'
```java
@RequestMapping("/hls/test/save") > 在后端controller通过定义 @RequestMapping("/hls/test/save") 注解映射到对应的url,捕获到对应请求。
@ResponseBody
public ResponseData save(HttpServletRequest request,@RequestBody HlsPerson hlsPerson, @RequestParam(defaultValue = DEFAULT_PAGE) final int page,
@RequestParam(defaultValue = DEFAULT_PAGE_SIZE) final int pagesize){ 后端收到前台传过来的数据,调用service层对数据进行持久化。
IRequest iRequest = createRequestContext(request);
testService.batchChildUpdate(iRequest, hlsPerson ,hlsPerson.getHlsHobby()); controller
return new ResponseData(); ```java
} @RequestMapping("/hls/test/save")
@ResponseBody
``` public ResponseData save(HttpServletRequest request,@RequestBody HlsPerson hlsPerson, @RequestParam(defaultValue = DEFAULT_PAGE) final int page,
@RequestParam(defaultValue = DEFAULT_PAGE_SIZE) final int pagesize){
testService的实现类testServiceImpl定义了batchChildUpdate方法: IRequest iRequest = createRequestContext(request);
```java testService.batchChildUpdate(iRequest, hlsPerson ,hlsPerson.getHlsHobby());
return new ResponseData();
@Override }
public void batchChildUpdate(IRequest iRequest, HlsPerson hlsPerson, List<HlsHobby> hlsHobbyList) {
List<HlsPerson> tempHlsPerson = new ArrayList<HlsPerson>(); ```
if(hlsPerson != null ){
tempHlsPerson.add(hlsPerson); testService的实现类testServiceImpl定义了batchChildUpdate方法:
hlsPersonService.batchUpdate(iRequest,tempHlsPerson); ```java
Long pid = tempHlsPerson.get(0).getPid();
for(HlsHobby hh : hlsHobbyList){ @Override
hh.setPid(pid); public void batchChildUpdate(IRequest iRequest, HlsPerson hlsPerson, List<HlsHobby> hlsHobbyList) {
} List<HlsPerson> tempHlsPerson = new ArrayList<HlsPerson>();
hlsHobbyService.batchUpdate(iRequest,hlsHobbyList); if(hlsPerson != null ){
} tempHlsPerson.add(hlsPerson);
hlsPersonService.batchUpdate(iRequest,tempHlsPerson);
Long pid = tempHlsPerson.get(0).getPid();
} for(HlsHobby hh : hlsHobbyList){
hh.setPid(pid);
``` }
hlsHobbyService.batchUpdate(iRequest,hlsHobbyList);
}
}
```
> bathUpdate会根据传入的List对象中单个对象的属性值__status判断,如为update即做更新操作,add为插入操作, __status的值由前端paramter函数决定。 > 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