dao层
// 模糊查询 List<UserInfo> findUserByName(String username);mapper
<!-- 模糊查询 %名字% --> <select id="findUserByName" parameterType="java.lang.String" resultType="com.zhongruan.bean.UserInfo"> select * from tb_user where username like '%' #{username} '%' </select>service
// 模糊查询 List<UserInfo> findUserByName(String username);serviceImpl
@Override public List<UserInfo> findUserByName(String username) { return userDao.findUserByName(username); }controller // 模糊查询 @RequestMapping("/findUserByName.do") public ModelAndView findUserByName(String username){ List userInfoList = userInfoService.findUserByName(username); ModelAndView mv = new ModelAndView(); mv.addObject(“userInfoList”,userInfoList); mv.setViewName(“user-list”); return mv; }
bean
// 登录 UserInfo doLogin(String username);mapper
<select id="doLogin" resultType="com.zhongruan.bean.UserInfo" parameterType="java.lang.String"> select *from tb_user where username=#{username}controller // 登录 @RequestMapping("/doLogin.do") public String doLogin(String username, String password, HttpSession session){ UserInfo userInfo = userInfoService.doLogin(username);
if(userInfo !=null){ if(userInfo.getPassword().equals(password)){ System.out.println("登录成功!"); session.setAttribute("userInfo",userInfo); return "main"; }else { System.out.println("密码错误,请重新登录!"); session.setAttribute("message","密码错误,请重新输入!"); return "../login"; } }else { System.out.println("用户不存在,请重新输入!"); session.setAttribute("message","用户不存在,请重新输入!"); return "../login"; } }a) pageHelper – 插件 步骤:
导入pageHelper的包在applicationContext.xml中告诉sqlSessioniFactory要开始用pageHelper在service传当前页和页码,然后在impl中开始分页讲查询到的结果集保存到PageInfo中并保存到ModelAndView并返回页面进行取值 // 查询全部 - 分页查询 @RequestMapping("/finAll.do") public ModelAndView findAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size){ List<UserInfo> userInfoList = userInfoService.findAll(page, size); // 把得到的结果集 给到PageHelper中得内置对象PageInfo中 PageInfo pageInfo =new PageInfo(userInfoList); ModelAndView mv = new ModelAndView(); mv.addObject("pageInfo",pageInfo); mv.setViewName("user-list"); return mv; }