美文网首页
查询、返回分页的json数据

查询、返回分页的json数据

作者: 少寨主的互联网洞察 | 来源:发表于2018-09-06 10:35 被阅读0次

whyjson?

json数据对不同设备的请求更容易处理

整体架构

  • controller
/**
     * @param pn
     * @param model
     * @return
     * 导入jackson包
     */
    @RequestMapping("/emps")
    @ResponseBody
    public Msg getEmpsWithJson(@RequestParam(value="pn",defaultValue="1")Integer pn,
            Model model) {
        PageHelper.startPage(pn, 5);
        List<Emploee> emps=emploeeService.getAll();
        PageInfo page=new PageInfo(emps,5);
        model.addAttribute("pageInfo",page);
        return Msg.success().add("pageInfo",page);
    } 
  • 引入Jackson包
<!-- 返回json字符串的支持 -->
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>
  • 返回结构信息封装
public class Msg {
    private int code;
    private String msg;
    //用户要返回给浏览器的数据
    private Map<String,Object> extend=new HashMap<String,Object>();
    
    public static Msg success() {
        Msg result=new Msg();
        result.setCode(100);
        result.setMsg("处理成功!");
        return result;
    }
    public static Msg fail() {
        Msg result=new Msg();
        result.setCode(200);
        result.setMsg("处理失败!");
        return result;
    }
    public Msg add(String key,Object value) {
        this.getExtend().put(key, value);
        return this;
    }
    
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Map<String, Object> getExtend() {
        return extend;
    }
    public void setExtend(Map<String, Object> extend) {
        this.extend = extend;
    }
    
}
  • service:
@Service
public class EmploeeService {
    @Autowired
    EmploeeMapper emploeeMapper;
    public List<Emploee> getAll() {
        // TODO Auto-generated method stub
        return emploeeMapper.selectByExampleWithDept(null);
    }

}
  • dao:
List<Emploee> selectByExampleWithDept(EmploeeExample example);
  • mapper:
<?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.cqu.dao.EmploeeMapper">
  <resultMap type="com.cqu.bean.Emploee" id="WithDeptResultMap">
    <id column="emp_id" jdbcType="INTEGER" property="empId" />
    <result column="emp_name" jdbcType="VARCHAR" property="empName" />
    <result column="gender" jdbcType="BIT" property="gender" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="d_id" jdbcType="INTEGER" property="dId" />
    <association property="department" javaType="com.cqu.bean.Department">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
    </association>
  </resultMap>

<sql id="WithDept_Colum_List">
    e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
  </sql>

<select id="selectByExampleWithDept" resultMap="WithDeptResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="WithDept_Colum_List" />
    FROM tbl_emp e
        left join tbl_dept d on e.`d_id`=d.`dept_id`
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
</mapper>
  • 数据库表结构


    图片.png

测试

访问:http://localhost:8080/myPro/emps?pn=5
返回结果:

图片.png

相关文章

网友评论

      本文标题:查询、返回分页的json数据

      本文链接:https://www.haomeiwen.com/subject/yeitgftx.html