Skip to content

Commit

Permalink
用户模块、商品模块
Browse files Browse the repository at this point in the history
  • Loading branch information
yyzim committed Aug 20, 2022
1 parent 74b2394 commit 9363618
Show file tree
Hide file tree
Showing 40 changed files with 1,308 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .idea/artifacts/wdp1_server_war_exploded.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>


</dependencies>

Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/example/mall/controller/AdminServlet.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.example.mall.controller;

import com.example.mall.mapper.AdminMapper;
import com.example.mall.model.bo.AddAdminssBO;
import com.example.mall.model.bo.AdminLoginBO;
import com.example.mall.model.bo.SearchAdminsBO;
import com.example.mall.model.bo.UpdateAdminssBO;
import com.example.mall.model.bo.*;
import com.example.mall.model.vo.*;
import com.example.mall.service.AdminService;
import com.example.mall.service.AdminServiceImpl;
Expand All @@ -22,7 +19,7 @@

/**
* @Classname AdminServlet
* @Description
* @Description 负责管理员模块
* @Date 2022-08-19 16:10
* @Created by Yang Yi-zhou
*/
Expand All @@ -47,9 +44,20 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
addAdminss(req, resp);
} else if ("updateAdminss".equals(targetResource)) {
updateAdminss(req, resp);
} else if ("changePwd".equals(targetResource)) {
changePwd(req, resp);
}
}

private void changePwd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解析载荷到BO
ChangePwdBO changePwdBO = getBO(req, ChangePwdBO.class);
//把数据更新到数据库
ChangePwdVO changePwdVO = adminService.changePwdBO(changePwdBO);
//写回响应体
resp.getWriter().println(gson.toJson(changePwdVO));
}

private void updateAdminss(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解析载荷到BO
UpdateAdminssBO updateAdminssBO = getBO(req, UpdateAdminssBO.class);
Expand Down Expand Up @@ -163,7 +171,6 @@ private void login(HttpServletRequest req, HttpServletResponse resp) throws IOEx
* @return T对象
*/
private <T> T getBO(HttpServletRequest req, Class<T> clazz) throws IOException {
//获取请求中json参数封装到AdminLoginBO中
//获取请求体参数
ServletInputStream inputStream = req.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand Down
122 changes: 122 additions & 0 deletions src/main/java/com/example/mall/controller/GoodsServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.example.mall.controller;

import com.example.mall.model.bo.AddGoodsBO;
import com.example.mall.model.bo.AddTypeBO;
import com.example.mall.model.bo.UpdateGoodsBO;
import com.example.mall.model.vo.*;
import com.example.mall.service.GoodsService;
import com.example.mall.service.GoodsServiceImpl;
import com.example.mall.service.UserService;
import com.example.mall.service.UserServiceImpl;
import com.example.mall.util.ParseUtils;
import com.google.gson.Gson;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
* @Classname BusinessServlet
* @Description 负责后台业务模块
* @Date 2022-08-20 10:36
* @Created by Yang Yi-zhou
*/
@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {
private Gson gson = new Gson();
//goods业务层
private GoodsService goodsService = new GoodsServiceImpl();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//分发
String contextPath = req.getContextPath();
String servletPath = req.getServletPath();
String targetResource = req.getRequestURI().replace(contextPath + servletPath + "/", "");
if ("getType".equals(targetResource)) {
getType(req, resp);
} else if ("getGoodsByType".equals(targetResource)) {
getGoodsByType(req, resp);
}

}

private void getGoodsByType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//取出TypeId
int typeId = Integer.parseInt(req.getParameter("typeId"));
//根据typeId去数据库取出数据
GetGoodsByTypeVO goodsByType = goodsService.getGoodsByType(typeId);
//响应
resp.getWriter().println(gson.toJson(goodsByType));
}

private void getType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//获取商品类型
GetTypeVO typeVO = goodsService.getType();
//响应
resp.getWriter().println(gson.toJson(typeVO));

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//分发
String targetResource = ParseUtils.parseURIToTargetResource(req);
if ("addType".equals(targetResource)) {
addType(req, resp);
} else if ("imgUpload".equals(targetResource)) {
try {
imgUpload(req, resp);
} catch (FileUploadException e) {
throw new RuntimeException(e);
}
} else if ("addGoods".equals(targetResource)) {
addGoods(req, resp);
} else if ("updateGoods".equals(targetResource)) {
updateGoods(req, resp);
}

}

private void updateGoods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解析载荷
UpdateGoodsBO updateGoodsBO = ParseUtils.parseToBO(req, UpdateGoodsBO.class);
}

private void addGoods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解析载荷
AddGoodsBO addGoodsBO = ParseUtils.parseToBO(req, AddGoodsBO.class);
//交给service处理
AddGoodsVO addGoodsVO = goodsService.addGoods(addGoodsBO);
//响应
resp.getWriter().println(gson.toJson(addGoodsVO));
}

private void imgUpload(HttpServletRequest req, HttpServletResponse resp) throws IOException, FileUploadException {
ImgUploadVO imgUploadVO = goodsService.imgUpload(req, resp);
//响应
resp.getWriter().println(gson.toJson(imgUploadVO));
}

