在上一篇我们实现了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.测试
可以看出,程序成功在数据库表中模糊查询出了符合条件的商品并返回信息