代码已上传到GitHub
地址:https://github.com/ylw-github/pingyougou.git
版本号:05dade77bb17941f980dcce99567cfdd8d380018
先看一下项目运行的效果:
表现层代码:
brand.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>品牌管理</title> <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport"> <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css"> <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css"> <link rel="stylesheet" href="../css/style.css"> <link rel="stylesheet" href="../plugins/angularjs/pagination.css"> <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script> <script src="../plugins/bootstrap/js/bootstrap.min.js"></script> <script src="../plugins/angularjs/angular.min.js"></script> <script src="../plugins/angularjs/pagination.js"></script> <script type="text/javascript"> //定义模块 var app = angular.module("pyg", [ 'pagination' ]); //定义控制器 app.controller( "brandController", function($scope, $http) { //查询所有函数 $scope.findAll = function() { //使用内置服务发送请求 $http.get("../brand/findAll").success(function(data) { $scope.list = data; }) }; //重新加载列表 数据 $scope.reloadList = function() { //切换页码 $scope.search($scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage); } //分页控件配置 $scope.paginationConf = { currentPage : 1, totalItems : 10, itemsPerPage : 10, perPageOptions : [ 10, 20, 30, 40, 50 ], onChange : function() { $scope.reloadList();//重新加载 } }; //分页 $scope.findPage = function(page, rows) { $http.get('../brand/findPage?page=' + page + '&rows='+ rows).success(function(data) { $scope.list = data.rows; $scope.paginationConf.totalItems = data.total;//更新总记录数 }); }; //添加函数 $scope.add = function(){ //定义方法名 var methodName = "add"; //判断如果id存在,是修改 if($scope.entity.id!=null){ methodName="update"; } //内置服务发送请求 $http.post("../brand/"+methodName,$scope.entity).success(function(data){ //判断 if(data.success){ //刷新 $scope.reloadList(); }else{ alert(data.message); } }) }; //根据id查询品牌 $scope.findOne = function(id){ //发送请求 $http.get("../brand/findOne?id="+id).success( function(data){ $scope.entity = data; } ) } //初始化对象 $scope.searchEntity = {}; //条件查询方法 $scope.search = function(page,rows){ //发送条件查询请求 $http.post("../brand/search?page="+page+"&rows="+rows,$scope.searchEntity).success( function(data){ $scope.list = data.rows; $scope.paginationConf.totalItems = data.total;//更新总记录数 } ) } }) </script> </head> <body ng-app="pyg" ng-controller="brandController" class="hold-transition skin-red sidebar-mini"> <!-- .box-body --> <div class="box-header with-border"> <h3 class="box-title">品牌管理</h3> </div> <div class="box-body"> <!-- 数据表格 --> <div class="table-box"> <!--工具栏--> <div class="pull-left"> <div class="form-group form-inline"> <div class="btn-group"> <button type="button" class="btn btn-default" title="新建" data-toggle="modal" ng-click="entity={}" data-target="#editModal"> <i class="fa fa-file-o"></i> 新建 </button> <button type="button" class="btn btn-default" title="删除"> <i class="fa fa-trash-o"></i> 删除 </button> <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"> <i class="fa fa-refresh"></i> 刷新 </button> </div> </div> </div> <div class="box-tools pull-right"> 品牌名:<input type="text" ng-model="searchEntity.name" > 首字母:<input type="text" ng-model="searchEntity.firstChar" > <button ng-click="reloadList()">查询</button> <div class="has-feedback"></div> </div> <!--工具栏/--> <!--数据列表--> <table id="dataList" class="table table-bordered table-striped table-hover dataTable"> <thead> <tr> <th class="" style="padding-right: 0px"><input id="selall" type="checkbox" class="icheckbox_square-blue"></th> <th class="sorting_asc">品牌ID</th> <th class="sorting">品牌名称</th> <th class="sorting">品牌首字母</th> <th class="text-center">操作</th> </tr> </thead> <tbody> <tr ng-repeat="entity in list"> <td><input type="checkbox"></td> <td>{{entity.id}}</td> <td>{{entity.name}}</td> <td>{{entity.firstChar}}</td> <td class="text-center"> <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" ng-click="findOne(entity.id)" data-target="#editModal">修改</button> </td> </tr> </tbody> </table> <!--数据列表/--> <tm-pagination conf="paginationConf"></tm-pagination> </div> <!-- 数据表格 /--> </div> <!-- /.box-body --> <!-- 编辑窗口 --> <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h3 id="myModalLabel">品牌编辑</h3> </div> <div class="modal-body"> <table class="table table-bordered table-striped" width="800px"> <tr> <td>品牌名称</td> <td><input class="form-control" ng-model="entity.name" placeholder="品牌名称"> </td> </tr> <tr> <td>首字母</td> <td><input class="form-control" ng-model="entity.firstChar" placeholder="首字母"></td> </tr> </table> </div> <div class="modal-footer"> <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" ng-click="add()">保存</button> <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button> </div> </div> </div> </div> </body> </html>
BrandController
package com.pyg.manager.controller; import java.util.List; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.pyg.manager.service.BrandService; import com.pyg.pojo.TbBrand; import com.pyg.utils.PageResult; import com.pyg.utils.PygResult; @RestController @RequestMapping("/brand") public class BrandController { //注入远程服务对象 @Reference(timeout=10000000,retries=2) private BrandService brandService; /** * 需求:查询所有品牌数据 */ @RequestMapping("/findAll") public List<TbBrand> findAll(){ //调用远程服务对象方法 List<TbBrand> list = brandService.findAll(); return list; } /** * 需求:分页展示品牌列表 * 参数:Integer page,Integer rows * 返回值:分页包装类对象PageResult */ @RequestMapping("/findPage") public PageResult findPage(Integer page,Integer rows){ //调用远程服务对象 PageResult result = brandService.findPage(page, rows); return result; } /** * 需求:添加品牌数据 * 参数:TbBrand brand * 返回值:PygResult * */ @RequestMapping("/add") public PygResult add(@RequestBody TbBrand brand){ //调用远程服务 PygResult result = brandService.add(brand); return result; } /** * 需求:根据id查询品牌数据 * 请求:../brand/findOne?id=? * 参数:Long id * 返回值:TbBrand */ @RequestMapping("/findOne") public TbBrand findOne(Long id){ //调用远程服务 TbBrand brand = brandService.findOne(id); return brand; } /** * 需求:更新品牌数据 * 请求:/brand/update * 参数:TbBrand brand * 返回值:PygResult */ @RequestMapping("/update") public PygResult update(@RequestBody TbBrand brand){ //调用远程服务 PygResult result = brandService.update(brand); return result; } /** * 需求:品牌条件查询 * 参数:TbBrand brand * 返回值:PygResult */ @RequestMapping("search") public PageResult search(@RequestBody TbBrand brand, @RequestParam(defaultValue="1") Integer page, @RequestParam(defaultValue="10") Integer rows){ //调用服务层条件查询方法 PageResult result = brandService.findBrandByPage(brand, page, rows); return result; } }
服务层代码
BrandService
package com.pyg.manager.service; import java.util.List; import com.pyg.pojo.TbBrand; import com.pyg.utils.PageResult; import com.pyg.utils.PygResult; public interface BrandService { /** * 需求:查询所有品牌数据 */ public List<TbBrand> findAll(); /** * 需求:分页展示品牌列表 * 参数:Integer page,Integer rows * 返回值:分页包装类对象PageResult */ public PageResult findPage(Integer page, Integer rows); /** * 需求:添加品牌数据 * 参数:TbBrand brand * 返回值:PygResult * */ public PygResult add(TbBrand brand); /** * 需求:根据id查询品牌数据 * 参数:Long id * 返回值:TbBrand */ public TbBrand findOne(Long id); /** * 需求:更新品牌数据 * 参数:TbBrand brand * 返回值:PygResult */ public PygResult update(TbBrand brand); /** * 需求:品牌条件查询 * 参数:TbBrand brand * 返回值:PygResult */ public PageResult findBrandByPage(TbBrand brand, Integer page, Integer rows); }
BrandServiceImpl
package com.pyg.manager.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.dubbo.config.annotation.Service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.pyg.manager.service.BrandService; import com.pyg.mapper.TbBrandMapper; import com.pyg.pojo.TbBrand; import com.pyg.pojo.TbBrandExample; import com.pyg.pojo.TbBrandExample.Criteria; import com.pyg.utils.PageResult; import com.pyg.utils.PygResult; @Service public class BrandServiceImpl implements BrandService{ //注入mapper接口代理对象 @Autowired private TbBrandMapper brandMapper; @Override public List<TbBrand> findAll() { //创建example对象 TbBrandExample example = new TbBrandExample(); // 查询 List<TbBrand> list = brandMapper.selectByExample(example); return list; } /** * 需求:分页展示品牌列表 * 参数:Integer page,Integer rows * 返回值:分页包装类对象PageResult */ public PageResult findPage(Integer page, Integer rows) { //创建example对象 TbBrandExample example = new TbBrandExample(); // 使用插件设置分页 PageHelper.startPage(page, rows); //查询品牌数据 //List={page[total,pagesie],list} List<TbBrand> list = brandMapper.selectByExample(example); //创建pageINfo对象,获取查询分页数据 PageInfo<TbBrand> pageInfo = new PageInfo<TbBrand>(list); return new PageResult(pageInfo.getTotal(), list); } /** * 需求:添加品牌数据 * 参数:TbBrand brand * 返回值:PygResult * */ public PygResult add(TbBrand brand) { try { //保存品牌数据 brandMapper.insertSelective(brand); //保存成功 return new PygResult(true, "保存成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); //保存成功 return new PygResult(false, "保存失败"); } } /** * 需求:根据id查询品牌数据 * 参数:Long id * 返回值:TbBrand */ public TbBrand findOne(Long id) { //根据id查询品牌数据 TbBrand brand = brandMapper.selectByPrimaryKey(id); return brand; } /** * 需求:更新品牌数据 * 参数:TbBrand brand * 返回值:PygResult */ public PygResult update(TbBrand brand) { // TODO Auto-generated method stub try { brandMapper.updateByPrimaryKeySelective(brand); //保存成功 return new PygResult(true, "修改成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); //保存成功 return new PygResult(false, "修改失败"); } } /** * 需求:品牌条件查询 * 参数:TbBrand brand * 返回值:PygResult */ public PageResult findBrandByPage(TbBrand brand, Integer page, Integer rows) { // 创建example对象 TbBrandExample example = new TbBrandExample(); //创建criteria对象 Criteria createCriteria = example.createCriteria(); //设置参数 //判断参数是否存在 if(brand.getName()!=null && !"".equals(brand.getName())){ //模糊查询 createCriteria.andNameLike("%"+brand.getName()+"%"); } if(brand.getFirstChar()!=null && !"".equals(brand.getFirstChar())){ createCriteria.andFirstCharEqualTo(brand.getFirstChar()); } //查询之前,必须设置分页 PageHelper.startPage(page,rows); //执行查询 List<TbBrand> list = brandMapper.selectByExample(example); //创建PageINfo,获取分页数据 PageInfo<TbBrand> pageInfo = new PageInfo<TbBrand>(list); //返回分页包装对象 return new PageResult(pageInfo.getTotal(), list); } }