您当前的位置: 首页 >  分布式

13分布式电商项目 - 品牌管理模块

杨林伟 发布时间:2019-07-02 17:10:38 ,浏览量:3

代码已上传到GitHub 地址:https://github.com/ylw-github/pingyougou.git 版本号:05dade77bb17941f980dcce99567cfdd8d380018

先看一下项目运行的效果: 在这里插入图片描述

表现层代码: brand.html





品牌管理











	//定义模块
	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;//更新总记录数
						}		
					)
					
				}
				

			})



	
	
品牌管理
新建 删除 刷新
品牌名: 首字母: 查询
品牌ID 品牌名称 品牌首字母 操作 {{entity.id}} {{entity.name}} {{entity.firstChar}} 修改
× 品牌编辑
品牌名称 首字母
保存 关闭
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 findAll(){
		//调用远程服务对象方法
		List 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 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 findAll() {
		//创建example对象
		TbBrandExample example = new TbBrandExample();
		// 查询
		List 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 list = brandMapper.selectByExample(example);
		//创建pageINfo对象,获取查询分页数据
		PageInfo pageInfo = new PageInfo(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 list = brandMapper.selectByExample(example);
		//创建PageINfo,获取分页数据
		PageInfo pageInfo = new PageInfo(list);
		//返回分页包装对象
		return new PageResult(pageInfo.getTotal(), list);
	}

}

关注
打赏
1688896170
查看更多评论

杨林伟

暂无认证

  • 3浏览

    0关注

    3183博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0838s