添加商品首先是选择类目
选择类目
分析:点击通常会有一个事件,但是这里没有。应该是一个js方法
搜索到文件
商品分类的叶子节点后,关闭窗口并将选中的商品分类的名称显示到网页上。
1、初始化tree的url:
/item/cat/list
2、请求的参数
Id(当前节点的id,根据此id查询子节点)
3、返回数据的格式json数据:
[{
“id”: 1, //当前节点的id
“text”: “Node 1”, //节点显示的名称
“state”: “closed” //节点的状态,如果是closed就是一个文件夹形式,
// 当打开时还会 做一次请求。如果是open就显示为叶子节点。
},{
“id”: 2,
“text”: “Node 2”,
“state”: “closed”
}]
3.2 Mapper
3.2.1 数据中的表:
3.2.2 Sql语句
SELECT * FROM tb_item_cat
where parent_id=父节点id;
3.3 Service层
功能:根据parentId查询商品分类列表。
参数:parentId
返回值:返回tree所需要的数据结构,是一个节点列表。
可以创建一个tree node的pojo表示节点的数据,也可以使用map。
List
public class TreeNode {
private long id;
private String text;
private String state;
public TreeNode(long id, String text, String state) {
this.id = id;
this.text = text;
this.state = state;
}
}
@Autowired
private TbItemCatMapper itemCatMapper;
@Override
public List<TreeNode> getItemCatList(long parentId) {
//根据parentId查询分类列表
TbItemCatExample example = new TbItemCatExample();
//设置查询条件
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
//执行查询
List<TbItemCat> list = itemCatMapper.selectByExample(example);
//分类列表转换成TreeNode的列表
List<TreeNode> resultList = new ArrayList<>();
for (TbItemCat tbItemCat : list) {
//创建一个TreeNode对象
TreeNode node = new TreeNode(tbItemCat.getId(), tbItemCat.getName(),
tbItemCat.getIsParent()?"closed":"open");
resultList.add(node);
}
return resultList;
}
// private Boolean isParent; 布尔型
`is_parent` tinyint(1) DEFAULT '1' COMMENT '该类目是否为
父类目,1为true,0为false',
Controller
功能:接收页面传递过来的id,作为parentId查询子节点。
value用来指定要传入值的id名称
@RequestMapping("/item/cat/list")
@ResponseBody
public List<TreeNode> getItemCatList
(@RequestParam(value="id",defaultValue="0")Long parentId) {
return itemService.getItemCatList(parentId);
}
图片服务器两个服务:
http:可以使用nginx做静态资源服务器。也可以使用apache。推荐使用nginx,效率更高。
Nginx:
1、http服务
2、反向代理
3、负载均衡
ftp服务:
使用linux做服务器,在linux中有个ftp组件vsftpd。
图片上传
@Test
public void testFtp() throws Exception {
//1、连接ftp服务器
FTPClient ftpClient = new FTPClient();
ftpClient.connect("192.168.25.133", 21);
//2、登录ftp服务器
ftpClient.login("ftpuser", "ftpuser");
//3、读取本地文件
FileInputStream inputStream = new FileInputStream(new File("D:\\Documents\\Pictures\\images\\2010062119283578.jpg"));
//4、上传文件
//1)指定上传目录
ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
//2)指定文件类型
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//第一个参数:文件在远程服务器的名称
//第二个参数:文件流
ftpClient.storeFile("hello.jpg", inputStream);
S1
@RequestMapping("/pic/upload")
@ResponseBody
public String uploadPicture(MultipartFile uploadFile) {
Map result = pictureService.uploadPicture(uploadFile);
String json = JsonUtils.objectToJson(result);
return json;
}
S2 service
加载配置文件的值
#FTP的相关配置
FTP_ADDRESS=192.168.238.129
FTP_PORT=21
FTP_USERNAME=ftpuser
利用spring 读取配置文件了的值
@Value("${FTP_ADDRESS}")
private String FTP_ADDRESS;
@Value("${FTP_PORT}")
private int FTP_PORT;
@Value("${FTP_USERNAME}")
修改-dao.xml文件
<context:property-placeholder location="classpath:resource/*.properties" />
读取*.properties
@Override
public Map uploadPicture(MultipartFile uploadFile) {
Map resultMap = new HashMap<>();
// 修改文件名
String oldName = uploadFile.getOriginalFilename();
String newName = IDUtils.genImageName() + oldName.substring(oldName.lastIndexOf("."));
// 上传文件到ftp服务器
String filePath = new DateTime().toString("/yyyy/MM/dd");
try {
boolean uploadResult = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD,
FTP_BASEPATH, filePath, newName, uploadFile.getInputStream());
if(!uploadResult) {
// error 成功是0 失败是1
resultMap.put("error", 1);
resultMap.put("message", "上传失败");
return resultMap;
}
// 上传成功
resultMap.put("error", 0);
resultMap.put("url", IMAGE_BASE_URL + filePath + "/" + newName);
return resultMap;
} catch (IOException e) {
e.printStackTrace();
resultMap.put("error", 1);
resultMap.put("message", "上传出现异常");
return resultMap;
}
}
6 商品添加功能实现
6.1 功能分析
将数据插入商品表、商品描述表。
// 因为有上传图片所以是post请求
@RequestMapping(value="/item/save", method=RequestMethod.POST)
@ResponseBody
public TaotaoResult saveItem(TbItem item,String desc) throws Exception {
TaotaoResult result = itemService.addItem(item, desc);
return result;
}
service
// 补全信息
Long itemId = IDUtils.genItemId();
item.setId(itemId);
item.setCreated(new Date());
item.setUpdated(new Date());
// 商品状态,1-正常,2-下架,3-删除
item.setStatus((byte) 1);
itemMapper.insert(item);
TaotaoResult result = addItemDesc(itemId,desc);