版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/85538675
在一般的网上商城系统中都会有商品信息的增删改查,本系统也一样
此功能实现按照 持久层——dao层——service层——controller层——jsp层
一、持久层实现
在com.po包中创建Goods持久化类,其详细代码如下所示:
package com.po;
import org.springframework.web.multipart.MultipartFile;
public class Goods {
private Integer id;
private String gname;
private Double goprice;
private Double grprice;
private Integer gstore;
private MultipartFile logoImage;
private String gpicture;
private Integer goodstype_id;
private String typename;//查询时使用
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public Double getGoprice() {
return goprice;
}
public void setGoprice(Double goprice) {
this.goprice = goprice;
}
public Double getGrprice() {
return grprice;
}
public void setGrprice(Double grprice) {
this.grprice = grprice;
}
public Integer getGstore() {
return gstore;
}
public void setGstore(Integer gstore) {
this.gstore = gstore;
}
public MultipartFile getLogoImage() {
return logoImage;
}
public void setLogoImage(MultipartFile logoImage) {
this.logoImage = logoImage;
}
public String getGpicture() {
return gpicture;
}
public void setGpicture(String gpicture) {
this.gpicture = gpicture;
}
public Integer getGoodstype_id() {
return goodstype_id;
}
public void setGoodstype_id(Integer goodstype_id) {
this.goodstype_id = goodstype_id;
}
public String getTypename() {
return typename;
}
public void setTypename(String typename) {
this.typename = typename;
}
}
二、dao层实现
dao层由dao层接口和mybatis的映射文件组成
在com.dao包中创建dao层接口AdminGoodsDao,其详细代码如下所示:
package com.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import com.po.Goods;
@Repository("adminGoodsDao")
@Mapper
public interface AdminGoodsDao {
public List<Goods> selectGoods();
public List<Goods> selsectALLGoodsByPage(Map<String, Object> map);
public int addGoods(Goods goods);
public Goods selectGoodsById(Integer id);
public int deleteGoods(List<Integer> ids);
public int deleteAGoods(Integer id);
public int updateGoodsById(Goods goods);
public List<Map<String, Object>> selectCartGoods(Integer id);
public List<Map<String, Object>> selectFocusGoods(Integer id);
public List<Map<String, Object>> selectOrderdetailGoods(Integer id);
}
其与接口同名的映射文件详细代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.AdminGoodsDao">
<!-- 查询商品 -->
<select id="selectGoods" resultType="Goods">
select * from goodstable
</select>
<!-- 分页查询商品 -->
<select id="selectALLGoodsByPage" resultType="Goods" parameterType="map">
select * from goodstable order by id limit #{startIndex},#{perPageSize}
</select>
<!-- 添加商品 -->
<insert id="addGoods" parameterType="Goods">
insert into goodstable (id,gname,goprice,grprice,gstore,gpicture,goodstype_id)
values (null, #{gname}, #{goprice}, #{grprice}, #{gstore}, #{gpicture}, #{goodstype_id})
</insert>
<!-- 根据id查询商品 -->
<select id="selectGoodsById" resultType="Goods" parameterType="Integer">
select gt.*,gy.typename from goodstable gt,goodstype gy where gt.id=#{id} and gt.goodstype_id = gy.id
</select>
<!-- 删除多个商品 -->
<delete id="deleteGoods" parameterType="List">
delete from goodstable where id in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</delete>
<!-- 删除单个商品 -->
<delete id="deleteAGoods" parameterType="Integer">
delete from goodstable where id=#{id}
</delete>
<!-- 修改一个商品 -->
<update id="updateGoodsById" parameterType="Goods">
update goodstable
<set>
<if test="gname != null">
gname = #{gname},
</if>
<if test="goprice != null">
goprice = #{goprice},
</if>
<if test="grprice != null">
grprice = #{grprice},
</if>
<if test="gstore != null">
gstore = #{gstore},
</if>
<if test="gpicture != null">
gpicture = #{gpicture},
</if>
<if test="goodstype_id != null">
goodstype_id = #{goodstype_id},
</if>
</set>
where id = #{id}
</update>
<!-- 查询关联商品 -->
<select id="selectCartGoods" parameterType="Integer" resultType="map">
select * from carttable where goodstable_id=#{id}
</select>
<select id="selectFocusGoods" parameterType="Integer" resultType="map">
select * from focustable where goodstable_id=#{id}
</select>
<select id="selectOrderdetailGoods" parameterType="Integer" resultType="map">
select * from orderdetail where goodstable_id=#{id}
</select>
</mapper>
三、业务层service层实现
此层由service接口以及其实现类组成,
在com.service.admin包中创建AdminGoodsService接口,其详细代码如下所示:
package com.service.admin;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.po.Goods;
public interface AdminGoodsService {
public String selectAGoods(Model model,Integer id,String act);
public String deleteGoods(Integer ids[],Model model);
public String deleteAGoods(Integer id,Model model);
public String addOrUpdateGoods(Goods goods,HttpServletRequest request,String updateAct);
public String selectGoods(Model model,Integer pageCur,String act);
}
在com.service.admin包中创建其对应的接口实现类AdminGoodsServiceImpl,其详细代码如下所示:
package com.service.admin;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import com.dao.AdminGoodsDao;
import com.po.Goods;
import com.util.MyUtil;
@Service("adminGoodsService")
@Transactional
public class AdminGoodsServiceImpl implements AdminGoodsService {
@Autowired
private AdminGoodsDao adminGoodsDao;
//查询一个商品
@Override
public String selectAGoods(Model model, Integer id, String act) {
Goods agoods = adminGoodsDao.selectGoodsById(id);
model.addAttribute("goods", agoods);
//修改页面
if("updateAgoods".equals(act)){
return "admin/updateAgoods";
}
//详情页面
return "admin/goodsDetail";
}
@Override
public String deleteGoods(Integer[] ids, Model model) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < ids.length; i++) {
//商品有关联
if(adminGoodsDao.selectCartGoods(ids[i]).size() > 0 ||
adminGoodsDao.selectFocusGoods(ids[i]).size() > 0 ||
adminGoodsDao.selectOrderdetailGoods(ids[i]).size() > 0) {
model.addAttribute("msg", "商品有关联,不允许删除!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
list.add(ids[i]);
}
adminGoodsDao.deleteGoods(list);
model.addAttribute("msg", "成功删除商品!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
@Override
public String deleteAGoods(Integer id, Model model) {
//商品有关联
if(adminGoodsDao.selectCartGoods(id).size() > 0 ||
adminGoodsDao.selectFocusGoods(id).size() > 0 ||
adminGoodsDao.selectOrderdetailGoods(id).size() > 0) {
model.addAttribute("msg", "商品有关联,不允许删除!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
adminGoodsDao.deleteAGoods(id);
model.addAttribute("msg", "成功删除商品!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
@Override
public String addOrUpdateGoods(Goods goods, HttpServletRequest request, String updateAct) {
/*上传文件的保存位置"/logos",该位置是指
workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps,
发布后使用*/
//防止文件名重名
String newFileName = "";
String fileName = goods.getLogoImage().getOriginalFilename();
//选择了文件
if(fileName.length() > 0){
String realpath = request.getServletContext().getRealPath("logos");
//实现文件上传
String fileType = fileName.substring(fileName.lastIndexOf('.'));
//防止文件名重名
newFileName = MyUtil.getStringID() + fileType;
goods.setGpicture(newFileName);
File targetFile = new File(realpath, newFileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//上传
try {
goods.getLogoImage().transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
//修改
if("update".equals(updateAct)){//updateAct不能与act重名,因为使用了转发
//修改到数据库
if(adminGoodsDao.updateGoodsById(goods) > 0){
return "forward:/adminGoods/selectGoods?act=updateSelect";
}else{
return "/adminGoods/updateAgoods";
}
}else{//添
//保存到数据库
if(adminGoodsDao.addGoods(goods) > 0){
//转发到查询的controller
return "forward:/adminGoods/selectGoods";
}else{
return "card/addCard";
}
}
}
@Override
public String selectGoods(Model model, Integer pageCur, String act) {
List<Goods> allGoods = adminGoodsDao.selectGoods();
int temp = allGoods.size();
model.addAttribute("totalCount", temp);
int totalPage = 0;
if (temp == 0) {
totalPage = 0;//总页数
} else {
//返回大于或者等于指定表达式的最小整数
totalPage = (int) Math.ceil((double) temp / 10);
}
if (pageCur == null) {
pageCur = 1;
}
if ((pageCur - 1) * 10 > temp) {
pageCur = pageCur - 1;
}
//分页查询
Map<String, Object> map = new HashMap<String, Object>();
map.put("startIndex", (pageCur - 1) * 10);//起始位置
map.put("perPageSize", 10);//每页10个
allGoods = adminGoodsDao.selectAllGoodsByPage(map);
model.addAttribute("allGoods", allGoods);
model.addAttribute("totalPage", totalPage);
model.addAttribute("pageCur", pageCur);
//删除查询
if("deleteSelect".equals(act)){
return "admin/deleteSelectGoods";
}
//修改查询
else if("updateSelect".equals(act)){
return "admin/updateSelectGoods";
}else{
return "admin/selectGoods";
}
}
}
四、控制层实现
在com.controller.admin包中创建AdminGoodsController,其详细代码如下所示:
package com.controller.admin;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.Goods;
import com.service.admin.AdminGoodsService;
@Controller
@RequestMapping("/adminGoods")
public class AdminGoodsController extends BaseController{
@Autowired
private AdminGoodsService adminGoodsService;
//查询所有商品
@RequestMapping("/selectGoods")
public String aeleceGoods(Model model,Integer pageCur,String act) {
return adminGoodsService.selectGoods(model, pageCur, act);
}
//页面初始化
@RequestMapping("/toAddGoods")
public String toAddGoods(Model model) {
model.addAttribute("goods",new Goods());
return "admin/addGoods";
}
//添加或修改
@RequestMapping("/addGoods")
public String addGoods(@ModelAttribute Goods goods,HttpServletRequest request ,String updateAct) {
return adminGoodsService.addOrUpdateGoods(goods, request, updateAct);
}
//查询一个名片
@RequestMapping("/selectAGoods")
public String selectAGoods(Model model ,Integer id,String act) {
return adminGoodsService.selectAGoods(model, id, act);
}
//删除多个商品
@RequestMapping("/deleteGoods")
public String deleteGoods(Integer ids[],Model model) {
return adminGoodsService.deleteGoods(ids, model);
}
//删除一个商品
@RequestMapping("/deleteAGoods")
public String deleteAGoods(Integer id,Model model) {
return adminGoodsService.deleteAGoods(id, model);
}
}
五、实现jsp层
本层共需要实现六个jsp页面,分别是:添加addGoods.jsp——商品详情GoodsDetail.jsp——查询selectGoods.jsp——修改查询页面updateSelectGoods.jsp——修改updateGoods.jsp——删除deleteGoods.jsp
由于篇幅过长就不一一详列,(全部源码)
到这,关于商品的增删改查所有操作算是实现了
实现的页面如图所示: