导致问题:
1.前端接收数据时不知道数据存储的格式
2.不知道业务是否成功
返回的json数据中要有一个数据表达后端操作是否成功
package com.czdsj.core.dto; import lombok.Data; /** * 后端返回前端json数据实体 * 2022/6/7 */ @Data public class MyResult { private String code = "100";//业务状态码:100=》业务操作成功 500=》业务操作失败(系统异常) private String msg = "操作成功!";//业务返回的数据表述信息 “操作成功!” “登录成功!” private Object data;//业务返回的数据 }
之后所有controller类中返回json数据的方法返回值类型必须填写为封装的结果集类型
/** * 查询学生的所有数据-返回json * @return */ @RequestMapping(value = "/getStudentList",method = RequestMethod.GET) @ResponseBody public MyResult getStudentListData(){ log.info("查询学生的所有数据"); MyResult result = new MyResult(); //控制器层调用-业务接口 List<StudentDo> studentList = null; try { studentList = baseService.getStudentList(); } catch (Exception e) { e.printStackTrace();//知道错误的原因和代码的位置 result.setCode("500");//告诉前端后端系统异常了 result.setMsg("学生信息查询异常!请联系管理员处理~"); } result.setData(studentList);//将查询出来的数据放到data属性中 return result; }
登录界面jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <meta charset="UTF-8" /> <title>登录页面</title> <!-- 引入 JQuery --> <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery-3.6.0.min.js"></script> <!-- 引入自定义的js文件 --> <script type="text/javascript" src="${pageContext.request.contextPath}/static/base/js/index.js"></script> </head> <body> <!-- 登录表单 --> <form> <label>用户名</label> <input type="text" name="userName" id="userName" placeholder="请输入用户名.."/><br/> <label>密码</label> <input type="password" name="pwd" id="pwd" placeholder="请输入密码.."/><br/> <!-- 提交按钮-关闭默认表单提交功能 --> <button type="button" onclick="doLogin()">登录</button> </form> </body> </html>
登录相关ajax代码(js文件)
/** * 点击登录提交表单进行登录操作 */ function doLogin(){ var loginData={ userName:$("#userName").val(), pwd:$("#pwd").val() }; console.dir(loginData); $.ajax({ url:"/ssm/base/doLogin", type:"POST", data:JSON.stringify(loginData), dataType:"JSON", contentType:"application/json;charset=UTF-8", success:function(data){ if(data.code=="100"){ alert(data.msg); //页面跳转 }else{ alert(data.msg); } }, error:function(XMLHttpRequest, textStatus, errorThrown){ console.dir("请求失败!"); } }); }
控制器层(处理登录请求和结果响应)
package com.czdsj.base.controller; import com.czdsj.base.mybatis.domain.StudentDo; import com.czdsj.base.mybatis.domain.UserDo; import com.czdsj.base.service.BaseService; import com.czdsj.core.dto.MyResult; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; /** * 学生基础信息采集模块-控制器层 * 2022/6/1 */ @Controller @RequestMapping("/base") @Slf4j //日志注解 自动生成一个log对象 public class BaseController { /** * 自动注入接口实现层, name是实现层类型首字母小写即可 */ @Resource(name = "baseServiceImpl") private BaseService baseService; /** * 登录操作 * @param user * @return */ @RequestMapping(value = "/doLogin",method = RequestMethod.POST) @ResponseBody public MyResult doLogin(@RequestBody UserDo user){ log.info("基础模块 -- 登录操作"); MyResult result = new MyResult(); if(!("".equals(user.getUserName())||user.getUserName()==null&&"".equals(user.getPwd())||user.getPwd()==null)){ int i = baseService.doLogin(user); if(i>0){ result.setMsg("登录成功!"); }else{ result.setCode("500"); result.setMsg("登录失败!用户名或密码错误!"); } }else{ result.setMsg("用户名和密码不能为空!"); result.setCode("500"); return result; } return result; } }
业务接口层
package com.czdsj.base.service; import com.czdsj.base.mybatis.domain.StudentDo; import com.czdsj.base.mybatis.domain.UserDo; import java.util.List; /** * 学生基础信息采集-业务接口层 * 2022/6/1 */ public interface BaseService { /** * 用户登录 * @param user * @return */ int doLogin(UserDo user); }
业务接口实现层
package com.czdsj.base.service.impl; import com.czdsj.base.mybatis.dao.BaseMapper; import com.czdsj.base.mybatis.domain.StudentDo; import com.czdsj.base.mybatis.domain.UserDo; import com.czdsj.base.service.BaseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.List; /** * 学生基础信息采集-业务实现层 * 2022/6/1 */ @Service public class BaseServiceImpl implements BaseService { /** * 通过容器自动注入mybatis接口的实现 */ @Autowired private BaseMapper baseMapper; /** * 用户登录 * @param user * @return */ @Override public int doLogin(UserDo user) { int i = baseMapper.doLogin(user); return i; } }
Mybatis接口-mapper接口
package com.czdsj.base.mybatis.dao; import com.czdsj.base.mybatis.domain.StudentDo; import com.czdsj.base.mybatis.domain.UserDo; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * Mybatis接口层 * 2022/6/1 */ @Mapper public interface BaseMapper { int doLogin(UserDo user); }
MybatisSQL映射文件-mapperXML
<?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.czdsj.base.mybatis.dao.BaseMapper"> <select id="doLogin" resultType="java.lang.Integer" parameterType="com.czdsj.base.mybatis.domain.UserDo"> select count(*) from t_user where userName= #{userName} and pwd=#{pwd} </select> </mapper>