1.1分页插件用法com.github.pagehelper pagehelper ${pagehelper.version}
pageInfo用来存储分页所需的各种信息
//传入相应的值
PageHelper.startPage(pageNum,pageSize);
//设置完排序后,取集合,切记切记,一定在取集合之前,设置pageHelper.startPage(pageNum,pageSize)
List list = productInfoMapper.selectByExample(example);
//将查询到的集合封装进PageInfo对象中
PageInfo pageInfo = new PageInfo<>(list);
1.2界面代码
1.3controller层的代码
//ajax分页翻页处理
@ResponseBody
@RequestMapping("/ajaxsplit")
public void ajaxSplit(int page, HttpSession session){
PageInfo info = productInfoService.splitPage(page, PAGE_SIZE);
session.setAttribute("info",info);
}
service层的代码
@Service
public class ProductInfoServiceImpl implements ProductInfoService {
@Autowired
ProductInfoMapper productInfoMapper;
@Override
public List getAll() {
return productInfoMapper.selectByExample(null);
}
@Override
public PageInfo splitPage(int pageNum, int pageSize) {
//分页插件使用PageHelper工具类完成分页设置
PageHelper.startPage(pageNum,pageSize);
//进行pageInfo的数据封装
//进行有条件的查询,必须创建ProductInfoExample对象
ProductInfoExample example = new ProductInfoExample();
//设置排序,按住键降序排序
//select * from product_info order by p_id desc
example.setOrderByClause("p_id desc");
//设置完排序后,取集合,切记切记,一定在取集合之前,设置pageHelper.startPage(pageNum,pageSize)
List list = productInfoMapper.selectByExample(example);
//将查询到的集合封装进PageInfo对象中
PageInfo pageInfo = new PageInfo<>(list);
return pageInfo;
}
}
===========================================================================
//mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
======================================================================
//pruductinfomapper用来放置条件
List selectByExample(ProductInfoExample example);
=======================================================================
//pruductinfomapper.xml
新增商品
新增商品业务逻辑层的实现
完成商品类别的绑定
查询全部商品名称
@Service("ProductTypeServiceImpl")
public class ProductTypeServiceImpl implements ProductTypeService {
@Autowired
ProductTypeMapper productTypeMapper;
@Override
public List getAll() {
return productTypeMapper.selectByExample(new ProductTypeExample());
}
}
商品类别监听器开发
Q:为什么要使用监听器?
A:因为以后可能会有很多地方要用到查看类别的功能,监听器随着服务器的启动而加载,可以减少数据库的负载
@WebListener
public class ProductTypeListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//手工从spring容器中取出ProductTypeServiceImpl的对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_*.xml");
ProductTypeService productTypeService = (ProductTypeService) context.getBean("ProductTypeServiceImpl");
List typeList = productTypeService.getAll();
//放入全局作用域中,供新增页面,修改页面,前台的查询功能提供全部商品类别集合
servletContextEvent.getServletContext().setAttribute("typeList",typeList);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
页面下拉列表框显示商品类型
异步ajax图片上传类别
点击图片 上传后换成一个全新的名字 ,同时进行回显 在提交按钮点击之前图片已经上传,并回显,所以按下提交之后不需要对图片进行操作
OTOD:借助jquery的插件
页面层代码
图片介绍
function fileChange(){//注意:此处不能使用jQuery中的change事件,因此仅触发一次,因此使用标签的:onchange属性
alert("change");
$.ajaxFileUpload({
url: "${pageContext.request.contextPath}/prod/ajaxImg.action",//用于文件上传的服务器端请求地址
secureuri: false,//一般设置为false
fileElementId: 'pimage',//文件上传控件的id属性
dataType: 'json',//返回值类型 一般设置为json
success: function(obj) //服务器成功响应处理函数
{
alert(obj);
$("#imgDiv").empty(); //清空原有数据
//创建img 标签对象
var imgObj = $("");
//给img标签对象追加属性
imgObj.attr("src","/image_big/"+obj.imgurl);
imgObj.attr("width","100px");
imgObj.attr("height","100px");
//将图片img标签追加到imgDiv末尾
$("#imgDiv").append(imgObj);
//将图片的名称(从服务端返回的JSON中取得)赋值给文件本框
//$("#imgName").html(data.imgName);
},
error: function (e)//服务器响应失败处理函数
{
alert(e.message);
}
});
}
服务端上传图片
需要借用图片命名工具
public class FileNameUtil {
//根据UUID生成文件名
public static String getUUIDFileName() {
UUID uuid = UUID.randomUUID();
return uuid.toString().replace("-", "");
}
//从请求头中提取文件名和类型
public static String getRealFileName(String context) {
// Content-Disposition: form-data; name="myfile"; filename="a_left.jpg"
int index = context.lastIndexOf("=");
String filename = context.substring(index + 2, context.length() - 1);
return filename;
}
//根据给定的文件名和后缀截取文件名
public static String getFileType(String fileName){
//9527s.jpg
int index = fileName.lastIndexOf(".");
return fileName.substring(index);
}
}
要上传文件需要借用springMVC提供的文件上传组件,极大简化文件上传的功能
controller层的开发
//异步ajax文件上传处理
@ResponseBody
@RequestMapping("/ajaxImg")
//MultipartFile专门用来接收上传的文件流
//要求action方法中参数的名称,要和提交的name名称完全一样
public Object ajaxImg(MultipartFile pimage,HttpServletRequest request) {
//提取生成文件名UUID+上传图片的后缀.jpg .png
String saveFileName = FileNameUtil.getUUIDFileName() + FileNameUtil.getFileType(pimage.getOriginalFilename());
//等到项目中图片存储的路径
String path = request.getServletContext().getRealPath("/image_big");
//转存
try {
pimage.transferTo(new File(path + File.separator + saveFileName));
} catch (IOException e) {
e.printStackTrace();
}
//添加json的依赖,返回客户端JSPN对象,封装图片路径,为了在页面实现立刻回显
JSONObject object = new JSONObject();
object.put("imgurl",saveFileName);
return object.toString();
}
}
商品增加Actiond的开发
前端添加表单提交地址如下
function condition(){
var pname= $("#pname").val();
var typeid = $("#typeid").val();
var lprice = $("#lprice").val();
var hprice= $("#hprice").val();
$.ajax({
type:"post",
url:"${pageContext.request.contextPath}/prod/condition.action",
data: {"pname":pname,"typeid":typeid,"lprice":lprice,"hprice":hprice},
success:function (){
$("#table").load("http://localhost:8888/admin/product.jsp #table");
}
})
}



