Java实战之鲜花商城系统的实现

时间:2022-12-10

项目介绍

该项目为前后台项目,分为普通用户与管理员两种角色,前台普通用户登录,后台管理员登录;

管理员角色包含以下功能:

管理员登录,用户管理,鲜花类别管理,鲜花管理,订单管理并发货,留言管理,系统公告管理等功能。

用户角色包含以下功能:

用户首页,用户注册登录,查看鲜花详情,加入购物车,确认订单,查看我的订单,商城留言板,商城公告等功能。

环境需要

1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。

2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;

3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可

4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 

5.数据库:MySql 5.7版本;

6.是否Maven项目:否;

技术栈

1. 后端:Spring+SpringMVC+Mybatis

2. 前端:JSP+jQuery+Ajax

使用说明

1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;

2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;

3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;

4. 运行项目,输入localhost:8080/ 登录

用户账号/密码: user/123456

管理员账号/密码:admin/admin

效果图展示

核心代码

商品管理控制层

/**
 * 商品
 **/
@RestController
@RequestMapping("flower")
public class FlowerController {
    @Autowired
    FlowersService flowerService;
 
    @Autowired
    FlowersDao flowersDao;
 
    @RequestMapping("/test")
    R test() {
        R r = new R();
        return r;
    }
 
    //find
    @RequestMapping("/find")
    R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey, @RequestParam("searchType") String searchType) {
        R r = new R();
        List<Flower> flowers = flowerService.find(searchKey, searchType);
        if (flowers == null) {
            return r.setCode(2000);
        }
        return getResponse(flowers, page, Constant.SHOW_PAGE_SIZE, r);
    }
 
    // 管理员查询所有
    @RequestMapping("/findAll")
    R findAll(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
        R r = new R();
        List<Flower> flowers = flowerService.findAll(searchKey);
        if (flowers == null) {
            return r.setCode(2000);
        }
        return getResponse(flowers, page, Constant.PAGE_SIZE, r);
    }
 
    // 返回结果
    private R getResponse(List<Flower> flowers, int page, int pageSize, R r) {
        Map<String, Object> map = new HashMap<>();
        List<Flower> items = flowers.size() >= page * pageSize ?
                flowers.subList((page - 1) * pageSize, page * pageSize)
                : flowers.subList((page - 1) * pageSize, flowers.size());
        int len = flowers.size() % pageSize == 0 ? flowers.size() / pageSize
                : (flowers.size() / pageSize + 1);
        for (Flower item : items) {
            if (item.getImg_guid() == null) {
                item.setImg_guid(Constant.DEFAULT_IMG);
            }
            item.setImg_guid(Constant.IMG_USE_PATH + item.getImg_guid());
        }
        map.put("items", items);
        map.put("len", len);
        return r.setCode(2000).setData(map);
    }
 
    @RequestMapping("/create")
    R create(@RequestBody Flower flower) {
        R r = new R();
        int ans = flowerService.add(flower);
        if (ans == 1) {
            return r.setCode(2000).setMsg(HttpMsg.ADD_FLOWER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.ADD_FLOWER_FAILED);
    }
 
    @RequestMapping("/update")
    R update(@RequestBody Flower flower) {
        R r = new R();
        int ans = flowerService.update(flower);
        if (ans >= 0) {
            return r.setCode(2000).setMsg(HttpMsg.UPDATE_FLOWER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.UPDATE_FLOWER_FAILED);
    }
 
    @RequestMapping("/changeState")
    R changeState(@RequestBody Flower flower) {
        R r = new R();
        flowersDao.changeState(flower);
        String msg = flower.getState() == 1?HttpMsg.GOODS_UP_OK:HttpMsg.GOODS_DOWN_OK;
        return r.setCode(2000).setMsg(msg);
    }
 
    // 保存上传的图片
    @RequestMapping("/updateImg")
    R updateImg(@RequestParam("file") MultipartFile file) {
        R r = new R();
        // 只接收 jpg/png 图片
        String filename = file.getOriginalFilename();
        if (!filename.endsWith(".jpg") && !filename.endsWith(".png")){
            return r.setCode(4000).setMsg(HttpMsg.ERROR_FILE_TYPE);
        }
        String img_guid = UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
        try {
            savePic(file.getInputStream(), img_guid);
            return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK).setData(img_guid);
        } catch (IOException e) {
            return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED);
        }
    }
 
    @PutMapping("/updateImgGuid")
    R updateImgGuid(@RequestParam("guid") String guid, @RequestParam("id") int id) {
        R r = new R();
        int ans = flowerService.updateImg(guid, id);
        if (ans == 1) {
            return r.setCode(2000).setMsg(HttpMsg.UPDATE_PIC_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.UPDATE_PIC_FAILED);
    }
 
    @DeleteMapping("/delete")
    R delete(@RequestParam("id") int id) {
        R r = new R();
        int ans = flowerService.delete(id);
        if (ans == 1) {
            return r.setCode(2000).setMsg(HttpMsg.DELETE_FLOWER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.DELETE_FLOWER_FAILED);
    }
 
    private void savePic(InputStream inputStream, String fileName) {
        OutputStream os = null;
        try {
            String path = Constant.IMG_PATH;
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            os = new FileOutputStream(new File(path + fileName));
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕,关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

订单管理控制层

/**
 * 用户
 **/
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    UserDao userDao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    FlowersDao flowersDao;
 
    @RequestMapping("/test")
    R test() {
        R r = new R();
        return r.setCode(4000).setMsg(Constant.IMG_PATH).setData(orderDao.findAll(null));
    }
 
    @RequestMapping("/queryByAccount")
    R queryByAccount(@RequestParam("account") String account) {
        R r = new R();
        if (StringUtil.isEmpty(account)) {
            return r.setCode(4000).setMsg(HttpMsg.INVALID_PARAM);
        }
        List<Order> orders = orderService.queryByAccount(account);
        return r.setCode(2000).setData(orders);
    }
 
    @RequestMapping("/find")
    R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey, @RequestParam("account") String account) {
        R r = new R();
        Map<String, Object> map = new HashMap<>();
        List<Order> orders = orderService.find(searchKey, account);
        if (orders == null) {
            return r.setCode(2000);
        }
        map.put("items", orders);
        map.put("len", orders.size());
        return r.setCode(2000).setData(map);
    }
 
    @RequestMapping("/findAll")
    R findAll(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
        R r = new R();
        Map<String, Object> map = new HashMap<>();
        List<Order> orders = orderService.findAll(searchKey);
        if (orders == null) {
            return r.setCode(2000);
        }
        List<Order> items = orders.size() >= page * Constant.PAGE_SIZE ?
                orders.subList((page - 1) * Constant.PAGE_SIZE, page * Constant.PAGE_SIZE)
                : orders.subList((page - 1) * Constant.PAGE_SIZE, orders.size());
        int len = orders.size() % Constant.PAGE_SIZE == 0 ? orders.size() / Constant.PAGE_SIZE
                : (orders.size() / Constant.PAGE_SIZE + 1);
        List<OrderVo> vos = new ArrayList<>();
        for (Order item : items) {
            User user = userDao.queryById(item.getUid());
            OrderVo vo = new OrderVo();
            vo.setAddress(user.getAddress()).setPhone(user.getPhone()).setUsername(user.getName())
                    .setAmount(item.getAmount()).setFlower(item.getFlower()).setId(item.getId())
                    .setUid(item.getUid()).setOrder_guid(item.getOrder_guid()).setPrice(item.getPrice())
                    .setState(item.getState());
            vos.add(vo);
        }
        map.put("items", vos);
        map.put("len", len);
        return r.setCode(2000).setData(map);
    }
 
    @RequestMapping("/update")
    R update(@RequestBody Order order) {
        R r = new R();
        int ans = orderService.update(order);
        if (ans >= 0) {
            return r.setCode(2000).setMsg(HttpMsg.UPDATE_USER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.UPDATE_USER_FAILED);
    }
 
    @RequestMapping("/changeState")
    R changeState(@RequestBody Order order) {
        orderDao.changeState(order);
        return new R().setCode(2000).setMsg(HttpMsg.UPDATE_ORDER_OK);
    }
 
    @DeleteMapping("/delete")
    R delete(@RequestParam("id") int id) {
        R r = new R();
        int ans = orderService.delete(id);
        if (ans == 1) {
            return r.setCode(2000).setMsg(HttpMsg.DELETE_USER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.DELETE_USER_FAILED);
    }
}
 

用户管理控制层

/**
 * 用户
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserDao dao;
 
    @RequestMapping("/test")
    R test() {
        R r = new R();
        return r.setCode(4000).setMsg(Constant.IMG_PATH).setData(dao.findAll());
    }
 
    @RequestMapping("/queryInfoByAccount")
    R queryInfoByAccount(@RequestParam("account") String account) {
        R r = new R();
        if (StringUtil.isEmpty(account)){
            return r.setCode(4000).setMsg(HttpMsg.INVALID_PARAM);
        }
        User loginUser = userService.queryInfo(account);
        if (loginUser == null){
            return r.setCode(4000).setMsg(HttpMsg.INVALID_USER);
        }
        return r.setCode(2000).setData(loginUser);
    }
 
 
    @RequestMapping("/find")
    R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
        R r = new R();
        Map<String, Object> map = new HashMap<>();
        List<User> users = userService.find(searchKey);
        if (users == null) {
            return r.setCode(2000);
        }
        List<User> items = users.size() >= page * Constant.PAGE_SIZE ?
                users.subList((page - 1) * Constant.PAGE_SIZE, page * Constant.PAGE_SIZE)
                : users.subList((page - 1) * Constant.PAGE_SIZE, users.size());
        int len = users.size() % Constant.PAGE_SIZE == 0 ? users.size() / Constant.PAGE_SIZE
                : (users.size() / Constant.PAGE_SIZE + 1);
        map.put("items", items);
        map.put("len", len);
        return r.setCode(2000).setData(map);
    }
 
 
    @RequestMapping("/create")
    R create(@RequestBody User user) {
        R r = new R();
        int ans = userService.add(user);
        if (ans == 1) {
            return r.setCode(2000).setMsg(HttpMsg.ADD_USER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.ADD_USER_FAILED);
    }
 
    @RequestMapping("/update")
    R update(@RequestBody User user) {
        R r = new R();
        int ans = userService.update(user);
        if (ans >= 0) {
            return r.setCode(2000).setMsg(HttpMsg.UPDATE_USER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.UPDATE_USER_FAILED);
    }
 
 
    @DeleteMapping("/delete")
    R delete(@RequestParam("id") int id) {
        R r = new R();
        int ans = userService.delete(id);
        if (ans == 1) {
            return r.setCode(2000).setMsg(HttpMsg.DELETE_USER_OK);
        }
        return r.setCode(4000).setMsg(HttpMsg.DELETE_USER_FAILED);
    }
}
 

以上就是Java实战之鲜花商城系统的实现的详细内容,更多关于Java鲜花商城系统的资料请关注html5模板网其它相关文章!

上一篇:Spring多线程的使用以及问题详解 下一篇:详解Java中List的正确的删除方法

相关文章

最新文章