前提:搭建子模块的ssm框架以及实现登录功能在前一篇博客已经给出,故不在赘述,这里只对登录后对数据库增删改查进行简单说明
- 说明框架图如下所示(资源文件以及webapp文件等前端材料自行准备,这里只做后台的编程介绍)
- 用户信息(UserInfo类):注意写完变量后利用系统自动生成get and set 等方法
package com.zhongruan.bean;
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3.IUserDao接口:与数据库操作紧密相关
package com.zhongruan.dao;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserDao {
public List<UserInfo> findAll();
public void save(UserInfo userInfo);
public UserInfo doLogin(UserInfo userInfo);
public void delete(UserInfo userInfo);
public boolean update (UserInfo userInfo);
public UserInfo queryById(long id);
}
4.在资源文件mapper文件夹下的usermapper.xml(执行sql命令,对数据库进行增删改查)
<?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.zhongruan.dao.IUserDao" >
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo
</select>
<select id="doLogin" parameterType="com.zhongruan.bean.UserInfo" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where username=#{username} and password=#{password}
</select>
<insert id="save" parameterType="com.zhongruan.bean.UserInfo">
insert into userinfo(id,username,password) values (#{id},#{username},#{password})
</insert>
<delete id="delete" parameterType="com.zhongruan.bean.UserInfo">
DELETE FROM userinfo WHERE id=#{id}
</delete>
<update
id="update" parameterType="com.zhongruan.bean.UserInfo">
update userinfo set username=#{username},password=#{password} where id=#{id}
</update>
<select id="queryById" parameterType="long" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where id = #{id}
</select>
</mapper>
5.服务层:
5.1IUserService接口
package com.zhongruan.service;
import com.zhongruan.bean.UserInfo;
import java.util.List;
public interface IUserService {
public List<UserInfo> findAll(int page ,int size);
public void save(UserInfo userInfo);
public Boolean doLogin(UserInfo userInfo);
public void delete(UserInfo userInfo);
public boolean updateUser(UserInfo userInfo);
public UserInfo queryById(long id);
}
5.2UserService类:实现IUserService接口中声明的函数,同时转到Dao层执行
package com.zhongruan.service.impl;
import com.github.pagehelper.PageHelper;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<UserInfo> findAll(int page ,int size) {
PageHelper.startPage(page,size);
return userDao.findAll();
}
@Override
public void save(UserInfo userInfo) {
userDao.save(userInfo);
}
@Override
public Boolean doLogin(UserInfo userInfo) {
UserInfo userInfo1 = userDao.doLogin(userInfo);
if(null!=userInfo1){
return true;
}else {
return false;
}
}
@Override
public void delete(UserInfo userInfo) {
userDao.delete(userInfo);
}
@Override
public boolean updateUser(UserInfo userInfo) {
return userDao.update(userInfo);
}
@Override
public UserInfo queryById(long id) {
return userDao.queryById(id);
}
}
6.控制层(UserController类):实现界面的控制和转换,实现后台操作的一个非常重要的类
package com.zhongruan.controller;
import com.github.pagehelper.PageInfo;
import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue="1") int page,@RequestParam(defaultValue="4")int size){
List<UserInfo> infos = userService.findAll(page,size);
PageInfo pageinfo =new PageInfo(infos);
ModelAndView mv=new ModelAndView();
mv.addObject("pageinfo",pageinfo);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("login.do")
public ModelAndView dologin(UserInfo userInfo){
Boolean flag = userService.doLogin(userInfo);
ModelAndView modelAndView=new ModelAndView();
if(flag){
modelAndView.setViewName("main");
}else{
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("save.do")
public String save(UserInfo userInfo){
userService.save(userInfo);
return "redirect:findAll.do";
}
@RequestMapping("delete.do")
public String delete(UserInfo userInfo){
userService.delete(userInfo);
return "redirect:findAll.do";
}
@RequestMapping("update.do")
public String update(Model model, long id) {
model.addAttribute("userInfo", userService.queryById(id));
return "user-update";
}
@RequestMapping("Save.do")
public String updateUser(Model model, UserInfo userInfo) {
userService.updateUser(userInfo);
userInfo = userService.queryById(userInfo.getId());
model.addAttribute("userInfo", userInfo);
return "redirect:/user/findAll.do";
}
}