private void addType(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解析载荷
AddTypeBO addTypeBO = ParseUtils.parseToBO(req, AddTypeBO.class);
//添加进数据库
AddTypeVO addTypeVO = goodsService.addType(addTypeBO);
//响应
resp.getWriter().println(gson.toJson(addTypeVO));

}
}
49 changes: 49 additions & 0 deletions src/main/java/com/example/mall/controller/ImageServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.mall.controller;

import com.example.mall.util.ParseUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
* @Classname ImageServlet
* @Description
* @Date 2022-08-20 11:35
* @Created by Yang Yi-zhou
*/
@WebServlet("/static/image/*")
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("image/jpeg;charset=UTF-8");
// //拿到图片的名称
// String contextPath = req.getContextPath();
// String servletPath = req.getServletPath();
// String targetResource = req.getRequestURI().replace(contextPath + servletPath + "/", "");
String targetResource = ParseUtils.parseURIToTargetResource(req);
//拿到图片的真实路径
String path = this.getServletContext().getRealPath("/static/image/" + targetResource);
// String path = this.getServletContext().getRealPath("static/image/1.jpg");
//读取图片
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
int size = fis.available();
byte[] data = new byte[size];
fis.read(data);
fis.close();
//写入响应体
ServletOutputStream ops = resp.getOutputStream();
ops.write(data);
ops.flush();
ops.close();

}
}
72 changes: 72 additions & 0 deletions src/main/java/com/example/mall/controller/UserServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.mall.controller;

import com.example.mall.model.vo.AllUserVO;
import com.example.mall.model.vo.DeleteUserVO;
import com.example.mall.model.vo.SearchUserVO;
import com.example.mall.service.UserService;
import com.example.mall.service.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @Classname UserServlet
* @Description 负责用户模块
* @Date 2022-08-20 9:44
* @Created by Yang Yi-zhou
*/
@WebServlet("/api/admin/user/*")
public class UserServlet extends HttpServlet {
private Gson gson = new Gson();
private UserService userService = new UserServiceImpl();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//分发请求
String contextPath = req.getContextPath();
String servletPath = req.getServletPath();
String targetResource = req.getRequestURI().replace(contextPath + servletPath + "/", "");
if ("allUser".equals(targetResource)) {
allUser(req, resp);
} else if ("deleteUser".equals(targetResource)) {
deleteUser(req, resp);
} else if ("searchUser".equals(targetResource)) {
searchUser(req, resp);
}
}

private void searchUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解析参数
String word = req.getParameter("word");
//根据搜索词去搜索用户
SearchUserVO searchUserVO = userService.searchUser(word);
//响应
resp.getWriter().println(gson.toJson(searchUserVO));
}

private void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解析参数
int id = Integer.parseInt(req.getParameter("id"));
//根据id去数据库删除数据
DeleteUserVO deleteUserVO = userService.deleteUser(id);
//响应
resp.getWriter().println(gson.toJson(deleteUserVO));
}

private void allUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//交给service层取数据
AllUserVO allUserVO = userService.allUser();
//写入响应体返回
resp.getWriter().println(gson.toJson(allUserVO));
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/mall/mapper/AdminMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface AdminMapper {
AdminPO selectAdminFromAdminById(@Param("id") Integer id);

Integer updateAdminById(@Param("admin") AdminPO admin);

Integer updateAdminPasswordByNameAndPassword(@Param("name") String name, @Param("oldPassword") String oldPassword, @Param("newPassword") String newPassword);
}
34 changes: 34 additions & 0 deletions src/main/java/com/example/mall/mapper/GoodsMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.mall.mapper;

import com.example.mall.model.po.GoodsPO;
import com.example.mall.model.po.GoodsSpecPO;
import com.example.mall.model.po.GoodsTypePO;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* @Classname AdminMapper
* @Description
* @Date 2022-08-19 17:52
* @Created by Yang Yi-zhou
*/
public interface GoodsMapper {
List<GoodsTypePO> selectAllGoodsTypeFromTableGoodsType();

List<GoodsPO> selectGoodsFromTableGoodsByTypeId(@Param("typeId") Integer typeId);

Integer selectStockNumFromTableGoodsSpecByGoodsId(@Param("goodsId") Integer goodsId);

Integer insertIntoTableGoodsType(@Param("name") String name);

GoodsTypePO selectGoodsTypeFromTableGoodsTypeByName(@Param("name") String name);

Integer insertGoodsIntoTableGoods(@Param("goodsPO") GoodsPO goodsPO);

Integer insertGoodsSpecListIntoTableGoodsSpec(@Param("specPOList") List<GoodsSpecPO> specPOList);

Double selectPriceFromTableGoodsById(@Param("id") Integer id);

Integer updateTableGoodsSetPriceById(@Param("price") Double price, @Param("id") Integer id);
}
22 changes: 22 additions & 0 deletions src/main/java/com/example/mall/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.mall.mapper;

import com.example.mall.model.po.UserPO;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* @Classname AdminMapper
* @Description
* @Date 2022-08-19 17:52
* @Created by Yang Yi-zhou
*/
public interface UserMapper {

List<UserPO> selectUserListFromUser();

Integer deleteUserById(@Param("id") Integer id);

List<UserPO> selectUserFromUserByWord(@Param("word") String word);

}
Loading

0 comments on commit 9363618

Please sign in to comment.