SSM实现网上商城 有聊天功能

1.项目介绍

     实现一个网上商城,商品信息展示,购物车,订单管理,个人中心,商品评价,商品搜索,地址管理,聊天,后台管理(商品增删改查),分类管理,活动管理,客服聊天回复

2.开发环境

  • 开发环境:IDEA/eclipse、Tomcat8.5+
  • 数据库:MySql
  • 前端主要使用Bootstrap以及JQuery,后端基于SpringMVC、Spring、MyBatis进行开发,使用Maven构建
  • activeMQ

 

 

 

相关代码

 

@Controller
public class OrderController {

    /*@Value("#{addressService}")*/
    @Autowired
    private AddressService addressService;

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ActivityService activityService;

    @RequestMapping("/order")
    public String showOrder(HttpSession session, Model model) {

        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }

        //查询当前用户的收货地址
        AddressExample addressExample = new AddressExample();
        addressExample.or().andUseridEqualTo(user.getUserid());
        List<Address> addressList = addressService.getAllAddressByExample(addressExample);

        model.addAttribute("address", addressList);

        //订单信息
        //获取当前用户的购物车信息
        ShopCartExample shopCartExample = new ShopCartExample();
        shopCartExample.or().andUseridEqualTo(user.getUserid());
        List<ShopCart> shopCart = shopCartService.selectByExample(shopCartExample);

        //获取购物车中的商品信息
        List<Goods> goodsAndImage = new ArrayList<>();

        Float totalPrice = new Float(0);
        Integer oldTotalPrice = 0;

        for (ShopCart cart:shopCart) {
            Goods goods = goodsService.selectById(cart.getGoodsid());

            List<ImagePath> imagePathList = goodsService.findImagePath(goods.getGoodsid());
            goods.setImagePaths(imagePathList);
            goods.setNum(cart.getGoodsnum());

            //活动信息
            Activity activity = activityService.selectByKey(goods.getActivityid());
            goods.setActivity(activity);

            if(activity.getDiscount() != 1) {
                goods.setNewPrice(goods.getPrice()*goods.getNum()* activity.getDiscount());
            } else if(activity.getFullnum() != null) {
                if (goods.getNum() >= activity.getFullnum()) {
                    goods.setNewPrice((float) (goods.getPrice()*(goods.getNum()-activity.getReducenum())));
                } else {
                    goods.setNewPrice((float) (goods.getPrice()*goods.getNum()));
                }
            } else {
                goods.setNewPrice((float) (goods.getPrice()*goods.getNum()));
            }
            totalPrice = totalPrice + goods.getNewPrice();
            oldTotalPrice = oldTotalPrice + goods.getNum() * goods.getPrice();
            goodsAndImage.add(goods);
        }

        model.addAttribute("totalPrice", totalPrice);
        model.addAttribute("oldTotalPrice", oldTotalPrice);
        model.addAttribute("goodsAndImage", goodsAndImage);

        return "orderConfirm";
    }

    @RequestMapping("/orderFinish")
    @ResponseBody
    public Msg orderFinish(Float oldPrice, Float newPrice, Boolean isPay, Integer addressid,HttpSession session) {
        User user = (User) session.getAttribute("user");

        //获取订单信息
        ShopCartExample shopCartExample = new ShopCartExample();
        shopCartExample.or().andUseridEqualTo(user.getUserid());
        List<ShopCart> shopCart = shopCartService.selectByExample(shopCartExample);

        //删除购物车
        for (ShopCart cart : shopCart) {
            shopCartService.deleteByKey(new ShopCartKey(cart.getUserid(),cart.getGoodsid()));
        }

        //把订单信息写入数据库
        Order order = new Order(null, user.getUserid(), new Date(), oldPrice, newPrice, isPay, false, false, false, addressid,null,null);
        orderService.insertOrder(order);
        //插入的订单号
        Integer orderId = order.getOrderid();

        //把订单项写入orderitem表中
        for (ShopCart cart : shopCart) {
            orderService.insertOrderItem(new OrderItem(null, orderId, cart.getGoodsid(), cart.getGoodsnum()));
        }

        return Msg.success("购买成功");
    }

}
@Controller
public class MainController {

    @Autowired
    private CateService cateService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/main")
    public String showAllGoods(Model model, HttpSession session) {

        Integer userid;
        User user = (User) session.getAttribute("user");
        if (user == null) {
            userid = null;
        } else {
            userid = user.getUserid();
        }

        //数码分类
        List<Goods> digGoods = getCateGoods("数码", userid);
        model.addAttribute("digGoods", digGoods);

        //家电
        List<Goods> houseGoods = getCateGoods("家电", userid);
        model.addAttribute("houseGoods", houseGoods);

        //服饰
        List<Goods> colGoods = getCateGoods("服饰", userid);
        model.addAttribute("colGoods", colGoods);

        //书籍
        List<Goods> bookGoods = getCateGoods("书籍", userid);
        model.addAttribute("bookGoods", bookGoods);

        return "main";
    }

