<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.business</groupId> <artifactId>SpringBootMyBusiness</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <!-- 声明项目配置依赖编码格式为 utf-8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <fastjson.version>1.2.24</fastjson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 添加MySQL依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.13</version><!--$NO-MVN-MAN-VER$ --> </dependency> <!-- MyBatis-Spring,Spring Boot应用整合MyBatis框架的核心依赖配置 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <!-- 由于commons-fileupload组件不属于Spring Boot,所以需要加上版本 --> <version>1.3.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
server.servlet.context-path=/eBusiness spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?serverTimezone=UTC&autoReconnect=true spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database=MYSQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jackson.serialization.indent-output=true mybatis.type-aliases-package=com.ch.ebusiness.entity mybatis.mapperLocations=classpath:mappers/*.xml logging.level.com.ch.ebusiness.repository=debug spring.thymeleaf.cache=false spring.servlet.multipart.max-file-size=50MB spring.servlet.multipart.max-request-size=500MB
package com.ch.ebusiness.entity; public class AUser { private String aname; private String apwd; public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } public String getApwd() { return apwd; } public void setApwd(String apwd) { this.apwd = apwd; } }
package com.ch.ebusiness.entity; import javax.validation.constraints.Email; import javax.validation.constraints.NotBlank; import org.hibernate.validator.constraints.Length; public class BUser { private Integer id; @NotBlank(message = "邮箱必须输入!") @Email(message = "邮箱格式不正确!") private String bemail; @NotBlank(message = "密码必须输入!") @Length(min = 6, max = 20, message = "密码长度在6到20之间!") private String bpwd; private String rebpwd; private String code; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBemail() { return bemail; } public void setBemail(String bemail) { this.bemail = bemail; } public String getBpwd() { return bpwd; } public void setBpwd(String bpwd) { this.bpwd = bpwd; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getRebpwd() { return rebpwd; } public void setRebpwd(String rebpwd) { this.rebpwd = rebpwd; } }
package com.ch.ebusiness.entity; import org.springframework.web.multipart.MultipartFile; public class Goods { private int id; private String gname; private double goprice; private double grprice; private int gstore; private String gpicture; private MultipartFile fileName; private int goodstype_id; private String typename; private int buyNumber;// 加入购物车使用 private int isAdvertisement; private int isRecommend; public int getId() { return id; } public void setId(int 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 int getGstore() { return gstore; } public void setGstore(int gstore) { this.gstore = gstore; } public String getGpicture() { return gpicture; } public void setGpicture(String gpicture) { this.gpicture = gpicture; } public int getGoodstype_id() { return goodstype_id; } public void setGoodstype_id(int goodstype_id) { this.goodstype_id = goodstype_id; } public String getTypename() { return typename; } public void setTypename(String typename) { this.typename = typename; } public int getBuyNumber() { return buyNumber; } public void setBuyNumber(int buyNumber) { this.buyNumber = buyNumber; } public int getIsAdvertisement() { return isAdvertisement; } public void setIsAdvertisement(int isAdvertisement) { this.isAdvertisement = isAdvertisement; } public int getIsRecommend() { return isRecommend; } public void setIsRecommend(int isRecommend) { this.isRecommend = isRecommend; } public MultipartFile getFileName() { return fileName; } public void setFileName(MultipartFile fileName) { this.fileName = fileName; } }
package com.ch.ebusiness.entity; public class GoodsType { private int id; private String typename; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTypename() { return typename; } public void setTypename(String typename) { this.typename = typename; } }
package com.ch.ebusiness.entity; public class Order { private Integer id; private Integer busertable_id; private Double amount; private Integer status; private String orderdate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getBusertable_id() { return busertable_id; } public void setBusertable_id(Integer busertable_id) { this.busertable_id = busertable_id; } public Double getAmount() { return amount; } public void setAmount(Double amount) { this.amount = amount; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getOrderdate() { return orderdate; } public void setOrderdate(String orderdate) { this.orderdate = orderdate; } }
package com.ch.ebusiness.repository.admin; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.ch.ebusiness.entity.AUser; @Mapper public interface AdminRepository { List<AUser> login(AUser aUser); }
package com.ch.ebusiness.repository.admin; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.entity.GoodsType; @Mapper public interface GoodsRepository { int addGoods(Goods goods); int updateGoods(Goods goods); Goods selectAGoods(Integer id); int selectAllGoods(); List<GoodsType> selectAllGoodsType(); List<Goods> selectAllGoodsByPage(@Param("startIndex") int startIndex, @Param("perPageSize") int perPageSize); int deleteAGoods(Integer id); List<Map<String, Object>> selectFocusGoods(Integer id); List<Map<String, Object>> selectCartGoods(Integer id); List<Map<String, Object>> selectOrderGoods(Integer id); }
package com.ch.ebusiness.repository.admin; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.entity.GoodsType; @Mapper public interface TypeRepository { int selectAll(); List<GoodsType> selectAllTypeByPage(@Param("startIndex") int startIndex, @Param("perPageSize") int perPageSize); int deleteType(int id); List<Goods> selectGoods(int goodstype_id); int addType(GoodsType goodsType); }
package com.ch.ebusiness.repository.admin; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.ch.ebusiness.entity.BUser; @Mapper public interface UserAndOrderAndOutRepository { List<BUser> selectUserByPage(@Param("startIndex") int startIndex, @Param("perPageSize") int perPageSize); int selectAllUser(); List<Map<String, Object>> selectCartUser(int id); List<Map<String, Object>> selectOrderUser(int id); int deleteUser(int id); int selectAllOrder(); List<Map<String, Object>> selectOrderByPage(@Param("startIndex") int startIndex, @Param("perPageSize") int perPageSize); }
package com.ch.ebusiness.repository.before; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.ch.ebusiness.entity.Order; @Mapper public interface CartRepository { public List<Map<String, Object>> isFocus(@Param("uid") Integer uid, @Param("gid") Integer gid); public int focus(@Param("uid") Integer uid, @Param("gid") Integer gid); public int putCart(@Param("uid") Integer uid, @Param("gid") Integer gid, @Param("bnum") Integer bnum); public List<Map<String, Object>> isPutCart(@Param("uid") Integer uid, @Param("gid") Integer gid); public int updateCart(@Param("uid") Integer uid, @Param("gid") Integer gid, @Param("bnum") Integer bnum); public List<Map<String, Object>> selectCart(Integer uid); public int deleteAgoods(@Param("uid") Integer uid, @Param("gid") Integer gid); public int clear(Integer uid); public int addOrder(Order order); public int addOrderDetail(@Param("ordersn") Integer ordersn, @Param("uid") Integer uid); public List<Map<String, Object>> selectGoodsShop(Integer uid); public int updateStore(Map<String, Object> map); public int pay(Integer ordersn); public List<Map<String, Object>> myFocus(Integer uid); public List<Map<String, Object>> myOrder(Integer uid); public List<Map<String, Object>> orderDetail(Integer id); public int updateUpwd(@Param("uid") Integer uid, @Param("bpwd") String bpwd); }
package com.ch.ebusiness.repository.before; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.entity.GoodsType; @Mapper public interface IndexRepository { public List<Goods> selectAdvertisementGoods(); public List<GoodsType> selectGoodsType(); public List<Goods> selectRecommendGoods(Integer tid); public List<Goods> selectLastedGoods(Integer tid); public Goods selectAGoods(Integer id); public List<Goods> search(String mykey); }
package com.ch.ebusiness.repository.before; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.ch.ebusiness.entity.BUser; @Mapper public interface UserRepository { public List<BUser> isUse(BUser bUser); public int register(BUser bUser); public List<BUser> login(BUser bUser); }
package com.ch.ebusiness.service.admin; import javax.servlet.http.HttpSession; import org.springframework.ui.Model; import com.ch.ebusiness.entity.AUser; public interface AdminService { public String login(AUser aUser, HttpSession session, Model model); }
package com.ch.ebusiness.service.admin; import java.util.List; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import com.ch.ebusiness.entity.AUser; import com.ch.ebusiness.repository.admin.AdminRepository; @Service public class AdminServiceImpl implements AdminService { @Autowired private AdminRepository adminRepository; @Override public String login(AUser aUser, HttpSession session, Model model) { List<AUser> list = adminRepository.login(aUser); if (list.size() > 0) {// 登录成功 session.setAttribute("auser", aUser); return "forward:/goods/selectAllGoodsByPage?currentPage=1&act=select"; } else {// 登录失败 model.addAttribute("errorMessage", "用户名或密码错误!"); return "admin/login"; } } }
package com.ch.ebusiness.service.admin; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.springframework.ui.Model; import com.ch.ebusiness.entity.Goods; public interface GoodsService { public String selectAllGoodsByPage(Model model, int currentPage, String act); public String addGoods(Goods goods, HttpServletRequest request, String act) throws IllegalStateException, IOException; public String toAddGoods(Goods goods, Model model); public String detail(Model model, Integer id, String act); public String delete(Integer id); }
package com.ch.ebusiness.util; import java.security.MessageDigest; public class MD5Util { /*** * MD5加码生成32位md5码 */ public static String string2MD5(String inStr) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); return ""; } char[] charArray = inStr.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) byteArray[i] = (byte) charArray[i]; byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) hexValue.append("0"); hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } /*** * 自己规则加密 * * @param inStr * @return */ public static String MD5(String inStr) { String xy = "abc"; String finalStr = ""; if (inStr != null) { String fStr = inStr.substring(0, 1); String lStr = inStr.substring(1, inStr.length()); finalStr = string2MD5(fStr + xy + lStr); } else { finalStr = string2MD5(xy); } return finalStr; } }
package com.ch.ebusiness.util; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpSession; import com.ch.ebusiness.entity.BUser; public class MyUtil { /** * 将实际的文件名重命名 */ public static String getNewFileName(String oldFileName) { int lastIndex = oldFileName.lastIndexOf("."); String fileType = oldFileName.substring(lastIndex); Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDDHHmmssSSS"); String time = sdf.format(now); String newFileName = time + fileType; return newFileName; } /** * 获得用户信息 */ public static BUser getUser(HttpSession session) { BUser bUser = (BUser) session.getAttribute("bUser"); return bUser; } }
package com.ch.ebusiness.service.admin; import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import org.springframework.web.multipart.MultipartFile; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.repository.admin.GoodsRepository; import com.ch.ebusiness.util.MyUtil; @Service public class GoodsServiceImpl implements GoodsService { @Autowired private GoodsRepository goodsRepository; @Override public String selectAllGoodsByPage(Model model, int currentPage, String act) { // 共多少个商品 int totalCount = goodsRepository.selectAllGoods(); // 计算共多少页 int pageSize = 5; int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize); List<Goods> typeByPage = goodsRepository.selectAllGoodsByPage((currentPage - 1) * pageSize, pageSize); model.addAttribute("allGoods", typeByPage); model.addAttribute("totalPage", totalPage); model.addAttribute("currentPage", currentPage); model.addAttribute("act", act); return "admin/selectGoods"; } @Override public String addGoods(Goods goods, HttpServletRequest request, String act) throws IllegalStateException, IOException { MultipartFile myfile = goods.getFileName(); // 如果选择了上传文件,将文件上传到指定的目录images if (!myfile.isEmpty()) { // 上传文件路径(生产环境) // String path = request.getServletContext().getRealPath("/images/"); // 获得上传文件原名 // 上传文件路径(开发环境) String path = "C:\\workspace-spring-tool-suite-4-4.1.1.RELEASE\\eBusiness\\src\\main\\resources\\static\\images"; // 获得上传文件原名 String fileName = myfile.getOriginalFilename(); // 对文件重命名 String fileNewName = MyUtil.getNewFileName(fileName); File filePath = new File(path + File.separator + fileNewName); // 如果文件目录不存在,创建目录 if (!filePath.getParentFile().exists()) { filePath.getParentFile().mkdirs(); } // 将上传文件保存到一个目标文件中 myfile.transferTo(filePath); // 将重命名后的图片名存到goods对象中,添加时使用 goods.setGpicture(fileNewName); } if ("add".equals(act)) { int n = goodsRepository.addGoods(goods); if (n > 0)// 成功 return "redirect:/goods/selectAllGoodsByPage?currentPage=1&act=select"; // 失败 return "admin/addGoods"; } else {// 修改 int n = goodsRepository.updateGoods(goods); if (n > 0)// 成功 return "redirect:/goods/selectAllGoodsByPage?currentPage=1&act=updateSelect"; // 失败 return "admin/UpdateAGoods"; } } @Override public String toAddGoods(Goods goods, Model model) { model.addAttribute("goodsType", goodsRepository.selectAllGoodsType()); return "admin/addGoods"; } @Override public String detail(Model model, Integer id, String act) { model.addAttribute("goods", goodsRepository.selectAGoods(id)); if ("detail".equals(act)) return "admin/detail"; else { model.addAttribute("goodsType", goodsRepository.selectAllGoodsType()); return "admin/updateAGoods"; } } @Override public String delete(Integer id) { if (goodsRepository.selectCartGoods(id).size() > 0 || goodsRepository.selectFocusGoods(id).size() > 0 || goodsRepository.selectOrderGoods(id).size() > 0) return "no"; else { goodsRepository.deleteAGoods(id); return "/goods/selectAllGoodsByPage?currentPage=1&act=deleteSelect"; } } }
package com.ch.ebusiness.util; import java.security.MessageDigest; public class MD5Util { /*** * MD5加码生成32位md5码 */ public static String string2MD5(String inStr) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); return ""; } char[] charArray = inStr.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) byteArray[i] = (byte) charArray[i]; byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) hexValue.append("0"); hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } /*** * 自己规则加密 * * @param inStr * @return */ public static String MD5(String inStr) { String xy = "abc"; String finalStr = ""; if (inStr != null) { String fStr = inStr.substring(0, 1); String lStr = inStr.substring(1, inStr.length()); finalStr = string2MD5(fStr + xy + lStr); } else { finalStr = string2MD5(xy); } return finalStr; } }
package com.ch.ebusiness.util; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpSession; import com.ch.ebusiness.entity.BUser; public class MyUtil { /** * 将实际的文件名重命名 */ public static String getNewFileName(String oldFileName) { int lastIndex = oldFileName.lastIndexOf("."); String fileType = oldFileName.substring(lastIndex); Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMDDHHmmssSSS"); String time = sdf.format(now); String newFileName = time + fileType; return newFileName; } /** * 获得用户信息 */ public static BUser getUser(HttpSession session) { BUser bUser = (BUser) session.getAttribute("bUser"); return bUser; } }
package com.ch.ebusiness.service.admin; import org.springframework.ui.Model; import com.ch.ebusiness.entity.GoodsType; public interface TypeService { public String selectAllTypeByPage(Model model, int currentPage); public String delete(int id); public String addType(GoodsType goodsType); }
package com.ch.ebusiness.service.admin; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.entity.GoodsType; import com.ch.ebusiness.repository.admin.TypeRepository; @Service public class TypeServiceImpl implements TypeService { @Autowired private TypeRepository typeRepository; @Override public String selectAllTypeByPage(Model model, int currentPage) { // 共多少个类型 int totalCount = typeRepository.selectAll(); // 计算共多少页 int pageSize = 2; int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize); List<GoodsType> typeByPage = typeRepository.selectAllTypeByPage((currentPage - 1) * pageSize, pageSize); model.addAttribute("allTypes", typeByPage); model.addAttribute("totalPage", totalPage); model.addAttribute("currentPage", currentPage); return "admin/selectGoodsType"; } @Override public String delete(int id) { List<Goods> list = typeRepository.selectGoods(id); if (list.size() > 0) { // 该类型下有商品不允许删除 return "no"; } else { typeRepository.deleteType(id); // 删除后回到查询页面 return "/type/selectAllTypeByPage?currentPage=1"; } } @Override public String addType(GoodsType goodsType) { typeRepository.addType(goodsType); return "redirect:/type/selectAllTypeByPage?currentPage=1"; } }
package com.ch.ebusiness.service.admin; import org.springframework.ui.Model; public interface UserAndOrderAndOutService { public String selectUser(Model model, int currentPage); public String deleteUser(Model model, int id); public String selectOrder(Model model, int currentPage); }
package com.ch.ebusiness.service.before; import javax.servlet.http.HttpSession; import org.springframework.ui.Model; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.entity.Order; public interface CartService { public String putCart(Goods goods, Model model, HttpSession session); public String focus(Model model, HttpSession session, Integer gid); public String selectCart(Model model, HttpSession session, String act); public String deleteCart(HttpSession session, Integer gid); public String clearCart(HttpSession session); public String submitOrder(Order order, Model model, HttpSession session); public String pay(Order order); public String myFocus(Model model, HttpSession session); public String myOder(Model model, HttpSession session); public String orderDetail(Model model, Integer id); public String updateUpwd(HttpSession session, String bpwd); }
package com.ch.ebusiness.service.before; import java.util.List; import java.util.Map; import javax.servlet.http.HttpSession; 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.ch.ebusiness.entity.Goods; import com.ch.ebusiness.entity.Order; import com.ch.ebusiness.repository.before.CartRepository; import com.ch.ebusiness.repository.before.IndexRepository; import com.ch.ebusiness.util.MD5Util; import com.ch.ebusiness.util.MyUtil; @Service public class CartServiceImpl implements CartService { @Autowired private CartRepository cartRepository; @Autowired private IndexRepository indexRepository; @Override public String putCart(Goods goods, Model model, HttpSession session) { Integer uid = MyUtil.getUser(session).getId(); // 如果商品已在购物车,只更新购买数量 if (cartRepository.isPutCart(uid, goods.getId()).size() > 0) { cartRepository.updateCart(uid, goods.getId(), goods.getBuyNumber()); } else {// 新增到购物车 cartRepository.putCart(uid, goods.getId(), goods.getBuyNumber()); } // 跳转到查询购物车 return "forward:/cart/selectCart"; } @Override public String selectCart(Model model, HttpSession session, String act) { List<Map<String, Object>> list = cartRepository.selectCart(MyUtil.getUser(session).getId()); double sum = 0; for (Map<String, Object> map : list) { sum = sum + (Double) map.get("smallsum"); } model.addAttribute("total", sum); model.addAttribute("cartlist", list); // 广告区商品 model.addAttribute("advertisementGoods", indexRepository.selectAdvertisementGoods()); // 导航栏商品类型 model.addAttribute("goodsType", indexRepository.selectGoodsType()); if ("toCount".equals(act)) {// 去结算页面 return "user/count"; } return "user/cart"; } @Override public String focus(Model model, HttpSession session, Integer gid) { Integer uid = MyUtil.getUser(session).getId(); List<Map<String, Object>> list = cartRepository.isFocus(uid, gid); // 判断是否已收藏 if (list.size() > 0) { return "no"; } else { cartRepository.focus(uid, gid); return "ok"; } } @Override public String deleteCart(HttpSession session, Integer gid) { Integer uid = MyUtil.getUser(session).getId(); cartRepository.deleteAgoods(uid, gid); return "forward:/cart/selectCart"; } @Override public String clearCart(HttpSession session) { cartRepository.clear(MyUtil.getUser(session).getId()); return "forward:/cart/selectCart"; } @Override @Transactional public String submitOrder(Order order, Model model, HttpSession session) { order.setBusertable_id(MyUtil.getUser(session).getId()); // 生成订单 cartRepository.addOrder(order); // 生成订单详情 cartRepository.addOrderDetail(order.getId(), MyUtil.getUser(session).getId()); // 减少商品库存 List<Map<String, Object>> listGoods = cartRepository.selectGoodsShop(MyUtil.getUser(session).getId()); for (Map<String, Object> map : listGoods) { cartRepository.updateStore(map); } // 清空购物车 cartRepository.clear(MyUtil.getUser(session).getId()); model.addAttribute("order", order); return "user/pay"; } @Override public String pay(Order order) { cartRepository.pay(order.getId()); return "ok"; } @Override public String myFocus(Model model, HttpSession session) { // 广告区商品 model.addAttribute("advertisementGoods", indexRepository.selectAdvertisementGoods()); // 导航栏商品类型 model.addAttribute("goodsType", indexRepository.selectGoodsType()); model.addAttribute("myFocus", cartRepository.myFocus(MyUtil.getUser(session).getId())); return "user/myFocus"; } @Override public String myOder(Model model, HttpSession session) { // 广告区商品 model.addAttribute("advertisementGoods", indexRepository.selectAdvertisementGoods()); // 导航栏商品类型 model.addAttribute("goodsType", indexRepository.selectGoodsType()); model.addAttribute("myOrder", cartRepository.myOrder(MyUtil.getUser(session).getId())); return "user/myOrder"; } @Override public String orderDetail(Model model, Integer id) { model.addAttribute("orderDetail", cartRepository.orderDetail(id)); return "user/orderDetail"; } @Override public String updateUpwd(HttpSession session, String bpwd) { Integer uid = MyUtil.getUser(session).getId(); cartRepository.updateUpwd(uid, MD5Util.MD5(bpwd)); return "forward:/user/toLogin"; } }
package com.ch.ebusiness.service.before; import org.springframework.ui.Model; public interface IndexService { public String index(Model model, Integer tid); public String goodsDetail(Model model, Integer id); public String search(Model model, String mykey); }
package com.ch.ebusiness.service.before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import com.ch.ebusiness.repository.before.IndexRepository; @Service public class IndexServiceImpl implements IndexService { @Autowired private IndexRepository indexRepository; @Override public String index(Model model, Integer tid) { if (tid == null) tid = 0; // 广告区商品 model.addAttribute("advertisementGoods", indexRepository.selectAdvertisementGoods()); // 导航栏商品类型 model.addAttribute("goodsType", indexRepository.selectGoodsType()); // 推荐商品 model.addAttribute("recommendGoods", indexRepository.selectRecommendGoods(tid)); // 最新商品 model.addAttribute("lastedGoods", indexRepository.selectLastedGoods(tid)); return "user/index"; } @Override public String goodsDetail(Model model, Integer id) { // 广告区商品 model.addAttribute("advertisementGoods", indexRepository.selectAdvertisementGoods()); // 导航栏商品类型 model.addAttribute("goodsType", indexRepository.selectGoodsType()); // 商品详情 model.addAttribute("goods", indexRepository.selectAGoods(id)); return "user/goodsDetail"; } @Override public String search(Model model, String mykey) { // 广告区商品 model.addAttribute("advertisementGoods", indexRepository.selectAdvertisementGoods()); // 导航栏商品类型 model.addAttribute("goodsType", indexRepository.selectGoodsType()); // 商品搜索 model.addAttribute("searchgoods", indexRepository.search(mykey)); return "user/searchResult"; } }
package com.ch.ebusiness.service.before; import javax.servlet.http.HttpSession; import org.springframework.ui.Model; import com.ch.ebusiness.entity.BUser; public interface UserService { public String isUse(BUser bUser); public String register(BUser bUser); public String login(BUser bUser, HttpSession session, Model model); }
package com.ch.ebusiness.service.before; import java.util.List; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import com.ch.ebusiness.entity.BUser; import com.ch.ebusiness.repository.before.UserRepository; import com.ch.ebusiness.util.MD5Util; @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public String isUse(BUser bUser) { if (userRepository.isUse(bUser).size() > 0) { return "no"; } return "ok"; } @Override public String register(BUser bUser) { // 对密码MD5加密 bUser.setBpwd(MD5Util.MD5(bUser.getBpwd())); if (userRepository.register(bUser) > 0) { return "user/login"; } return "user/register"; } @Override public String login(BUser bUser, HttpSession session, Model model) { // 对密码MD5加密 bUser.setBpwd(MD5Util.MD5(bUser.getBpwd())); String rand = (String) session.getAttribute("rand"); if (!rand.equalsIgnoreCase(bUser.getCode())) { model.addAttribute("errorMessage", "验证码错误!"); return "user/login"; } List<BUser> list = userRepository.login(bUser); if (list.size() > 0) { session.setAttribute("bUser", list.get(0)); return "redirect:/";// 到首页 } model.addAttribute("errorMessage", "用户名或密码错误!"); return "user/login"; } }
package com.ch.ebusiness.controller.admin; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import com.ch.ebusiness.NoLoginException; @Controller public class AdminBaseController { /** * 登录权限控制,处理方法执行前执行该方法 */ @ModelAttribute public void isLogin(HttpSession session) throws NoLoginException { if(session.getAttribute("auser") == null){ throw new NoLoginException("没有登录"); } } }
package com.ch.ebusiness.controller.admin; import javax.servlet.http.HttpSession; 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.ch.ebusiness.entity.AUser; import com.ch.ebusiness.service.admin.AdminService; @Controller @RequestMapping("/admin") public class AdminController { @Autowired private AdminService adminService; @RequestMapping("/toLogin") public String toLogin(@ModelAttribute("aUser") AUser aUser) { return "admin/login"; } @RequestMapping("/login") public String login(@ModelAttribute("aUser") AUser aUser, HttpSession session, Model model) { return adminService.login(aUser, session, model); } }
package com.ch.ebusiness.controller.admin; import java.io.IOException; 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 org.springframework.web.bind.annotation.ResponseBody; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.service.admin.GoodsService; @Controller @RequestMapping("/goods") public class GoodsController extends AdminBaseController { @Autowired private GoodsService goodsService; @RequestMapping("/selectAllGoodsByPage") public String selectAllGoodsByPage(Model model, int currentPage, String act) { return goodsService.selectAllGoodsByPage(model, currentPage, act); } @RequestMapping("/toAddGoods") public String toAddGoods(@ModelAttribute("goods") Goods goods, Model model) { goods.setIsAdvertisement(0); goods.setIsRecommend(1); return goodsService.toAddGoods(goods, model); } @RequestMapping("/addGoods") public String addGoods(@ModelAttribute("goods") Goods goods, HttpServletRequest request, String act) throws IllegalStateException, IOException { return goodsService.addGoods(goods, request, act); } @RequestMapping("/detail") public String detail(Model model, Integer id, String act) { return goodsService.detail(model, id, act); } @RequestMapping("/delete") @ResponseBody public String delete(Integer id) { return goodsService.delete(id); } }
package com.ch.ebusiness.controller.admin; 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 org.springframework.web.bind.annotation.ResponseBody; import com.ch.ebusiness.entity.GoodsType; import com.ch.ebusiness.service.admin.TypeService; @Controller @RequestMapping("/type") public class TypeController extends AdminBaseController { @Autowired private TypeService typeService; @RequestMapping("/selectAllTypeByPage") public String selectAllTypeByPage(Model model, int currentPage) { return typeService.selectAllTypeByPage(model, currentPage); } @RequestMapping("/deleteType") @ResponseBody // 返回字符串数据而不是视图 public String delete(int id) { return typeService.delete(id); } @RequestMapping("/toAddType") public String toAddType(@ModelAttribute("goodsType") GoodsType goodsType) { return "admin/addType"; } @RequestMapping("/addType") public String addType(@ModelAttribute("goodsType") GoodsType goodsType) { return typeService.addType(goodsType); } }
package com.ch.ebusiness.controller.admin; import javax.servlet.http.HttpSession; 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 org.springframework.web.bind.annotation.ResponseBody; import com.ch.ebusiness.entity.AUser; import com.ch.ebusiness.service.admin.UserAndOrderAndOutService; @Controller public class UserAndOrderAndOutController extends AdminBaseController { @Autowired private UserAndOrderAndOutService userAndOrderAndOutService; @RequestMapping("/loginOut") public String loginOut(@ModelAttribute("aUser") AUser aUser, HttpSession session) { session.invalidate(); return "admin/login"; } @RequestMapping("/selectUser") public String selectUser(Model model, int currentPage) { return userAndOrderAndOutService.selectUser(model, currentPage); } @RequestMapping("/deleteUser") @ResponseBody public String deleteUser(Model model, int id) { return userAndOrderAndOutService.deleteUser(model, id); } @RequestMapping("/selectOrder") public String selectOrder(Model model, int currentPage) { return userAndOrderAndOutService.selectOrder(model, currentPage); } }
package com.ch.ebusiness; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //配置扫描MyBatis接口的包路径 @MapperScan(basePackages = { "com.ch.ebusiness.repository" }) public class EBusinessApplication { public static void main(String[] args) { SpringApplication.run(EBusinessApplication.class, args); } }
package com.ch.ebusiness; import java.sql.SQLException; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; /** * 统一异常处理 */ @ControllerAdvice public class GlobalExceptionHandleController { @ExceptionHandler(value = Exception.class) public String exceptionHandler(Exception e, Model model) { String message = ""; // 数据库异常 if (e instanceof SQLException) { message = "数据库异常"; } else if (e instanceof NoLoginException) { message = "未登录异常"; } else {// 未知异常 message = "未知异常"; } model.addAttribute("mymessage", message); return "myError"; } }
package com.ch.ebusiness.controller.before; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import com.ch.ebusiness.NoLoginException; @Controller public class BeforeBaseController { /** * 登录权限控制,处理方法执行前执行该方法 */ @ModelAttribute public void isLogin(HttpSession session) throws NoLoginException { if (session.getAttribute("bUser") == null) { throw new NoLoginException("没有登录"); } } }
package com.ch.ebusiness.controller.before; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.ch.ebusiness.entity.Goods; import com.ch.ebusiness.entity.Order; import com.ch.ebusiness.service.before.CartService; @Controller @RequestMapping("/cart") public class CartController extends BeforeBaseController { @Autowired private CartService cartService; @RequestMapping("/putCart") public String putCart(Goods goods, Model model, HttpSession session) { return cartService.putCart(goods, model, session); } @RequestMapping("/focus") @ResponseBody public String focus(@RequestBody Goods goods, Model model, HttpSession session) { return cartService.focus(model, session, goods.getId()); } @RequestMapping("/selectCart") public String selectCart(Model model, HttpSession session, String act) { return cartService.selectCart(model, session, act); } @RequestMapping("/deleteCart") public String deleteCart(HttpSession session, Integer gid) { return cartService.deleteCart(session, gid); } @RequestMapping("/clearCart") public String clearCart(HttpSession session) { return cartService.clearCart(session); } @RequestMapping("/submitOrder") public String submitOrder(Order order, Model model, HttpSession session) { return cartService.submitOrder(order, model, session); } @RequestMapping("/pay") @ResponseBody public String pay(@RequestBody Order order) { return cartService.pay(order); } @RequestMapping("/myFocus") public String myFocus(Model model, HttpSession session) { return cartService.myFocus(model, session); } @RequestMapping("/myOder") public String myOder(Model model, HttpSession session) { return cartService.myOder(model, session); } @RequestMapping("/orderDetail") public String orderDetail(Model model, Integer id) { return cartService.orderDetail(model, id); } @RequestMapping("/userInfo") public String userInfo() { return "user/userInfo"; } @RequestMapping("/updateUpwd") public String updateUpwd(HttpSession session, String bpwd) { return cartService.updateUpwd(session, bpwd); } }
package com.ch.ebusiness.controller.before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.ch.ebusiness.service.before.IndexService; @Controller public class IndexController { @Autowired private IndexService indexService; @RequestMapping("/") public String index(Model model, Integer tid) { return indexService.index(model, tid); } @RequestMapping("/goodsDetail") public String goodsDetail(Model model, Integer id) { return indexService.goodsDetail(model, id); } @RequestMapping("/search") public String search(Model model, String mykey) { return indexService.search(model, mykey); } }
package com.ch.ebusiness.controller.before; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.ch.ebusiness.entity.BUser; import com.ch.ebusiness.service.before.UserService; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/toRegister") public String toRegister(@ModelAttribute("bUser") BUser bUser) { return "user/register"; } @RequestMapping("/toLogin") public String toLogin(@ModelAttribute("bUser") BUser bUser) { // @ModelAttribute("bUser")与th:object="${bUser}"相对应 return "user/login"; } @RequestMapping("/login") public String login(@ModelAttribute("bUser") @Validated BUser bUser, BindingResult rs, HttpSession session, Model model) { if (rs.hasErrors()) {// 验证失败 return "user/login"; } return userService.login(bUser, session, model); } @RequestMapping("/isUse") @ResponseBody public String isUse(@RequestBody BUser bUser) { return userService.isUse(bUser); } @RequestMapping("/register") public String register(@ModelAttribute("bUser") @Validated BUser bUser, BindingResult rs) { if (rs.hasErrors()) {// 验证失败 return "user/register"; } return userService.register(bUser); } }
package com.ch.ebusiness.controller.before; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class ValidateCodeController { private char code[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' }; private static final int WIDTH = 50; private static final int HEIGHT = 20; private static final int LENGTH = 4; @RequestMapping("/validateCode") public void validateCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // 设置响应报头信息 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // 设置响应的MIME类型 response.setContentType("image/jpeg"); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Font mFont = new Font("Arial", Font.TRUETYPE_FONT, 18); Graphics g = image.getGraphics(); Random rd = new Random(); // 设置背景颜色 g.setColor(new Color(rd.nextInt(55) + 200, rd.nextInt(55) + 200, rd.nextInt(55) + 200)); g.fillRect(0, 0, WIDTH, HEIGHT); // 设置字体 g.setFont(mFont); // 画边框 g.setColor(Color.black); g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1); // 随机产生的验证码 String result = ""; for (int i = 0; i < LENGTH; ++i) { result += code[rd.nextInt(code.length)]; } HttpSession se = request.getSession(); se.setAttribute("rand", result); // 画验证码 for (int i = 0; i < result.length(); i++) { g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd.nextInt(200))); g.drawString(result.charAt(i) + "", 12 * i + 1, 16); } // 随机产生2个干扰线 for (int i = 0; i < 2; i++) { g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd.nextInt(200))); int x1 = rd.nextInt(WIDTH); int x2 = rd.nextInt(WIDTH); int y1 = rd.nextInt(HEIGHT); int y2 = rd.nextInt(HEIGHT); g.drawLine(x1, y1, x2, y2); } // 释放图形资源 g.dispose(); try { OutputStream os = response.getOutputStream(); // 输出图像到页面 ImageIO.write(image, "JPEG", os); } catch (IOException e) { e.printStackTrace(); } } }