10-2 图书管理系统(SSM+LayUi)

返回响应信息工具类

  • 在com.gychen.util里新建Constants和RestRespond
    • Constants
      package com.gychen.util;
      
      /**
       * 常量类
       */
      
      public class Constants {
      
          public final static int OK_CODE = 0;
          public final static int FAIL_CODE = 400;
          public final static int OTHER_FAIL_CODE = 333;
          public final static String  OK_MSG = "请求成功";
          public final static String FAIL_MSG = "请求失败";
          public final static int STATUS_0 = 0;  //可用状态
          public final static int STATUS_1 = 1;   //禁用状态
      
          // 单个对象
          public static final String ITEM = "item";
      
          // 返回的对象列表
          public static final String LIST = "list";
      
          public final static String CACHE_NAME = "KACache";
      
      }
    
    
    • RestRespond
      package com.gychen.util;
      
      import java.util.HashMap;
      import java.util.List;
      
      /**
       * REST 接口返回数据
       *
       * @author gychen
       */
      public class RestResponse extends HashMap {
      
          /**
           * 禁止通过构造函数构造对象,只能通过静态方法获取实例。
           *
           * @see #ok()
           * @see #ok(String)
           * @see #fail()
           * @see #fail(String)
           */
          private RestResponse() {
          }
      
      
      
          /**
           * 设置接口返回的文本消息,属性 key: message
           *
           * @param msg
           * @return
           */
          public RestResponse msg(String msg) {
              this.put(Constants.OK_MSG, msg);
              return this;
          }
      
          /**
           * 设置接口返回的数据对象,属性 key: item
           *
           * @param item
           * @return
           */
          public RestResponse item(Object item) {
              this.put(Constants.ITEM, item);
              return this;
          }
      
          /**
           * 设置接口返回的数据对象列表,属性 key: list
           *
           * @param list
           * @return
           */
          public RestResponse list(List list) {
              this.put("data", list);
              return this;
          }
      
          /**
           * 设置接口返回的数据项,并指定数据项的属性 key
           *
           * @param key
           * @param value
           * @return
           */
          public RestResponse put(String key, Object value) {
              super.put(key, value);
              return this;
          }
      
          /**
           * 接口执行成功的返回数据,其中属性 OK_CODE = 0
           *
           * @return
           */
          public static RestResponse ok() {
              RestResponse result = new RestResponse();
              result.put("code", Constants.OK_CODE);
              result.put("msg", Constants.OK_MSG);
              return result;
          }
      
          /**
           * 接口执行成功的返回数据,并设置文本消息
           *
           * @param msg
           * @return
           */
          public static RestResponse ok(String msg) {
              RestResponse result = new RestResponse();
              result.put("code", Constants.OK_CODE);
              result.put("msg", Constants.OK_MSG).msg(msg);
              return result;
          }
      
          /**
           * 接口执行成功的返回数据,并设置对象数据
           *
           * @param item
           * @return
           */
          public static RestResponse ok(Object item) {
              RestResponse result = new RestResponse();
              result.put("msg", Constants.OK_MSG);
              result.put("code", Constants.OK_CODE).item(item);
              return result;
          }
      
          /**
           * 接口执行成功的返回数据,并设置列表对象数据
           *
           * @param list
           * @return
           */
          public static RestResponse ok(List list) {
              RestResponse result = new RestResponse();
              result.put("code", Constants.OK_CODE);
              result.put("msg",Constants.OK_MSG).list(list);
              return result;
          }
      
          /**
           * 接口执行失败的返回数据,其中属性 FIAL_CODE = 1
           *
           * @return
           */
          public static RestResponse fail() {
              RestResponse result = new RestResponse();
              result.put("code", Constants.FAIL_CODE);
              result.put("msg", Constants.FAIL_MSG);
              return result;
          }
      
          /**
           * 接口执行失败的返回数据,并设置文本消息,其中属性 error = 1, message = {msg}
           *
           * @param msg
           * @return
           */
          public static RestResponse fail(String msg) {
              RestResponse result = new RestResponse();
              result.put("msg", Constants.FAIL_MSG);
              result.put("code", Constants.FAIL_CODE).msg(msg);
              return result;
          }
      
          /**
           * 接口执行失败的返回数据,自定义状态码,其中属性 error = {errcode}
           *
           * @param errcode
           * @return
           */
          public static RestResponse fail(int errcode) {
              RestResponse result = new RestResponse();
              result.put("msg", Constants.FAIL_MSG);
              result.put("code", errcode);
              return result;
          }
      }
    

新建TypeController

  • 在com.gychen.controller里新建TypeController
package com.gychen.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TypeController {

    @RequestMapping("/typeIndex")
    public String typeIndex(){
        
        return "type/typeIndex";
    }


    /**
     * 获取type数据信息 分页
     */
    @RequestMapping("/typeAll")
    @ResponseBody   // 以json格式返回
    public Object typeAll(){
        
        return null;
    }
}

修改typeIndex.jsp

  • 添加jsp头文件和jstl、el、路径信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%
    String path=request.getContextPath();
    String basePath=request.getScheme()+
            "://"+request.getServerName()+
            ":"+request.getServerPort()+path+
            "/";
%>

搜索信息

修改ClassInfoDao

  • 在com.gychen.dao里修改ClassInfoDao
package com.gychen.dao;

import com.gychen.po.ClassInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("classDao")
public interface ClassInfoDao {
    /**
     * 查询所有图书类型信息
     */
//    @Select("select * from class_info")
//    List queryClassInfoAll();

    List queryClassInfoAll(@Param(value = "name")String name);

}

添加ClassInfoDao.xml

  • 在resources/com.gychen.dao里新建ClassInfoDao.xml




    
    


  • 在ClassInfoService中修改接口信息

List queryClassInfoAll (@Param(value = "name")String name);

  • 修改TypeController
package com.gychen.controller;

import com.github.pagehelper.PageInfo;
import com.gychen.po.ClassInfo;
import com.gychen.service.ClassInfoService;
import com.gychen.util.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TypeController {

    @Autowired
    private ClassInfoService classInfoService;  // 这里的classInfoService要和ClassInfoServiceImpl中的注解名字相同
    @RequestMapping("/typeIndex")
    public String typeIndex(){

        return "type/typeIndex";
    }


    /**
     * 获取type数据信息 分页
     */
    @RequestMapping("/typeAll")
    @ResponseBody
    public RestResponse typeAll(String name, @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "15") Integer limit){

        PageInfo pageInfo = classInfoService.queryClassInfoAll(name, page, limit);

        return RestResponse.ok(pageInfo.getList());
    }
}

  • 修改typeindex.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%
    String path=request.getContextPath();
    String basePath=request.getScheme()+
            "://"+request.getServerName()+
            ":"+request.getServerPort()+path+
            "/";
%>

类型名称:

你可能感兴趣的:(10-2 图书管理系统(SSM+LayUi))