    public List<Goods> getCateGoods(String cate, Integer userid) {
        //查询分类
        CategoryExample digCategoryExample = new CategoryExample();
        digCategoryExample.or().andCatenameLike(cate);
        List<Category> digCategoryList = cateService.selectByExample(digCategoryExample);

        if (digCategoryList.size() == 0) {
            return null;
        }

        //查询属于刚查到的分类的商品
        GoodsExample digGoodsExample = new GoodsExample();
        List<Integer> digCateId = new ArrayList<Integer>();
        for (Category tmp:digCategoryList) {
            digCateId.add(tmp.getCateid());
        }
        digGoodsExample.or().andCategoryIn(digCateId);

        List<Goods> goodsList = goodsService.selectByExampleLimit(digGoodsExample);

        List<Goods> goodsAndImage = new ArrayList<>();
        //获取每个商品的图片
        for (Goods goods:goodsList) {
            //判断是否为登录状态
            if (userid == null) {
                goods.setFav(false);
            } else {
                Favorite favorite = goodsService.selectFavByKey(new FavoriteKey(userid, goods.getGoodsid()));
                if (favorite == null) {
                    goods.setFav(false);
                } else {
                    goods.setFav(true);
                }
            }

            List<ImagePath> imagePathList = goodsService.findImagePath(goods.getGoodsid());
            goods.setImagePaths(imagePathList);
            goodsAndImage.add(goods);
        }
        return goodsAndImage;
    }
}

 

 

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 20
    评论
以下是SSM(Spring+SpringMVC+MyBatis)集成jQuery实现聊天功能的代码示例: 首先是前端页面代码,使用jQuery实现聊天发送和接收: ```html <!-- 聊天记录 --> <div id="msg-box"></div> <!-- 发送消息 --> <div> <input type="text" id="msg" placeholder="请输入消息内容"> <button id="send-btn">发送</button> </div> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(function() { // 接收消息 setInterval(function() { $.ajax({ url: "/getMsg", type: "get", success: function(result) { console.log(result); $("#msg-box").append("<p>" + result + "</p>"); }, error: function() { console.log("获取消息失败"); } }); }, 2000); // 发送消息 $("#send-btn").click(function() { var msg = $("#msg").val(); $.ajax({ url: "/sendMsg", type: "post", data: {msg: msg}, success: function(result) { console.log(result); $("#msg-box").append("<p style='color: blue;'>我:" + msg + "</p>"); $("#msg").val(""); }, error: function() { console.log("发送消息失败"); } }); }); }); </script> ``` 接下来是后端代码实现: 1. 定义消息实体类: ```java public class Message { private Integer id; private String content; private Date createTime; // getter和setter省略 } ``` 2. 定义消息DAO接口和Mapper文件: ```java public interface MessageMapper { void addMessage(Message message); List<Message> getAllMessages(); } ``` ```xml <mapper namespace="com.example.dao.MessageMapper"> <insert id="addMessage" parameterType="com.example.entity.Message"> insert into message(content, create_time) values(#{content}, now()) </insert> <select id="getAllMessages" resultMap="messageResultMap"> select * from message order by create_time desc </select> <resultMap id="messageResultMap" type="com.example.entity.Message"> <id column="id" property="id"/> <result column="content" property="content"/> <result column="create_time" property="createTime"/> </resultMap> </mapper> ``` 3. 定义消息Service接口和实现: ```java public interface MessageService { void sendMessage(String content); List<String> getAllMessages(); } @Service public class MessageServiceImpl implements MessageService { @Autowired private MessageMapper messageMapper; @Override public void sendMessage(String content) { Message message = new Message(); message.setContent(content); messageMapper.addMessage(message); } @Override public List<String> getAllMessages() { List<Message> messages = messageMapper.getAllMessages(); List<String> contents = new ArrayList<>(); for (Message message : messages) { contents.add(message.getContent()); } return contents; } } ``` 4. 定义Controller处理消息发送和接收请求: ```java @Controller public class MessageController { @Autowired private MessageService messageService; @RequestMapping("/sendMsg") @ResponseBody public String sendMsg(String msg) { messageService.sendMessage(msg); return "success"; } @RequestMapping("/getMsg") @ResponseBody public String getMsg() { List<String> messages = messageService.getAllMessages(); if (messages.size() > 0) { return messages.get(0); } else { return ""; } } } ``` 以上代码就是SSM+jQuery实现聊天功能的示例,可以根据实际需求进行修改和扩展。需要注意的是,该示例只实现了简单的一对一聊天,如果需要实现多人聊天或群聊等功能,需要进行更多的业务逻辑开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古月_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值