e3mall项目第十五天——复习:商品管理模块的商品添加、商品列表展示

商品添加:

在这里插入图片描述
选择类目的功能实现:
创建一个pojo来描述tree的节点信息,包含三个属性id、text、state。放到e3-common工程中。

public class EasyUITreeNode implements Serializable {

	private long id;
	private String text;
	private String state;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	
}

interface:

public interface ItemCatService {

	List<EasyUITreeNode> getItemCatlist(long parentId);
}

service:
参数:long parentId
业务逻辑:
1、根据parentId查询节点列表
2、转换成EasyUITreeNode列表。
3、返回。
返回值:List

/**
 * 商品分类管理
 * <p>Title: ItemCatServiceImpl</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

	@Autowired
	private TbItemCatMapper itemCatMapper;
	
	@Override
	public List<EasyUITreeNode> getItemCatlist(long parentId) {
		//根据parentId查询子节点列表
		TbItemCatExample example = new TbItemCatExample();
		Criteria criteria = example.createCriteria();
		//设置查询条件
		criteria.andParentIdEqualTo(parentId);
		//执行查询
		List<TbItemCat> list = itemCatMapper.selectByExample(example);
		//创建返回结果List
		List<EasyUITreeNode> resultList = new ArrayList<>();
		//把列表转换成EasyUITreeNode列表
		for (TbItemCat tbItemCat : list) {
			EasyUITreeNode node = new EasyUITreeNode();
			//设置属性
			node.setId(tbItemCat.getId());
			node.setText(tbItemCat.getName());
			node.setState(tbItemCat.getIsParent()?"closed":"open");
			//添加到结果列表
			resultList.add(node);
		}
		//返回结果
		return resultList;
	}

}

controller:
初始化tree请求的url:/item/cat/list
参数:
long id(父节点id)
返回值:json。数据格式
List《EasyUITreeNode》

/**
 * 商品分类管理Controller
 * <p>Title: ItemCatController</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Controller
public class ItemCatController {

	@Autowired
	private ItemCatService itemCatService;
	
	@RequestMapping("/item/cat/list")
	@ResponseBody
	public List<EasyUITreeNode> getItemCatList(
			@RequestParam(name="id", defaultValue="0")Long parentId) {
		//调用服务查询节点列表
		List<EasyUITreeNode> list = itemCatService.getItemCatlist(parentId);
		return list;
		
	}
}

上传图片功能的实现
使用的是KindEditor的多图片上传插件。
KindEditor 4.x 文档
http://kindeditor.net/doc.php

请求的url:/pic/upload
参数:MultiPartFile uploadFile
返回值:
可以创建一个pojo对应返回值。可以使用map

业务逻辑:
1、接收页面传递的图片信息uploadFile
2、把图片上传到图片服务器。使用封装的工具类实现。需要取文件的内容和扩展名。
3、图片服务器返回图片的url
4、将图片的url补充完整,返回一个完整的url。
5、把返回结果封装到一个Map对象中返回。

准备:
1、需要把commons-io-fileupload 的jar包添加到工程中。

<!-- 文件上传组件 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
		</dependency>

2、配置多媒体解析器。

<!-- 定义文件上传解析器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设定默认编码 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
		<property name="maxUploadSize" value="5242880"></property>
	</bean>

controller层:

/**
 * 图片上传处理Controller
 * <p>Title: PictureController</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Controller
public class PictureController {
	
	@Value("${IMAGE_SERVER_URL}")
	private String IMAGE_SERVER_URL;

	@RequestMapping(value="/pic/upload", produces=MediaType.TEXT_PLAIN_VALUE+";charset=utf-8")
	@ResponseBody
	public String uploadFile(MultipartFile uploadFile) {
		try {
			//把图片上传的图片服务器
			FastDFSClient fastDFSClient = new FastDFSClient("classpath:conf/client.conf");
			//取文件扩展名
			String originalFilename = uploadFile.getOriginalFilename();
			String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
			//得到一个图片的地址和文件名
			String url = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
			//补充为完整的url
			url = IMAGE_SERVER_URL + url;
			//封装到map中返回
			Map result = new HashMap<>();
			result.put("error", 0);
			result.put("url", url);
			return JsonUtils.objectToJson(result);
		} catch (Exception e) {
			e.printStackTrace();
			Map result = new HashMap<>();
			result.put("error", 1);
			result.put("message", "图片上传失败");
			return JsonUtils.objectToJson(result);
		}
	}
}

商品添加功能的实现:

功能分析
请求的url:/item/save
参数:表单的数据。可以使用pojo接收表单的数据,要求pojo的属性和input的name属性要一致。
使用TbItem对象接收表单的数据。
TbItem item,String desc
返回值:
Json数据。应该包含一个status的属性。
可以使用E3Result,放到e3-common中。

业务逻辑:
1、生成商品id
实现方案:
a)Uuid,字符串,不推荐使用。
b)数值类型,不重复。日期+时间+随机数20160402151333123123
c)可以直接去毫秒值+随机数。可以使用。
d)使用redis。Incr。推荐使用。
使用IDUtils生成商品id
2、补全TbItem对象的属性
3、向商品表插入数据
4、创建一个TbItemDesc对象
5、补全TbItemDesc的属性
6、向商品描述表插入数据
7、E3Result.ok()

interface层:

E3Result addItem(TbItem item, String desc);

service层:

     /**
	 * 添加商品
	 * <p>Title: addItem</p>
	 * <p>Description: </p>
	 * @param item
	 * @param desc
	 * @return
	 * @see cn.e3mall.service.ItemService#addItem(cn.e3mall.pojo.TbItem, java.lang.String)
	 */
	@Override
	public E3Result addItem(TbItem item, String desc) {
		//生成商品id
		final long itemId = IDUtils.genItemId();
		//补全item的属性
		item.setId(itemId);
		//1-正常,2-下架,3-删除
		item.setStatus((byte) 1);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		//向商品表插入数据
		itemMapper.insert(item);
		//创建一个商品描述表对应的pojo对象。
		TbItemDesc itemDesc = new TbItemDesc();
		//补全属性
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setCreated(new Date());
		itemDesc.setUpdated(new Date());
		//向商品描述表插入数据
		itemDescMapper.insert(itemDesc);
		//发送商品添加消息
		jmsTemplate.send(topicDestination, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				TextMessage textMessage = session.createTextMessage(itemId + "");
				return textMessage;
			}
		});
		//返回成功
		return E3Result.ok();
	}

