#开发技术
#前端
bootstarp框架 html页面
#后端技术
SpringBoot SpringMvc jpa
#开发工具
eclipse或者idea
jdk1.8 mysql5点几版本
maven环境 maven3.5+
前端地址::http://localhost:8080/mall
后台地址:http://localhost:8080/mall/admin/toLogin.html
package com.mall.web.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mall.entity.Classification;
import com.mall.entity.OrderItem;
import com.mall.entity.Product;
import com.mall.entity.pojo.ResultBean;
import com.mall.service.ClassificationService;
import com.mall.service.ProductService;
import com.mall.service.ShopCartService;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@Autowired
private ClassificationService classificationService;
@Autowired
private ShopCartService shopCartService;
/**
* 关键字搜索
*
* @return
*/
@RequestMapping("/serchPro.html")
public String serchPro(String serchname,HttpServletRequest request) {
request.getSession().setAttribute("serchname",serchname);
return "mall/product/product";
}
@ResponseBody
@RequestMapping("/serchPro.do")
public ResultBean> getSerchPro(HttpServletRequest request) {
String serchname = (String) request.getSession().getAttribute("serchname");
List products = new ArrayList<>();
List list = productService.findAll();
for (Product product:list) {
if(product.getTitle().contains(serchname)){
products.add(product);
}
}
return new ResultBean<>(products);
}
/**
* 获取商品信息
*
* @param id
* @return
*/
@RequestMapping("/get.do")
public ResultBean getProduct(int id) {
Product product = productService.findById(id);
return new ResultBean<>(product);
}
/**
* 打开商品详情页面
*
* @param id
* @param map
* @return
*/
@RequestMapping("/get.html")
public String toProductPage(int id, Map map) {
Product product = productService.findById(id);
map.put("product", product);
return "mall/product/info";
}
/**
* 查找热门商品
*
* @return
*/
@ResponseBody
@RequestMapping("/hot.do")
public ResultBean> getHotProduct() {
List products = productService.findHotProduct();
return new ResultBean<>(products);
}
/**
* 查找最新商品
*
* @param pageNo
* @param pageSize
* @return
*/
@ResponseBody
@RequestMapping("/new.do")
public ResultBean> getNewProduct(int pageNo, int pageSize) {
Pageable pageable = new PageRequest(pageNo, pageSize);
List products = productService.findNewProduct(pageable);
return new ResultBean<>(products);
}
/**
* 打开分类查看商品页面
*
* @return
*/
@RequestMapping("/category.html")
public String toCatePage(int cid, Map map) {
Classification classification = classificationService.findById(cid);
map.put("category", classification);
return "mall/product/category";
}
@RequestMapping("/toCart.html")
public String toCart(){
return "mall/product/cart";
}
/**
* 按一级分类查找商品
*
* @param cid
* @param pageNo
* @param pageSize
* @return
*/
@ResponseBody
@RequestMapping("/category.do")
public ResultBean> getCategoryProduct(int cid, int pageNo, int pageSize) {
Pageable pageable = new PageRequest(pageNo, pageSize);
List products = productService.findByCid(cid, pageable);
return new ResultBean<>(products);
}
/**
* 按二级分类查找商品
*
* @param csId
* @param pageNo
* @param pageSize
* @return
*/
@ResponseBody
@RequestMapping("/categorySec.do")
public ResultBean> getCategorySecProduct(int csId, int pageNo, int pageSize) {
Pageable pageable = new PageRequest(pageNo, pageSize);
List products = productService.findByCsid(csId, pageable);
return new ResultBean<>(products);
}
/**
* 根据一级分类查询它所有的二级分类
* @param cid
* @return
*/
@ResponseBody
@RequestMapping("/getCategorySec.do")
public ResultBean> getCategorySec(int cid){
List list = classificationService.findByParentId(cid);
return new ResultBean<>(list);
}
/**
* 加购物车
*
* @param productId
* @param request
* @return
*/
@ResponseBody
@RequestMapping("/addCart.do")
public ResultBean addToCart(int productId, HttpServletRequest request) throws Exception {
shopCartService.addCart(productId, request);
return new ResultBean<>(true);
}
/**
* 移除购物车
*
* @param productId
* @param request
* @return
*/
@ResponseBody
@RequestMapping("/delCart.do")
public ResultBean delToCart(int productId, HttpServletRequest request) throws Exception {
shopCartService.remove(productId, request);
return new ResultBean<>(true);
}
@ResponseBody
@RequestMapping("/updateCartNum.do")
public ResultBean updateCartNum(int productId,int type,HttpServletRequest request) throws Exception {
if(type==1){
shopCartService.addCart1(productId, request);
}else{
shopCartService.remove1(productId,request);
}
return new ResultBean<>(true);
}
/**
* 查看购物车商品
* @param request
* @return
*/
@ResponseBody
@RequestMapping("/listCart.do")
public ResultBean> listCart(HttpServletRequest request) throws Exception {
List orderItems = shopCartService.listCart(request);
return new ResultBean<>(orderItems);
}
}
package com.mall.web.user;
import com.mall.entity.User;
import com.mall.entity.pojo.ResultBean;
import com.mall.service.UserService;
import com.mall.service.exception.LoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 修改用户信息
* @return
*/
@RequestMapping("/editpsw.html")
public String editpsw(HttpServletRequest request,Model model) {
User user =(User) request.getSession().getAttribute("user");
model.addAttribute("user", user);
return "mall/user/editpsw";
}
@RequestMapping("/edituser.do")
public void edituser(Integer userid,String username,
String password,
String name,
String phone,
String email,
String addr,
HttpServletResponse response) throws Exception {
User user = new User();
user.setUsername(username);
user.setPhone(phone);
user.setPassword(password);
user.setName(name);
user.setEmail(email);
user.setAddr(addr);
user.setId(userid);
userService.update(user);
response.sendRedirect("/mall/index.html");
}
/**
* 打开注册页面
*
* @return
*/
@RequestMapping("/toRegister.html")
public String toRegister() {
return "mall/user/register";
}
/**
* 打开登录页面
*
* @return
*/
@RequestMapping("/toLogin.html")
public String toLogin() {
return "mall/user/login";
}
/**
* 登录
*
* @param username
* @param password
*/
@RequestMapping("/login.do")
public void login(String username,
String password,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
User user = userService.checkLogin(username, password);
if (user != null) {
//登录成功 重定向到首页
request.getSession().setAttribute("user", user);
response.sendRedirect("/mall/index.html");
} else {
throw new LoginException("登录失败! 用户名或者密码错误");
}
}
/**
* 注册
*/
@RequestMapping("/register.do")
public void register(String username,
String password,
String name,
String phone,
String email,
String addr,
HttpServletResponse response) throws IOException {
User user = new User();
user.setUsername(username);
user.setPhone(phone);
user.setPassword(password);
user.setName(name);
user.setEmail(email);
user.setAddr(addr);
userService.create(user);
// 注册完成后重定向到登录页面
response.sendRedirect("/mall/user/toLogin.html");
}
/**
* 登出
*/
@RequestMapping("/logout.do")
public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.getSession().removeAttribute("user");
response.sendRedirect("/mall/index.html");
}
/**
* 验证用户名是否唯一
* @param username
* @return
*/
@ResponseBody
@RequestMapping("/checkUsername.do")
public ResultBean checkUsername(String username){
List users = userService.findByUsername(username);
if (users==null||users.isEmpty()){
return new ResultBean<>(true);
}
return new ResultBean<>(false);
}
/**
* 如发生错误 转发到这页面
*
* @param response
* @param request
* @return
*/
@RequestMapping("/error.html")
public String error(HttpServletResponse response, HttpServletRequest request) {
return "error";
}
}