mybatis实现从数据库获取商品列表的信息

mybatis是一个流行的Java持久层框架,它提供了一种简单、可读性高的方式来将SQL语句和Java代码进行映射,因此被广泛使用于各种Java项目,以下是一个简单的示例,展示了mybatis如何从数据库获取商品列表的信息并将信息返回

1.新建数据库表插入商品信息

这里我将id设置为主键自增,使用category_id来区分商品的种类

2.创建实体类Goodist

@Data
@Entity
public class GoodList {
    @Id
    private int goodListId;				//商品编号
    private String goodListName;	//商品名称
    private double goodListPrice;		//商品单价
    private String goodListPicture;
    private String goodListBriefIntroduction;
    private Integer categoryId;

}

3.创建GoodMapper.xml

在resources目录下新建mappers包,在mappers包下新建GoodMapper.xml,将数据库表字段与实体类字段对应

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.software.PetShop.mapper.GoodMapper">
    <resultMap id="goodResult" type="com.software.PetShop.entity.GoodList">
            <id column="good_list_id" property="goodListId"/>
            <id column="good_list_name" property="goodListName"/>
            <id column="good_list_price" property="goodListPrice"/>
            <id column="good_list_brief_introduction"         
                        property="goodListBriefIntroduction"/>
            <id column="good_list_picture" property="goodListPicture"/>
            <id column="category_id" property="categoryId"/>
    </resultMap>
   
    </mapper>

4.配置mybatis-config.xml

在<mappers>下新加GoodMapper.xml映射文件

<mappers>
    <mapper resource="mappers/GoodMapper.xml"></mapper>
</mappers>

5.创建GoodController,新建getAllGoodList()方法

将CartMapper中的getAllGoodList方法获取到的数据填充在allGoodList列表里,遍历将商品信息存入jsonArray中,根据判断条件将jsonArray和状态码添加到jsonObject1中并返回数据

package com.software.PetShop.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.software.PetShop.entity.GoodList;
import com.software.PetShop.mapper.GoodMapper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.software.PetShop.utils.MybatisUtils.getSqlSession;

@RestController
@RequestMapping("/petShop")
public class GoodController {
/**
     * 获取所有商品列表**/
    @GetMapping("/getAllGoodsList")
    public String getAllGoodList() {
        SqlSession sqlSession = getSqlSession();
        try {
            GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
            List<GoodList> allGoodList = goodMapper.getAllGoodList();
            System.out.println("goodList数据:"+allGoodList);
            JSONArray jsonArray = new JSONArray();
            for (GoodList goodList : allGoodList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("goodId", goodList.getGoodListId());
                jsonObject.put("goodName", goodList.getGoodListName());
                jsonObject.put("goodBriefIntroduction",     
                                 goodList.getGoodListBriefIntroduction());
                jsonObject.put("goodPicture", goodList.getGoodListPicture());
                jsonObject.put("goodPrice", goodList.getGoodListPrice());
                jsonObject.put("goodCategoryId", goodList.getCategoryId());
                System.out.println("goodList数据:" + goodList.getGoodListId() + ": " + 
                                 goodList.getGoodListName());
                jsonArray.put(jsonObject);
            }
                JSONObject jsonObject1 = new JSONObject();
                if (jsonArray!=null){
                    jsonObject1.append("data",jsonArray);
                    jsonObject1.append("status","ok");
                }else{
                    jsonObject1.append("status","no");
                }
                System.out.println("jsonobject: "+jsonObject1);
                return jsonObject1.toString();

        } finally {
            sqlSession.close();
        }
    }
}

6.创建GoodMapper接口,实现getAllGoodList()方法

注意方法名和类型要与GoodController里面一致

public interface GoodMapper extends BaseMapper<GoodList> {

    List<GoodList> getAllGoodList();

}

7.在GoodMapper.xml中实现sql查询语句

注意id要与方法名一致

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.software.PetShop.mapper.GoodMapper">
    <resultMap id="goodResult" type="com.software.PetShop.entity.GoodList">
            <id column="good_list_id" property="goodListId"/>
            <id column="good_list_name" property="goodListName"/>
            <id column="good_list_price" property="goodListPrice"/>
            <id column="good_list_brief_introduction" property="goodListBriefIntroduction"/>
            <id column="good_list_picture" property="goodListPicture"/>
            <id column="category_id" property="categoryId"/>
    </resultMap>

     <select id="getAllGoodList" resultMap="goodResult">
        SELECT * FROM good_list
    </select>

</mapper>

8.测试

可以看出,程序成功在数据库表中查询出了商品的数据信息并返回了信息状态

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Redis、RabbitMQ和MyBatis可以结合使用来实现秒杀功能。 首先,Redis可以用作秒杀的缓存层。当用户请求秒杀商品时,可以先从Redis中查询商品的库存信息。由于Redis的高性能和擅长处理高并发的特性,可以快速返回库存信息,减轻数据库的压力。同时,为了防止超卖现象的发生,在Redis中可以设置一个计数器,记录已经被抢购的商品数量,每次秒杀成功后即使库存减一,确保不会超过实际库存数量。 其次,RabbitMQ可以用来处理秒杀请求的异步处理。当用户发起秒杀请求后,可以将请求消息发送至RabbitMQ中的秒杀队列。然后,消费者可以异步地从队列中获取消息,进行处理。这样可以有效地削峰填谷,降低系统的压力,提高并发处理能力。另外,通过RabbitMQ还可以实现消息的延迟投递功能,可以设置一个定时任务,定时将未处理完的请求重新放入队列中进行处理。 最后,MyBatis可以用来操作数据库,处理秒杀请求的商品库存信息。当消费者从队列中获取到秒杀请求消息后,可以通过MyBatis来更新商品库存信息MyBatis提供了强大的数据库操作功能,可以方便地将秒杀请求与数据库进行交互。同时,为了防止超卖现象的发生,可以在更新库存信息之前进行乐观锁的检查,确保库存足够的情况下才进行更新操作。 综上所述,使用Redis、RabbitMQ和MyBatis可以实现一个高效、可靠的秒杀系统。Redis用于缓存商品库存信息,RabbitMQ用于异步处理秒杀请求,MyBatis用于操作数据库。通过这三个工具的结合利用,可以满足高并发场景下的秒杀需求,提高系统的性能和可伸缩性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值