发表于: 2017-05-22 22:26:05

1 1160


今日目标:添加Mybatis,数据库连接池,初步实现接口。

添加Mybatis和C3p0已经在任务1的日报中实现。

编写StudentController:

  1. package com.qhs.rest.controller;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import org.apache.ibatis.io.Resources;
  7. import org.apache.ibatis.session.SqlSession;
  8. import org.apache.ibatis.session.SqlSessionFactory;
  9. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  10. import org.json.JSONArray;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.web.servlet.ModelAndView;
  14. import com.qhs.rest.bean.Student;
  15. import com.qhs.rest.mapper.StudentMapper;
  16. @Controller
  17. public class StudentController {
  18.    @RequestMapping(value="/students",method=RequestMethod.GET)
  19.    @ResponseBody
  20.    //分页查询所有用户,加入默认页数参数为1
  21.    //2017年5月22日21:37:13 未完成根据专业查询用户
  22.    public ModelAndView getList(@RequestParam(value="pn",required=false,defaultValue="1") Integer pn) throws IOException{
  23.        //指定mapper
  24.        StudentMapper mapper = new SqlSessionFactoryBuilder()
  25.                .build(Resources.getResourceAsStream("mybatis-config.xml")).openSession()
  26.                .getMapper(StudentMapper.class);
  27.        List<Student> list = new ArrayList<Student>();
  28.        //不带参数,pn=0,参数为0,10
  29.        //带2,参数为10,20
  30.        list = mapper.list((pn-1)*10,pn*10);
  31.        ModelAndView mav = new ModelAndView("students");
  32.        JSONArray json = new JSONArray();
  33.        json.put(list);
  34.        mav.addObject("msg",json);
  35.        return mav;
  36.    }
  37. }

指定处理/students请求(实现了视图定位),方法为get,传参数pn

将pn作为参数传入,交给Mapper处理,返回List,直接封装进json对象,直接作为返回值传出。


  1. package com.qhs.rest.mapper;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.*;
  4. import com.qhs.rest.bean.Student;
  5. public interface  StudentMapper {
  6.    @Select("select * from signup limit #{start},#{count}")
  7.    public List<Student> list(@Param("start") int start, @Param("count")int count);  
  8.    @Select("select * from signup where id= #{id}")
  9.    public Student get(int id);
  10.    @Select("select * from signup where id= #{name}")
  11.    public Student getall(String name);
  12.    @Insert("INSERT INTO signup VALUES "
  13.            + "(UNIX_TIMESTAMP(now())*1000,UNIX_TIMESTAMP(now())*1000,null,#{name},#{qq},#{major},#{start_date},"
  14.            + "#{school},#{onlineclass},#{onlineno},#{diarylink},"
  15.            + "#{aim},#{recommender},#{censor},#{wherefrom})")
  16.    public int add(Student student);
  17.    @Delete("delete * from signup where id= #{id}")
  18.    public boolean delete(int id);
  19. }

mapper是接口,其中提供一个分页查询的方法。

students.jsp里直接用EL表达式输出msg的内容。

  1. </head>
  2. <body>
  3. ${msg}
  4. </body>
  5. </html>

初步实现了接口的分页查询所有学生的功能。

参考文档:http://www.cnblogs.com/wangchuanfu/p/5913310.html(SpringMvc之@RequestParam详解)

遇到的问题:


1.JSP中文乱码(手动指定编码为UTF-8解决)


2.向github提交项目的时候,由于之前误操作添加了内置的Tomcat,多出了400+个Unstaged Changes,只好删掉整个项目重新从github导入。

导入时报错,Java compiler level does not match,解决方法参见https://jingyan.baidu.com/article/95c9d20da3ec5fec4e756186.html。


3.暂时不知道要处理两个参数时,方法体内应该怎么写。。逐个判断是否为空?


明日目标:

继续做接口……对参数major的响应,以及student/name/的实现。


返回列表 返回列表
评论

    分享到