增删改查(CURD)
提示:我们做好了准备工作,测试成功之后,我们做增删改查,这里建议大家单个功能操作成功之后,再操作下一个功能
注意:我们再做项目的时候,回出现很多细节的小问题,如果全部功能都操作完成之后,再运行项目的话,可能回出现问题,这样我们寻找问题的时候会有些困难!
前言
本篇文章中,介绍的是最简单,基础的增删改查的功能!
一,创建实体类
表名:SysUser
package com.xinxi2.bean; import com.alibaba.fastjson.annotation.JSONField; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; public class SysUser { /** 主键 */ private Integer id ; /** 用户名 */ private String userName ; /** 昵称 */ private String nickName ; /** 密码 */ private String password ; /** 账号状态(0正常 1停用) */ private Character status ; /** 邮箱 */ private String email ; /** 手机号 */ private String phonenumber ; /** 用户性别(0男,1女,2未知) */ private Character sex ; /** 头像 */ private String avatar ; /** 用户类型(0管理员,1普通用户) */ private Character userType ; /** 创建人的用户id */ private Integer createBy ; /** 创建时间 */ @DateTimeFormat(pattern="yyyy-MM-dd") // String 转 Date 视图到控制层 @JSONField(format = "yyyy-MM_dd") private Date createTime ; /** 更新人 */ private Integer updateBy ; /** 更新时间 */ @DateTimeFormat(pattern="yyyy-MM-dd") // String 转 Date 视图到控制层 @JSONField(format = "yyyy-MM_dd") private Date updateTime ; /** 删除标志(0代表未删除,1代表已删除) */ private Integer delFlag ; /** 主键 */ public Integer getId(){ return this.id; } /** 主键 */ public void setId(Integer id){ this.id=id; } /** 用户名 */ public String getUserName(){ return this.userName; } /** 用户名 */ public void setUserName(String userName){ this.userName=userName; } /** 昵称 */ public String getNickName(){ return this.nickName; } /** 昵称 */ public void setNickName(String nickName){ this.nickName=nickName; } /** 密码 */ public String getPassword(){ return this.password; } /** 密码 */ public void setPassword(String password){ this.password=password; } /** 账号状态(0正常 1停用) */ public Character getStatus(){ return this.status; } /** 账号状态(0正常 1停用) */ public void setStatus(Character status){ this.status=status; } /** 邮箱 */ public String getEmail(){ return this.email; } /** 邮箱 */ public void setEmail(String email){ this.email=email; } /** 手机号 */ public String getPhonenumber(){ return this.phonenumber; } /** 手机号 */ public void setPhonenumber(String phonenumber){ this.phonenumber=phonenumber; } /** 用户性别(0男,1女,2未知) */ public Character getSex(){ return this.sex; } /** 用户性别(0男,1女,2未知) */ public void setSex(Character sex){ this.sex=sex; } /** 头像 */ public String getAvatar(){ return this.avatar; } /** 头像 */ public void setAvatar(String avatar){ this.avatar=avatar; } /** 用户类型(0管理员,1普通用户) */ public Character getUserType(){ return this.userType; } /** 用户类型(0管理员,1普通用户) */ public void setUserType(Character userType){ this.userType=userType; } /** 创建人的用户id */ public Integer getCreateBy(){ return this.createBy; } /** 创建人的用户id */ public void setCreateBy(Integer createBy){ this.createBy=createBy; } /** 创建时间 */ public Date getCreateTime(){ return this.createTime; } /** 创建时间 */ public void setCreateTime(Date createTime){ this.createTime=createTime; } /** 更新人 */ public Integer getUpdateBy(){ return this.updateBy; } /** 更新人 */ public void setUpdateBy(Integer updateBy){ this.updateBy=updateBy; } /** 更新时间 */ public Date getUpdateTime(){ return this.updateTime; } /** 更新时间 */ public void setUpdateTime(Date updateTime){ this.updateTime=updateTime; } /** 删除标志(0代表未删除,1代表已删除) */ public Integer getDelFlag(){ return this.delFlag; } /** 删除标志(0代表未删除,1代表已删除) */ public void setDelFlag(Integer delFlag){ this.delFlag=delFlag; } }
注意:
@DateTimeFormat(pattern="yyyy-MM-dd") // String 转 Date 视图到控制层 @JSONField(format = "yyyy-MM_dd") // 这两个注释加在时间的封装的上面
二,创建Mapper接口
SysUserMapper :接口类
package com.xinxi2.dao; import com.xinxi2.bean.SysUser; import java.util.List; public interface SysUserMapper { // 查询全部 List<SysUser> getlistSysUser(SysUser sysUser); // 增加 int addSysUser(SysUser sysUser); // 根据id查询 SysUser updatesysUserw(Integer id); // 修改 int updateSysUser(SysUser sysUser); // 删除 int deleteSysUser(int id); }
准备mapper.xml文件
SysUserMapper.xml
<?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.xinxi2.dao.SysUserMapper"> <resultMap id="SysUserinto" type="com.xinxi2.bean.SysUser"> <id property="id" column="id"></id> <result property="userName" column="user_name"></result> <result property="nickName" column="nick_name"></result> <result property="password" column="password"></result> <result property="status" column="status"></result> <result property="email" column="email"></result> <result property="phonenumber" column="phonenumber"></result> <result property="sex" column="sex"></result> <result property="avatar" column="avatar"></result> <result property="userType" column="user_type"></result> <result property="createBy" column="create_by"></result> <result property="createTime" column="create_time"></result> <result property="updateBy" column="update_by"></result> <result property="updateTime" column="update_time"></result> <result property="delFlag" column="del_flag"></result> </resultMap> <select id="getlistSysUser" resultType="com.xinxi2.bean.SysUser" resultMap="SysUserinto"> SELECT id,user_name,nick_name,`password`,`status`,email,phonenumber,sex, avatar,user_type,create_by,create_time,update_by,update_time, del_flag FROM `sys_user` <where> <if test="id!=null"> and id=#{id} </if> <if test="userName!=null"> and user_name=#{userName} </if> <if test="nickName!=null"> and nick_name=#{nickName} </if> <if test="password!=null"> and password=#{password} </if> <if test="status!=null"> and status=#{status} </if> <if test="email!=null"> and email=#{email} </if> <if test="phonenumber!=null"> and phonenumber=#{phonenumber} </if> <if test="sex!=null"> and sex=#{sex} </if> <if test="avatar!=null"> and avatar=#{avatar} </if> <if test="userType!=null"> and user_type=#{userType} </if> <if test="createBy!=null"> and create_by=#{createBy} </if> <if test="createTime!=null"> and create_time=#{createTime} </if> <if test="updateBy!=null"> and update_by=#{updateBy} </if> <if test="updateTime!=null"> and update_time=#{updateTime} </if> <if test="delFlag!=null"> and del_flag=#{delFlag} </if> </where> </select> <insert id="addSysUser"> INSERT INTO `sys_user`(user_name,nick_name,`password`,`status`,email,phonenumber, sex,avatar,user_type,create_by,create_time,update_by,update_time,del_flag) values(#{userName},#{nickName},#{password},#{status},#{email},#{phonenumber},#{sex},#{avatar}, #{userType},#{createBy},#{createTime},#{updateBy},#{updateTime},#{delFlag}) </insert> <update id="updateSysUser"> update `sys_user` <set> <if test="id!=null"> id=#{id}, </if> <if test="userName!=null"> user_name=#{userName}, </if> <if test="nickName!=null"> nick_name=#{nickName}, </if> <if test="password!=null"> password=#{password}, </if> <if test="status!=null"> status=#{status}, </if> <if test="email!=null"> email=#{email}, </if> <if test="phonenumber!=null"> phonenumber=#{phonenumber}, </if> <if test="sex!=null"> sex=#{sex}, </if> <if test="avatar!=null"> avatar=#{avatar}, </if> <if test="userType!=null"> user_type=#{userType}, </if> <if test="createBy!=null"> create_by=#{createBy}, </if> <if test="createTime!=null"> create_time=#{createTime}, </if> <if test="updateBy!=null"> update_by=#{updateBy}, </if> <if test="updateTime!=null"> update_time=#{updateTime}, </if> <if test="delFlag!=null"> del_flag=#{delFlag}, </if> </set> where id = #{id} </update> <select id="updatesysUserw" parameterType="integer" resultMap="SysUserinto"> SELECT id,user_name,nick_name,`password`,`status`,email,phonenumber,sex, avatar,user_type,create_by,create_time,update_by,update_time, del_flag FROM `sys_user` where id=#{id} </select> <delete id="deleteSysUser"> delete from `sys_user` where id = #{id} </delete> </mapper>
四,创建service接口类
SysUserService
package com.xinxi2.service; import com.xinxi2.bean.SysUser; import java.util.List; public interface SysUserService { List<SysUser> getlistSysUser(SysUser sysUser); int addSysUser(SysUser sysUser); public SysUser updatesysUserw(Integer id); int updateSysUser(SysUser sysUser); int deleteSysUser(int id); }
五,写impl类
SysUserServiceImpl
package com.xinxi2.service.impl; import com.xinxi2.bean.SysUser; import com.xinxi2.dao.SysUserMapper; import com.xinxi2.service.SysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service("sysUserService") public class SysUserServiceImpl implements SysUserService { @Autowired private SysUserMapper sysUserMapper; @Override public List<SysUser> getlistSysUser(SysUser sysUser) { return sysUserMapper.getlistSysUser(sysUser); } @Override public int addSysUser(SysUser sysUser) { return sysUserMapper.addSysUser(sysUser); } @Override public SysUser updatesysUserw(Integer id) { return sysUserMapper.updatesysUserw(id); } @Override public int updateSysUser(SysUser sysUser) { return sysUserMapper.updateSysUser(sysUser); } @Override public int deleteSysUser(int id) { return sysUserMapper.deleteSysUser(id); } }
六,创建Controller测试类
SysUserController
package com.xinxi2.controller; import com.xinxi2.bean.SysUser; import com.xinxi2.service.SysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @Controller @RequestMapping("/sysUserController") public class SysUserController { @Autowired private SysUserService sysUserService; @RequestMapping("/sysUser") public String ListsysUser(SysUser sysUser, Model model){ List<SysUser> list = sysUserService.getlistSysUser(sysUser); model.addAttribute("tsList",list); return "list.jsp"; } @RequestMapping("tiaozhuan") public String tiaozhuan(){ return "add.jsp"; } @RequestMapping("/addsysUser") public String addsysUser(SysUser sysUser){ sysUserService.addSysUser(sysUser); return "redirect:/sysUserController/sysUser"; } @RequestMapping("updateById") public String updateById(int id,Model model){ SysUser sysUser = sysUserService.updatesysUserw(id); model.addAttribute("emp",sysUser); return "update.jsp"; } @RequestMapping("/update") public String update(SysUser sysUser){ sysUserService.updateSysUser(sysUser); return "redirect:/sysUserController/sysUser"; } @RequestMapping("delete") public String delete(int id){ sysUserService.deleteSysUser(id); return "redirect:/sysUserController/sysUser"; } }
七,创建需要的页面
add.jsp
list.jsp
update.jsp
查询页面list.jsp
删除修改按钮都写在这个页面
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2023/6/29 Time: 15:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>Title</title> </head> <body> <table border="1"> <tr> <td>编号</td> <td>用户名</td> <td>昵称</td> <td>密码</td> <td>账号状态</td> <td>邮箱</td> <td>手机号</td> <td>用户性别</td> <td>头像</td> <td>用户类型</td> <td>创建人的用户id</td> <td>创建时间</td> <td>更新人</td> <td>更新时间</td> <td>删除标志</td> <td>操作</td> </tr> <c:forEach items="${tsList}" var="b"> <tr> <td>${b.id}</td> <td>${b.userName}</td> <td>${b.nickName}</td> <td>${b.password}</td> <td>${b.status}</td> <td>${b.email}</td> <td>${b.phonenumber}</td> <td>${b.sex}</td> <td>${b.avatar}</td> <td>${b.userType}</td> <td>${b.createBy}</td> <td>${b.createTime}</td> <td>${b.updateBy}</td> <td>${b.updateTime}</td> <td>${b.delFlag}</td> <td><a href="/sysUserController/updateById?id=${b.id}">修改</a><a href="/sysUserController/delete?id=${b.id}">删除</a></td> </tr> </c:forEach> </table> <a href="/sysUserController/tiaozhuan">添加</a> </body> </html>
新增页面add.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2023/6/29 Time: 15:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="/sysUserController/addsysUser"> 用户名<input type="text" name="userName"><br> 昵称<input type="text" name="nickName"><br> 密码<input type="text" name="password"><br> 账号状态<input type="text" name="status"><br> 邮箱<input type="text" name="email"><br> 手机号<input type="text" name="phonenumber"><br> 用户性别<input type="text" name="sex"><br> 头像<input type="text" name="avatar"><br> 用户类型<input type="text" name="userType"><br> 创建人的用户id<input type="text" name="createBy"><br> 创建时间<input type="text" name="createTime"><br> 更新人<input type="text" name="updateBy"><br> 更新时间<input type="text" name="updateTime"><br> 删除标志<input type="text" name="delFlag"><br> <input type="submit"> </form> </body> </html>
删除页面update.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2023/6/29 Time: 16:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form method="post" action="/sysUserController/update"> <input type="hidden" name="id" value="${emp.id}"> 用户名<input type="text" name="userName" value="${emp.userName}"><br> 昵称<input type="text" name="nickName" value="${emp.nickName}"><br> 密码<input type="text" name="password" value="${emp.password}"><br> 账号状态<input type="text" name="status" value="${emp.status}"><br> 邮箱<input type="text" name="email" value="${emp.email}"><br> 手机号<input type="text" name="phonenumber" value="${emp.phonenumber}"><br> 用户性别<input type="text" name="sex" value="${emp.sex}"><br> 头像<input type="text" name="avatar" value="${emp.avatar}"><br> 用户类型<input type="text" name="userType" value="${emp.userType}"><br> 创建人的用户id<input type="text" name="createBy" value="${emp.createBy}"><br> 创建时间<input type="text" name="createTime" value="${emp.createTime}"><br> 更新人<input type="text" name="updateBy" value="${emp.updateBy}"><br> 更新时间<input type="text" name="updateTime" value="${emp.updateTime}"><br> 删除标志<input type="text" name="delFlag" value="${emp.delFlag}"><br> <input type="submit"> </form> </body> </html>
总结
提示:
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容),常作为数据源较简单的web项目的框架。 Spring Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。 Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。 页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据