mybatis实现商品列表的模糊查询

本文介绍了如何使用Mybatis在数据库中实现商品列表的模糊查询功能,包括创建实体类、MapperXML文件的配置、SQL查询语句编写以及在控制器中调用相关方法返回JSON数据。
摘要由CSDN通过智能技术生成

在上一篇我们实现了mybatis从数据库获取商品列表的信息并将信息返回,那么在前端展示商品列表的时候,用户往往会根据自己的需求通过搜索来寻找符合的商品,以下是一个简单的示例展示了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>
    <mapper resource="mappers/GoodMapper.xml"></mapper>
</mappers>

5.在GoodController中新建getGoodListByGoodName()方法

在这里我使用categoryId作为商品种类标识符,searchText作为前端传输过来的搜索框内容

/**
     * 根据商品名字模糊查询,搜索商品列表
     * **/
    @PostMapping("/getGoodsListByGoodName")
    public String getGoodListByGoodName(@RequestBody String json) {
        JSONObject jsonObject = new JSONObject(json);
        int categoryId = jsonObject.getInt("categoryId");
        String searchText = jsonObject.getStr("searchText");
        SqlSession sqlSession = getSqlSession();
        try {
            GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
            List<GoodList> allGoodList =                 
                goodMapper.findGoodListByNameAndCategoryId(searchText,categoryId);
            JSONArray jsonArray = new JSONArray();
            for (GoodList goodList : allGoodList) {
                JSONObject clothJson = new JSONObject();
                clothJson.put("goodId", goodList.getGoodListId());
                clothJson.put("goodName", goodList.getGoodListName());
                clothJson.put("goodBriefIntroduction",     
                   goodList.getGoodListBriefIntroduction());
                clothJson.put("goodPicture", goodList.getGoodListPicture());
                clothJson.put("goodPrice", goodList.getGoodListPrice());
                clothJson.put("goodCategoryId", goodList.getCategoryId());
                System.out.println("goodList数据:" + goodList.getGoodListId() + ": " + 
                    goodList.getGoodListName());
                jsonArray.put(clothJson);
            }

            JSONObject result = new JSONObject();
            if (jsonArray!=null){
                result.append("data",jsonArray);
                result.append("status","ok");
            }else{
                result.append("status","no");
            }

            return result.toString();
        }finally {
            sqlSession.close();
        }

    }

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

6.在GoodMapper接口中实现findGoodListByNameAndCategoryId()方法

public interface GoodMapper extends BaseMapper<GoodList> {
    //根据商品名称搜索商品
    List<GoodList> findGoodListByNameAndCategoryId(String searchText,int categoryId);
}

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

<?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>

    <!--通过名字和种类id进行模糊查询-->
    <select id="findGoodListByNameAndCategoryId" resultMap="goodResult">
        SELECT * FROM good_list WHERE good_list_name LIKE CONCAT('%', #{searchText}, '%')     
        AND category_id = #{categoryId}
    </select>
</mapper>

8.测试

可以看出,程序成功在数据库表中模糊查询出了符合条件的商品并返回信息

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值