SSM框架的简单使用----基于单表的增删改查功能

SSM框架的简单使用----基于单表的增删改查功能

1、前言:

在前面配好的SSM框架基础上,我们今天来进行一些简单的单表增删改查的简单使用
如果有不清楚SSM框架的同学可以参考上一篇博客
https://blog.csdn.net/m0_50217781/article/details/111485399

2、功能实现:

2.1 分页查询功能:

这里我们需要用到

  • pagehelper-5.1.9.jar
  • jsqlparser-2.1.jar
    这两个jar包,可以帮我们自动封装好需要的分页资源,经过处理后我们就可以直接在页面上显示了

1)在beans-datasource.xml文件的SQLSessionFactoryBean配置中添加以下配置:

<!--分页插件  -->
<property name="plugins">
    <array>
        <bean class="com.github.pagehelper.PageInterceptor">
            <!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
            <property name="properties">
                <value>
                    helperDialect=mysql
                    reasonable=true
                    supportMethodsArguments=true
                    params=count=countSql
                    autoRuntimeDialect=true
                </value>
            </property>
        </bean>
    </array>
</property>

2)自定义一个MyPage实体类:

import com.github.pagehelper.Page;
import java.util.List;
public class MyPage<T> {
    
    
    private int curryPage;//当前页
    private int totalPage;//总页面数量
    private List<T> dateList;//每页的数据集合

    public MyPage(){
    
    
    }
    public MyPage(Page page){
    
    
        curryPage=page.getPageNum();
        totalPage=page.getPages();
        dateList=page.getResult();
    }
    public int getCurryPage() {
    
    
        if (curryPage<1)curryPage=1;
        if (curryPage>totalPage)curryPage=totalPage;
        return curryPage;
    }

    public void setCurryPage(int curryPage) {
    
    
        this.curryPage = curryPage;
    }

    public int getTotalPage() {
    
    
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
    
    
        this.totalPage = totalPage;
    }

    public List<T> getDateList() {
    
    
        return dateList;
    }

    public void setDateList(List<T> dateList) {
    
    
        this.dateList = dateList;
    }
}

3)在service层中查询出我们需要的数据:

	@Autowired
    UserinfoMapper mapper;
    @Override
    public MyPage<Userinfo> selectUserByCurryPageAndPageSize(int curry, int pageSize) {
    
    
        PageHelper.startPage(curry, pageSize);
        List<Userinfo> allUser = mapper.selectAllUser();
        Page page=(Page)allUser;
        MyPage myPage=new MyPage(page);
        return myPage;
    }

4)在controller中传递MyPage到jsp页面:

	@Autowired
    UserService userService;
    @RequestMapping("/info")
    public ModelAndView showUser(@RequestParam(value="pageNum",defaultValue="1") int  pageNum){
    
    
        MyPage<Userinfo> myPage = userService.selectUserByCurryPageAndPageSize(pageNum, 10);
        ModelAndView mv=new ModelAndView();
        mv.addObject("myPage",myPage);
        mv.setViewName("info");
        return mv;
    }

5)jsp页面的编写:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table width="80%" border="1">
    <tr>
        <th>id</th>
        <th>username</th>
        <th>birthday</th>
        <th>sex</th>
        <th>address</th>
        <th>money</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${myPage.dateList}" var="item">
    <tr>
        <td>${
    
    item.id}</td>
        <td>${
    
    item.username}</td>
        <td>${
    
    item.birthday}</td>
        <td>${
    
    item.sex}</td>
        <td>${
    
    item.address}</td>
        <td>${
    
    item.money}</td>
        <td><a href="/delete?id=${item.id}">删除</a>
            <a href="/toUpdate?id=${item.id}">修改</a>
        </td>
    </tr>
    </c:forEach>
</table>
<a href="/info?pageNum=1">首页</a>
<a href="/info?pageNum=${myPage.curryPage-1}">上一页</a>
<a href="/info?pageNum=${myPage.curryPage}">当前页${
    
    myPage.curryPage}</a>
