詹子聪 5 лет назад
Родитель
Сommit
90fbe2fdf6

+ 2 - 2
src/main/java/com/miekir/shibei/bean/CouponBean.java

@@ -39,14 +39,14 @@ public class CouponBean {
      * 点击之后跳转的链接
      */
     @Basic
-    @Column(name = "jumpUrl", nullable = true, insertable = true, updatable = true)
+    @Column(columnDefinition = "MEDIUMTEXT", name = "jumpUrl", nullable = true, insertable = true, updatable = true)
     public String jumpUrl;
 
     /**
      * 封面图片URL
      */
     @Basic
-    @Column(name = "coverUrl", nullable = true, insertable = true, updatable = true)
+    @Column(columnDefinition = "MEDIUMTEXT", name = "coverUrl", nullable = true, insertable = true, updatable = true)
     public String coverUrl;
 
     /**

+ 2 - 2
src/main/java/com/miekir/shibei/bean/GoodsBean.java

@@ -27,7 +27,7 @@ public class GoodsBean {
      * 封面图片地址
      */
     @Basic
-    @Column(name = "coverImageUrl", nullable = true, insertable = true, updatable = true)
+    @Column(columnDefinition = "MEDIUMTEXT", name = "coverImageUrl", nullable = true, insertable = true, updatable = true)
     public String coverImageUrl;
     /**
      * 标题
@@ -105,7 +105,7 @@ public class GoodsBean {
      * 商品链接
      */
     @Basic
-    @Column(name = "goodsUrl", nullable = true, insertable = true, updatable = true)
+    @Column(columnDefinition = "MEDIUMTEXT", name = "goodsUrl", nullable = true, insertable = true, updatable = true)
     public String goodsUrl;
     /**
      * 月销量

+ 31 - 0
src/main/java/com/miekir/shibei/controller/api/GoodsController.java

@@ -136,4 +136,35 @@ public class GoodsController {
         responseResult.setMessage("获取成功");
         return JSON.toJSONString(responseResult);
     }
+
+    /**
+     * 根据关键字查询京东商品
+     */
+    @RequestMapping(value = "/api/getGoodsListByKeyword", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String getGoodsListByKeyword(String keywords, int pageNum, int pageSize) {
+        ResponseResult<List<GoodsBean>> responseResult = new ResponseResult<List<GoodsBean>>();
+        responseResult.setMessage("获取失败");
+
+        // 自带的分页查询
+        List<GoodsBean> goodsBeanList;
+        try {
+            // 如果pageNum为1,pageSize为20,则查询第1*20条到第1*20+20-1(这里的第几条是从0开始的)
+            goodsBeanList = goodsRepository.getGoodsListByKeyword(keywords, pageNum*pageSize, pageSize);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        try {
+            responseResult.setContent(goodsBeanList);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        responseResult.setCode(ResultCode.SUCCESS);
+        responseResult.setMessage("获取成功");
+        return JSON.toJSONString(responseResult);
+    }
 }

+ 4 - 0
src/main/java/com/miekir/shibei/repository/GoodsRepository.java

@@ -1,6 +1,7 @@
 package com.miekir.shibei.repository;
 
 import com.miekir.shibei.bean.GoodsBean;
+import com.miekir.shibei.bean.JsonBean;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.Query;
 import org.springframework.data.repository.query.Param;
@@ -22,6 +23,9 @@ public interface GoodsRepository extends JpaRepository<GoodsBean, Integer> {
     @Query(value="select goodsBean from GoodsBean goodsBean where goodsBean.id=:id")
     public GoodsBean findGoodsById(@Param("id") Long id);
 
+    // 使用原生SQL MYSQL 不支持TOP,要用连接符号||连接,否则会被当成字符串常量拼成%?1%
+    @Query(value="SELECT * FROM t_goods where title like '%'||?1||'%' limit ?2, ?3", nativeQuery = true)
+    public List<GoodsBean> getGoodsListByKeyword(String keywords, int startNum, int pageSize);
 
 
 //    @Transactional