1. specification-新增
- specification.html
- showAll
<tbody>
<tr ng-repeat="entity in list">
<td><input type="checkbox"></td>
<td>{{entity.id}}</td>
<td>{{entity.specName}}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs"
data-toggle="modal" data-target="#editModal">修改</button>
</td>
</tr>
</tbody>
- paination:
<!-- 分页 -->
<tm-pagination conf="paginationConf"></tm-pagination>
- addSpec
<!-- 规格选项 -->
<div class="btn-group">
<button ng-click="addTableRow()" type="button"
class="btn btn-default" title="新建">
<i class="fa fa-file-o"></i> 新增规格选项
</button>
</div>
- addTableRow
// 需要定义好entity的结构,否则不知道specificationOptionList,
// 同时需要对specificationOptionList进行初始化,否则也为null
// $scope.entity = {spcificationOptionList : []};
// 新增选项行
$scope.addTableRow = function() {
$scope.entity.spcificationOptionList.push({});
}
- clearSpec
<button ng-click="entity = {spcificationOptionList : []}" type="button" class="btn btn-default" title="新建"
data-toggle="modal" data-target="#editModal">
<i class="fa fa-file-o"></i> 新建
</button>
2. 删除
- specificationController.js
// 选项批量刪除
$scope.deleTableRow = function(index) {
$scope.entity.spcificationOptionList.splice(index, 1);
}
- specification.html
<td>
<!--使用$index来获取对应ng-repeat中索引 -->
<button ng-click="deleTableRow($index)" type="button"
class="btn btn-default" title="删除">
<i class="fa fa-trash-o"></i> 删除
</button>
</td>
3. 规格的提交保存
- 由于插入规格选项需要知道规格的id,故需要修改pinyougou-dao的TbSpecificationMapper.xml文件,添加特设insert方法:
<insert id="insert" parameterType="com.pinyougou.pojo.TbSpecification" >
<!-- order AFTER 表示 SELECT LAST_INSERT_ID() 在insert执行之后执行,多用与自增主键, -->
<!-- BEFORE 表示 SELECT LAST_INSERT_ID() 在insert执行之前执行,这样的话就拿不到主键了, -->
<!-- 这种适合那种主键不是自增的类型 -->
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
insert into tb_specification (id, spec_name)
values (#{id,jdbcType=BIGINT}, #{specName,jdbcType=VARCHAR})
</insert>
- SpecificationServiceImpl.java
@Override
public void add(Specification specification) {
TbSpecification tbSpecification = specification.getSpecification();
specificationMapper.insert(tbSpecification);// 插入规格
// 循环插入规格选项
List<TbSpecificationOption> specificationOptionList = specification.getSpecificationOptionList();
for (TbSpecificationOption option : specificationOptionList) {
// 设置规格ID
option.setSpecId(tbSpecification.getId());
// 新增规格
specificationOptionMapper.insert(option);
}
}
- specificationController.js
// 保存
$scope.save = function() {
var serviceObject;// 服务层对象
if ($scope.entity.specification.id != null) {// 如果有ID
serviceObject = specificationService.update($scope.entity); // 修改
} else {
serviceObject = specificationService.add($scope.entity);// 增加
}
serviceObject.success(function(response) {
if (response.success) {
// 重新查询
$scope.reloadList();// 重新加载
} else {
alert(response.message);
}
});
}
- Specification.java
package com.pinyougou.pojogroup;
import java.io.Serializable;
import java.util.List;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.pojo.TbSpecificationOption;
/**
* 规格组合的实体类
*
* @author wjt12
*
*/
public class Specification implements Serializable {
private TbSpecification specification;
private List<TbSpecificationOption> specificationOptionList;
// 没有构造函数
public TbSpecification getSpecification() {
return specification;
}
public void setSpecification(TbSpecification specification) {
this.specification = specification;
}
public List<TbSpecificationOption> getSpecificationOptionList() {
return specificationOptionList;
}
public void setSpecificationOptionList(List<TbSpecificationOption> specificationOptionList) {
this.specificationOptionList = specificationOptionList;
}
}
4. 规格的查找:
- 流程:
1)前端ng-click="findOne()";
2)specificationController.java--RequestMapping("/findOne");
3)specificationService->specificationServiceImpl->findOne(Specification sp);
4)ng-model="entity.specification.specName"--ng-repeat="pojo in entity.specificationOptionList"
4.1)前端ng-click="save()"
5)specificationController.java--RequestMapping("/update");
6)specificationService->specificationServiceImpl->update(Specification sp);
2.SpecificationServiceImpl.java
/**
* 根据ID获取实体
*
* @param id
* @return
*/
@Override
public Specification findOne(Long id) {
// 1.查找规格
TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
// 2.创建example
TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
// 3.根据example创建criteria
com.pinyougou.pojo.TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample
.createCriteria();
// 4.根据规格ID查询
criteria.andSpecIdEqualTo(id);
// 5.根据example查询规格选项
List<TbSpecificationOption> specificationOptionList = specificationOptionMapper
.selectByExample(tbSpecificationOptionExample);
// 6.构建Specification
Specification specification = new Specification();
specification.setSpecification(tbSpecification);
specification.setSpecificationOptionList(specificationOptionList);
// 7.返回对象
return specification;
}
3.SpecificationServiceImpl.java
/**
* 修改
*/
@Override
public void update(Specification specification) {
// 1.修改规格
specificationMapper.updateByPrimaryKey(specification.getSpecification());
// 2.构建example
TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
// 3.根据example构建criteria
com.pinyougou.pojo.TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample
.createCriteria();
// 4.添加条件
criteria.andSpecIdEqualTo(specification.getSpecification().getId());
// 5.删除
specificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
// 6.添加规格选项
for (TbSpecificationOption specificationOption : specification.getSpecificationOptionList()) {
// 7.设置规格Id
specificationOption.setSpecId(specification.getSpecification().getId());
// 8.插入
specificationOptionMapper.insert(specificationOption);
}
}
5.规格的删除
- SpecificationServiceImpl.java
/**
* 批量删除
*/
@Override
public void delete(Long[] ids) {
TbSpecificationOptionExample tbSpecificationOptionExample;
for (Long id : ids) {
// 1.删除规格
specificationMapper.deleteByPrimaryKey(id);
// 2.创建example
tbSpecificationOptionExample = new TbSpecificationOptionExample();
// 3.创建criteria
com.pinyougou.pojo.TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample
.createCriteria();
// 4.条件
criteria.andSpecIdEqualTo(id);
// 5.删除
specificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
}
}
- specification.html
<button type="button" class="btn btn-default" title="删除"
ng-click="dele()">
<i class="fa fa-trash-o"></i> 删除
</button>
3.specificationController.js
$scope.dele = function() {
// 获取选中的复选框
specificationService.dele($scope.selectIds).success(function(response) {
if (response.success) {
$scope.reloadList();// 刷新列表
$scope.selectIds = [];
}
});
}
网友评论