<a href="/info?pageNum=${myPage.curryPage+1}">下一页</a>
<a href="/info?pageNum=${myPage.totalPage}">尾页</a>
</body>
</html>

页面显示效果:
在这里插入图片描述

2.2 删除功能:

1)删除的超链接

当我们点击删除按钮的超链接时,是需要将当前的ID带过去到Controller中,所以在配置href的时候需要注意:
在这里插入图片描述

2)Controller层的编写

@RequestMapping("/delete")
    public ModelAndView test4(int id){
    
    
        ModelAndView mv=new ModelAndView();
        userService.deleteById(id);
        mv.setViewName("redirect:/info");
        return mv;
    }

注意:在参数传递的时候,需要注意参数名不能随便取,需要和我们实体类中的参数名保持一致,或者需要配置相关的参数,例如:

public ModelAndView test4(@PathVariable("id") int userIdint )

Service层的相关代码省略,此外也可以在执行时进行执行结果的判断,此处也省略

2.3 修改功能:

1)跳转到修改界面的超链接

同上,省略

2)跳转界面的Controller

我们需要将带过来的id获取后查询到用户,然后查询玩再返回给jsp页面

@RequestMapping("/toUpdate")
    public ModelAndView test2(int id){
    
    
        ModelAndView mv=new ModelAndView();
        Userinfo userinfo = userService.selectUserById(id);
        mv.addObject("user",userinfo);
        mv.setViewName("update");
        return mv;

3)修改的jsp页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jstl/fmt_rt"  prefix="fmt"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/doUpdate" method="post">
<table width="80%" border="1">
    <tr>
        <th>id</th>
        <th>username</th>
        <th>birthday</th>
        <th>sex</th>
        <th>address</th>
        <th>money</th>
    </tr>
    <tr align="center">
        <td ><input value="${user.id }" name="id" readonly="readonly"/></td>
        <td><input type="text" name="username" value="${user.username }"/> </td>
        <td><input type="text" name="sex" value="${user.sex }"/> </td>
        <td>
            <!-- html5 中新增的标签:日期标签 -->
            <input type="date" name="birthday"
                   value="<fmt:formatDate value='${user.birthday}' pattern='yyyy-MM-dd'/>"/>
        </td>
        <td><input type="text" name="address" value=" ${user.address}"/> </td>
        <td><input type="text" name="money" value=" ${user.money}"/> </td>
    </tr>
    <tr>
        <td colspan="6" align="center">
            <input type="submit" value="更新"/>
        </td>
    </tr>
</table>
</form>
</body>
</html>

4)用来执行修改的Controller:

@RequestMapping("/doUpdate")
    public ModelAndView test3(Userinfo userinfo){
    
    
        ModelAndView mv=new ModelAndView();
        int i = userService.updateByUserinfo(userinfo);
        if (i>0){
    
    
            mv.setViewName("redirect:/info");
        }else {
    
    
            mv.setViewName("redirect:/info");
        }
        return mv;
    }

特别注意:
在传递参数的时候我们是用实体类来接受的,但是值得注意的是,jsp页面上传过来的东西,本质上还是用的request.getParameter来接受的,所以类型全都是String型的,这里如果不修改,会直接出现400的错误
修改办法:

  1. 如果是时间格式或者数字格式,只需要在实体类中加上相关的注解就行了
@DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birthday;
  1. spring-web.xml 配置文件中,之前配置的处理器映射器和适配器需要更换为注解配置:

      <mvc:annotation-driven/>
    

5)Service层:

省略

2.4 新增功能:

此功能与修改功能类似,只需要跳转,然后提交和修改的一样,所以此处省略

3、总结:

参考文档:
https://docs.spring.io/spring-framework/docs/current/reference/html/web.html

欢迎小伙伴留言,评论,一起探讨相关的问题,一起成长突破自己

猜你喜欢

转载自blog.csdn.net/m0_50217781/article/details/111564149