增删改查
mapper.xml内容
<!--分页查询-->
<select id="selByPage" resultMap="BaseResultMap">
select * from
(select rownum rn ,t.* from product t)
where rn <#{end} and rn >#{first}
</select>
<!--添加商品-->
<insert id="insByPage" >
INSERT INTO product(pro_id,pro_name,pro_price,pro_count)VALUES (seq_product.nextval,#{proName},#{proPrice},#{proCount})
</insert>
<!--删除商品-->
<delete id="delete">
delete from product where pro_id=#{proId}
</delete>
<!--修改商品-->
<update id="update">
UPDATE product
<set>
<if test="proName!=null and !proName.equals('')">
pro_name=#{proName},
</if>
<if test="proPrice!=0">
pro_price=#{proPrice},
</if>
<if test="proCount!=0">
pro_count=#{proCount}
</if>
</set>
WHERE pro_id=#{proId}
</update>
controller内容
//添加商品
@RequestMapping("/insBaPage")
public String insByPage(Product product, HttpSession session){
int add = productService.insByPage(product);
if(add!=0){
session.setAttribute("ins","添加成功");
return "forward:/allByPage.do";
}
session.setAttribute("ins","添加失败");
return "forward:/allByPage.do";
}
//删除商品
@RequestMapping("/delete")
public String delete(HttpServletRequest request) throws IOException {
String proId = request.getParameter("id");
int id;
int delete = 0;
if(proId!=null){
id = Integer.parseInt(proId);
delete = productService.delete(id);
}
if(delete>0){
System.out.println("删除成功");
return "forward:/allByPage.do";
}else{
System.out.println("删除失败");
return "forward:/allByPage.do";
}
}
//分页显示
@RequestMapping("/allByPage")
public String showByPage(Model model, HttpServletRequest request){
int pageIndex=1;
int pageSize=5;
int totalPage=0;
//获取数据
String index = request.getParameter("index");
String size = request.getParameter("size");
if(index!=null&&size!=null){
pageIndex = Integer.parseInt(index);
pageSize = Integer.parseInt(size);
}
//调用service
List<Product> products = productService.selProducts();
int total = products.size();
System.out.println("total = " + total);
double s=total%pageSize;
double ss=total/pageSize;
if(s!=0){
totalPage= (int) ss;
System.out.println("ss= " + ss);
totalPage+=1;
}else if(s==0){
totalPage= (int) ss;
System.out.println("ss = " + ss);
}
if(pageIndex<1){
pageIndex=totalPage;
}else if(pageIndex>totalPage){
pageIndex=1;
}
List<Product> selByPage=productService.selByPage(pageIndex,pageSize);
System.out.println("selByPage = " + selByPage);
model.addAttribute("pageIndex",pageIndex);
model.addAttribute("pageSize",pageSize);
model.addAttribute("totalPage",totalPage);
model.addAttribute("productList",selByPage);
return "forward:/admin/product/productList.jsp";
}
//修改商品
@RequestMapping("/update")
public String update(HttpSession session,HttpServletRequest request){
String id = request.getParameter("id");
String name = request.getParameter("name");
String price = request.getParameter("price");
String count = request.getParameter("count");
System.out.println("id:"+id+",name:"+name);
int update = 0;
if(id!=null){
int id1 = Integer.parseInt(id);
if(price!=null){
Double price1 = Double.valueOf(price.toString());
if(count!=null){
int count1 = Integer.parseInt(count);
update = productService.update(id1,name,price1,count1);
System.out.println(update);
}
}
}
if(update>0){
System.out.println("修改成功");
session.setAttribute("upd","修改成功");
return "forward:/allByPage.do";
}else {
System.out.println("修改失败");
session.setAttribute("upd", "修改失败");
return "forward:/allByPage.do";
}
}