IDEA+Maven 整合SSM框架实现用户管理系统登陆,模糊查询,分页

it2022-05-05  94

IDEA+Maven 整合SSM框架实现用户管理系统登陆,模糊查询,分页

模糊查询

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"; } }

3. 完成分页

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; }


最新回复(0)