controller层:

/**
	 * 商品添加功能
	 */
	@RequestMapping(value="/item/save", method=RequestMethod.POST)
	@ResponseBody
	public E3Result addItem(TbItem item, String desc) {
		E3Result result = itemService.addItem(item, desc);
		return result;
	}

查询商品列表展示

分页插件PageHelper
使用方法
第一步:把PageHelper依赖的jar包添加到工程中。官方提供的代码对逆向工程支持的不好,使用参考资料中的pagehelper-fix。

<dependency>
				<groupId>com.github.pagehelper</groupId>
				<artifactId>pagehelper</artifactId>
				<version>${pagehelper.version}</version>
</dependency>

第二步:在Mybatis配置xml中配置拦截器插件:

<configuration>
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
        	<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>
</configuration>

响应的json数据格式EasyUIResult:

public class EasyUIDataGridResult implements Serializable{
	
	//总条数
	private long total;
	//一页显示10条,显示多少页
	private List rows;
	public long getTotal() {
		return total;
	}
	public void setTotal(long total) {
		this.total = total;
	}
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	
}

interface:

EasyUIDataGridResult getItemList(int page, int rows);

service:
参数:int page ,int rows
业务逻辑:查询所有商品列表,要进行分页处理。
返回值:EasyUIDataGridResult

/**
	 * 商品列表查询
	 * <p>Title: getItemList</p>
	 * <p>Description: </p>
	 * @param page
	 * @param rows
	 * @return
	 * @see cn.e3mall.service.ItemService#getItemList(int, int)
	 */
	@Override
	public EasyUIDataGridResult getItemList(int page, int rows) {
		//设置分页信息
		PageHelper.startPage(page, rows);
		//执行查询
		TbItemExample example = new TbItemExample();
		List<TbItem> list = itemMapper.selectByExample(example);
		//创建一个返回值对象
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		//取分页结果
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		//取总记录数
		long total = pageInfo.getTotal();
		result.setTotal(total);
		return result;
	}

controller:
1、初始化表格请求的url:/item/list
2、Datagrid默认请求参数:
1、page:当前的页码,从1开始。
2、rows:每页显示的记录数。
3、响应的数据:json数据。E3Result


@RequestMapping("/item/list")
	@ResponseBody
	public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
		//调用服务查询商品列表
		EasyUIDataGridResult result = itemService.getItemList(page, rows);
		return result;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法提供运行图。但是,我可以给你提供一些思路和示例代码。 首先,你需要创建一个Vue实例,并在其定义一个data对象,用于存储品牌列表数据。示例代码如下: ```javascript new Vue({ el: '#app', data: { brands: [ { id: 1, name: 'Apple' }, { id: 2, name: 'Samsung' }, { id: 3, name: 'Huawei' } ], newBrandName: '' }, methods: { addBrand: function() { if (this.newBrandName) { var id = this.brands.length + 1; this.brands.push({ id: id, name: this.newBrandName }); this.newBrandName = ''; } } } }); ``` 在data对象,我们定义了一个brands数组,它包含了三个品牌的信息。我们还定义了一个newBrandName变量,用于存储用户输入的品牌名称。 在methods对象,我们定义了一个addBrand方法,它用于添加品牌。当用户点击添加按钮时,我们会检查newBrandName变量是否为空,如果不为空,则生成一个的品牌id,并将的品牌对象添加到brands数组,最后将newBrandName变量清空。 接下来,你需要在HTML页面使用Vue指令来渲染品牌列表添加表单。示例代码如下: ```html <div id="app"> <ul> <li v-for="brand in brands" :key="brand.id">{{ brand.name }}</li> </ul> <form> <input type="text" v-model="newBrandName" placeholder="请输入品牌名称"> <button type="button" @click="addBrand">添加</button> </form> </div> ``` 在上面的代码,我们使用了v-for指令来循环渲染品牌列表。v-for指令会遍历brands数组的所有品牌,并为每个品牌生成一个li元素。我们还使用了:key属性来指定每个品牌的唯一标识符。 在添加表单,我们使用了v-model指令将用户输入的品牌名称绑定到newBrandName变量上。当用户点击添加按钮时,我们会调用addBrand方法来添加品牌。 最后,你只需要在页面引入Vue的CDN链接即可运行代码。运行效果如下图所示: ![Vue品牌列表查询和添加功能](https://img-blog.csdnimg.cn/img_convert/2ef323e3d7a245e2d7c3a13a9e18ad5b.png)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值