u4 day03
index.jsp页面
- 访问地址中若不想输入项目名称,在web工程中pom.xml端口号下方加path
[TOC]
1.分页查询
1.1 接口
/**
* 分页查询商品信息
* @param pageNow 当前页码
* @param pageSize 每页显示的信息条数
* @return 一页信息
* 注意导入pageHelper依赖
*/
PageInfo<EasybuyProduct> getProductPages(Integer pageNow,Integer pageSize);
1.2 common工程中,pager类
<!--实现serializable接口-->
属性: private Integer pageCount; //总的页数
private Long count; //总的信息条数
private String url; //当前要跳转的url地址
private Integer currentPage; //当前页码
private Integer pageSize; //每页显示的信息条数
方法:public Integer getPageCount() {
//直接将计算的结果返回
this.pageCount = (int)(count / pageSize);
if(count % pageSize != 0){
pageCount++;
}
return pageCount;
}
...省略getter setter
1.3 接口实现类
@Override
public PageInfo<EasybuyProduct> getProductPages(Integer pageNow, Integer pageSize) {
if(pageNow == null){
pageNow = 1; //默认是显示第一页
}
if(pageSize == null){
pageSize = 8; //默认每页展示8条信息
}
//开始分页
PageHelper.startPage(pageNow, pageSize);
//查询所有的商品
EasybuyProductExample example = new EasybuyProductExample();
//id进行降序排序
example.setOrderByClause("id desc");
List<EasybuyProduct> list = easybuyProductMapper.selectByExample(example);
//转换成pageInfo对象
PageInfo<EasybuyProduct> pageInfo = new PageInfo<>(list);
return pageInfo;
}
1.4控制层 controller
/**
* 进入到商品管理页面
*/
@RequestMapping(value="/product/list",method=RequestMethod.GET)
public String productList(@RequestParam(name="currentPage",required=false,defaultValue="1") Integer pageNow,
@RequestParam(name="pageSize",required=false,defaultValue="8")Integer pageSize,
Model model){
//获取到商品列表
PageInfo<EasybuyProduct> pageInfo = productService.getProductPages(pageNow, pageSize);
model.addAttribute("productList", pageInfo.getList());
Pager pager = new Pager();
pager.setCurrentPage(pageNow);
pager.setCount(pageInfo.getTotal());
pager.setPageSize(pageSize);
pager.setUrl("product/list?1=1");
model.addAttribute("pager", pager);
model.addAttribute("menu", 5);
return "productList";
}
2.新增
由jsp页面的需求-->controller需要传入的对象-->在接口中编写方法得到需要的对象-->相应的实现类
2.1 接口
public interface ProductCategoryService {
/**
* 根据父id查询分类列表
* @param parentId
* @return
*/
List<EasybuyProductCategory> getByParentId(Integer parentId);
}
2.2 接口实现类
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService{
@Autowired
private EasybuyProductCategoryMapper easybuyProductCategoryMapper;
@Override
public List<EasybuyProductCategory> getByParentId(Integer parentId) {
EasybuyProductCategoryExample example = new EasybuyProductCategoryExample();
Criteria criteria = example.createCriteria();
criteria.andParentidEqualTo(parentId);
List<EasybuyProductCategory> list = easybuyProductCategoryMapper.selectByExample(example);
return list;
}
}
2.3 dubbo服务 ProductCategoryService接口的暴露
<dubbo:service interface="com.kgc.service.ProductCategoryService" ref="productCategoryServiceImpl"></dubbo:service>
2.4 springmvc.xml dubbo服务的引用
<dubbo:reference interface="com.kgc.service.ProductCategoryService" id="productCategoryService"></dubbo:reference>
2.5 common工程中添加 EasyBuyResult.java
package com.kgc.common.pojo;
import java.io.Serializable;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EasyBuyResult implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
// 响应业务状态
private Integer status;
// 响应消息
private String msg;
// 响应中的数据
private Object data;
public static EasyBuyResult build(Integer status, String msg, Object data) {
return new EasyBuyResult(status, msg, data);
}
public static EasyBuyResult ok(Object data) {
return new EasyBuyResult(data);
}
public static EasyBuyResult ok() {
return new EasyBuyResult(null);
}
public EasyBuyResult() {
}
public static EasyBuyResult build(Integer status, String msg) {
return new EasyBuyResult(status, msg, null);
}
public EasyBuyResult(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public EasyBuyResult(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}
// public Boolean isOK() {
// return this.status == 200;
// }
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
/**
* 将json结果集转化为EasyBuyResult对象
*
* @param jsonData json数据
* @param clazz EasyBuyResult中的object类型
* @return
*/
public static EasyBuyResult formatToPojo(String jsonData, Class<?> clazz) {
try {
if (clazz == null) {
return MAPPER.readValue(jsonData, EasyBuyResult.class);
}
JsonNode jsonNode = MAPPER.readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (clazz != null) {
if (data.isObject()) {
obj = MAPPER.readValue(data.traverse(), clazz);
} else if (data.isTextual()) {
obj = MAPPER.readValue(data.asText(), clazz);
}
}
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
} catch (Exception e) {
return null;
}
}
/**
* 没有object对象的转化
*
* @param json
* @return
*/
public static EasyBuyResult format(String json) {
try {
return MAPPER.readValue(json, EasyBuyResult.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Object是集合转化
*
* @param jsonData json数据
* @param clazz 集合中的类型
* @return
*/
public static EasyBuyResult formatToList(String jsonData, Class<?> clazz) {
try {
JsonNode jsonNode = MAPPER.readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (data.isArray() && data.size() > 0) {
obj = MAPPER.readValue(data.traverse(),
MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
}
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
} catch (Exception e) {
return null;
}
}
}
2.6 控制层 controller
@Autowired
private ProductCategoryService productCategoryService;
/**
* 进入到商品上架,显示一级分类
*/
@RequestMapping(value="/product/toadd",method=RequestMethod.GET)
public String toadd(Model model){
//查询一级分类
List<EasybuyProductCategory> productCategoryList1 = productCategoryService.getByParentId(0);
//传递一级分类到商品添加页面
model.addAttribute("productCategoryList1",productCategoryList1);
model.addAttribute("menu", 6);
return "toAddProduct";
}
/**
* 进入到商品上架,通过父级分类,查得当前分类
*/
@RequestMapping(value="/queryProductCategoryList",method=RequestMethod.POST)
@ResponseBody
public EasyBuyResult queryProductCategoryList(@RequestParam("parentId")Integer parentId){
try {
//查询下级分类
List<EasybuyProductCategory> productCategoryList2 = productCategoryService.getByParentId(parentId);
//传递下级分类到商品添加页面
return new EasyBuyResult(productCategoryList2);
} catch (Exception e) {
e.printStackTrace();
return EasyBuyResult.build(400, "查询分类失败");
}
}
3. 图片上传(采用fastDFS分布式文件系统)
见doc文档
3.1 安装图片服务器虚拟机
3.2 将fastdfs_client war包导入工作空间
3.3 web工程中导入dfs依赖
3 .4 web工程中添加properties/fdfs_conf.conf文件 写入tracker服务器的地址
3.5 封装后的图片上传
3.5.1 将FastDFSClient.java放入common工程中
3.5.2 导入fastDFS依赖
3.6 封装前后变化
<!--封装前测试图片上传-->
@Test
public void testUpload() throws Exception{
//1.导入fastDFS的jar包依赖
//2.加载配置文件,配置文件的内容就是tracker服务器的地址
ClientGlobal.init("E:\\k8507WorkSpace\\easybuy-manager-web\\src\\main\\resources\\properties\\fdfs_conf.conf");
//3.创建TrackerClient对象
TrackerClient trackerClient = new TrackerClient();
//4.创建tackerService对象
TrackerServer trackerServer = trackerClient.getConnection();
//5.创建StrageServer对象
StorageServer storageServer = null;
//6.创建StroageClient对象
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//7.使用storageClient实现图片的上传
String[] strings = storageClient.upload_file("D:/img.jpg", "jpg", null);
//8.遍历字符串数组,得到group和文件名
for (String string : strings) {
System.out.println(string);
}
}
<!--封装后测试图片上传-->
@Test
public void testFastDFSUp2() throws Exception{
FastDFSClient client = new FastDFSClient("E:\\k8507WorkSpace\\easybuy-manager-web\\src\\main\\resources\\properties\\fdfs_conf.conf");
String str = client.uploadFile("D:/img.jpg");
System.out.println("封装后文件上传" + str);
}
网友评论