SSM框架实战(记账项目)

2021-12-03  本文已影响0人  凡哥爱丽姐
    该项目主要实现账单的增、删、改、分页处理以及模糊查询,可以很好的锻炼我们之前学的框架知识。
    首先我们先搭建SSM框架,如上两章节所示,这里不做过多阐述。

1、数据库

bills表.png
bill_type表.png

2、Bills和BillType实体类

package com.fan.entity;

import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class Bills {
    private Integer id;
    private String title;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date billTime;
    private Integer typeId;
    private double price;
    private String explain;

    private BillType billType;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getBillTime() {
        return billTime;
    }

    public void setBillTime(Date billTime) {
        this.billTime = billTime;
    }

    public Integer getTypeId() {
        return typeId;
    }

    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getExplain() {
        return explain;
    }

    public void setExplain(String explain) {
        this.explain = explain;
    }

    public BillType getBillType() {
        return billType;
    }

    public void setBillType(BillType billType) {
        this.billType = billType;
    }
}
package com.fan.entity;

import java.util.List;

public class BillType {
    private Integer id;
    private String name;

    private List<Bills> billsList;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Bills> getBillsList() {
        return billsList;
    }

    public void setBillsList(List<Bills> billsList) {
        this.billsList = billsList;
    }
}

3、BillsService接口类和BillsServiceImpl实现类

package com.fan.service;

import com.fan.entity.BillType;
import com.fan.entity.Bills;
import com.github.pagehelper.PageInfo;

import java.util.List;

public interface BillsService {

    //分页+模糊查
    public PageInfo<Bills> findAll(int pageindex,int pagesize,int typeid,String begintime,String endtime);
    //查询分类列表的方法
    public List<BillType> findtypes();

    //记账(增加)方法
    public int add(Bills bills);
    //删除方法
    public int delete(int id);

    //根据主键查找
    public Bills findbyid(int sid);
    //根据主键查找进行修改
    public int update(Bills bills);
}
package com.fan.service.impl;

import com.fan.dao.BillsDao;
import com.fan.entity.BillType;
import com.fan.entity.Bills;
import com.fan.service.BillsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BillsServiceImpl implements BillsService {
    @Resource
    private BillsDao billsDao;
    @Override
    public PageInfo<Bills> findAll(int pageindex, int pagesize, int typeid, String begintime, String endtime) {
        PageHelper.startPage(pageindex,pagesize);
        Map map=new HashMap();
        map.put("typeid",typeid);
        map.put("begintime",begintime);
        map.put("endtime",endtime);
        List<Bills> bills = billsDao.findAll(map);
        PageInfo pageInfo = new PageInfo(bills);

        return pageInfo;
    }

    @Override
    public List<BillType> findtypes() {
        return billsDao.findtypes();
    }

    @Override
    public int add(Bills bills) {
        return billsDao.add(bills);
    }

    @Override
    public int delete(int id) {
        return billsDao.delete(id);
    }

    @Override
    public Bills findbyid(int sid) {
        return billsDao.findbyid(sid);
    }

    @Override
    public int update(Bills bills) {
        return billsDao.update(bills);
    }
}

4、BillsDao接口类(此处我们省略dao接口类的实现类,所以配置文件得配置好)

package com.fan.dao;

import com.fan.entity.BillType;
import com.fan.entity.Bills;

import java.util.List;
import java.util.Map;

public interface BillsDao {
    //分页+模糊查
    public List<Bills> findAll(Map map);
    //查询类别下拉信息
    public List<BillType> findtypes();
    //记账(增加)方法
    public int add(Bills bills);
    //删除方法
    public int delete(int id);

    //根据主键查找
    public Bills findbyid(int sid);
    //根据主键查找进行修改
    public int update(Bills bills);
}

5、配置BillsDaoMapper.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">
<!--namespace 表示命名空间,通常定义的格式是接口的完整路径-->
<mapper namespace="com.fan.dao.BillsDao">
    <resultMap id="r1" type="com.fan.entity.Bills">
        <id property="id" column="id"></id>
        <result property="title" column="title"></result>
        <result property="billTime" column="bill_time"></result>
        <result property="typeId" column="type_id"></result>
        <result property="price" column="price"></result>
        <result property="explain" column="explain"></result>
        <association property="billType" javaType="com.fan.entity.BillType">
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
        </association>
    </resultMap>

    <select id="findAll" resultMap="r1">
        <!--select bills.*,bill_type.id btid,bill_type.name name from bills,bill_type-->
        select * from bills,bill_type
        where bills.type_id=bill_type.id
        <if test="typeid!=-1">
            and bills.type_id=#{typeid}
        </if>
        <if test="begintime!=null and begintime!=''">
            and bills.bill_time>#{begintime}
        </if>
        <if test="endtime!=null and endtime!=''">
            and bills.bill_time <![CDATA[ <= ]]> #{endtime}
        </if>
    </select>

    <!--查询分类列表的方法(查询类别下拉信息)-->
    <select id="findtypes" resultType="com.fan.entity.BillType">
        select * from bill_type
    </select>
    <!--记账(增加)-->
    <insert id="add">
        insert into bills values(null,#{title},#{billTime},#{typeId},#{price},#{explain})
    </insert>
    <!--删除账单-->
    <delete id="delete">
        delete from bills where id=#{id}
    </delete>

    <!--根据主键查找-->
    <select id="findbyid" resultMap="r1">
        select * from bills where id=#{sid}
    </select>
    <!--根据主键查找进行更新-->
    <update id="update">
        update bills set title=#{title},bill_time=#{billTime},
               type_id=#{typeId},price=#{price},`explain`=#{explain}
               where id=#{id}
    </update>
</mapper>

6、BillsController类

package com.fan.controller;

import com.fan.entity.BillType;
import com.fan.entity.Bills;
import com.fan.service.BillsService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class BillsController {
       @Resource
       private BillsService billsService;

       //定义每一页有几行数据
       public static int pagesize=5;

       @RequestMapping("/getbills")
       public String test(@RequestParam(defaultValue= "1") int pageindex, @RequestParam(defaultValue= "-1") int typeid,
                          String begintime, String endtime, ModelMap map){
           PageInfo<Bills> pageInfo = billsService.findAll(pageindex, pagesize, typeid, begintime, endtime);
           map.addAttribute("pageInfo",pageInfo);
           //查询类别下拉信息
           List<BillType> billTypes = billsService.findtypes();
           map.addAttribute("billtypes",billTypes);
           //回显
           map.addAttribute("tid",typeid);
           map.addAttribute("begin",begintime);
           map.addAttribute("end",endtime);
           return "showbills";
       }

       /**
        * 从数据库中查询类别信息,显示到记账(add)页面中
        * */
       @RequestMapping("/gettypes")
       public String gettypes(ModelMap map){
           List<BillType> findtypes = billsService.findtypes();
           map.addAttribute("findtypes",findtypes);
           return "add";
       }

       /**
        *记账(增加)功能
        * */
       @RequestMapping("/addBills")
       public String addBills(Bills bills){
           int i = billsService.add(bills);
           if(i>0){
               //增加成功
               return "redirect:/getbills";//重定向
           }else{
               //增加失败
               return "redirect:/gettypes";
           }
       }

       /**
        *删除功能
        * */
       @RequestMapping("/deleteBills")
       public void deleteBills(int id, HttpServletResponse response) {
           int i = billsService.delete(id);
           response.setContentType("text/html;charset=utf-8");

           try {
               PrintWriter out = response.getWriter();
               if(i>0){
                   out.print("<script>alert('删除成功!');location.href='/getbills'</script>");
               }else{
                   out.print("<script>alert('删除失败!');location.href='/getbills'</script>");
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

       /**
        * 根据主键查询
        * */
       @RequestMapping("/findbyid")
      public String findbyid(int id,ModelMap map){
           //查找类型
           List<BillType> findtypes = billsService.findtypes();
           map.addAttribute("findtypes",findtypes);
           //根据主键查找
           Bills findbyid = billsService.findbyid(id);
           map.addAttribute("findbyid",findbyid);
           return "update";
       }

    /**
     * 根据主键查询进行修改
     * */
    @RequestMapping("/updateBills")
    public String updateBills(Bills bills){
        int i = billsService.update(bills);
        if(i>0){
            System.out.println("更新成功");
            return "redirect:/getbills";
        }else{
            System.out.println("更新不成功");
            return "redirect:/findbyid?id="+bills.getId();
        }
    }
}

7、各个前端页面

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body onload="a()">
<script type="text/javascript">
    function a() {
         location.href="/getbills";
    }
</script>
<%--<h1>首页</h1>--%>
<%--<h2><a href="/findall">查询所有用户</a></h2>--%>
<%--</body>--%>
</html>

showbills.jsp

<%--
  Created by IntelliJ IDEA.
  User: Mr Wei
  Date: 2020/6/10
  Time: 17:28
  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" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>showbills</title>
</head>
<body>
<h1>记账管理</h1>
<form action="/getbills" method="post">
    <p>
        类型:<select name="typeid">
                <option value="-1">不限</option><!--全查的意思-->

              <c:forEach items="${billtypes}" var="type"> <!--var相当于取别名-->
                <option value="${type.id}" ${type.id==tid?'selected':''}>${type.name}</option>
              </c:forEach>
            </select>
        时间:从:<input type="text" name="begintime" value="${begin}">到<input type="text" name="endtime" value="${end}">
        <input type="submit" value="搜索">
        <input type="button"  onclick="javascript:location.href='/gettypes'"  value="记账">
    </p>
</form>
<table border="1px">
    <tr>
        <td>标题</td>
        <td>记账时间</td>
        <td>类别</td>
        <td>金额</td>
        <td>说明</td>
        <td>操作</td>
    </tr>
    <c:forEach items="${pageInfo.list}" var="bill">
    <tr>
        <td>${bill.title}</td>
        <td><fmt:formatDate value="${bill.billTime}" pattern="yyyy-MM-dd"></fmt:formatDate></td>
        <td>${bill.billType.name}</td>
        <td>${bill.price}</td>
        <td>${bill.explain}</td>
        <td><a href="/deleteBills?id=${bill.id}">删除</a>
            <a href="/findbyid?id=${bill.id}">修改</a>
        </td>
    </tr>
    </c:forEach>
    <tr>
        <td colspan="6">
            <a href="/getbills?typeid=${tid}&begintime=${begin}&endtime=${end}"> 首页 </a>
            <a href="/getbills?pageindex=${pageInfo.prePage==0?1:pageInfo.prePage}&typeid=${tid}&begintime=${begin}&endtime=${end}"> 上一页 </a>
            ${pageInfo.pageNum}
            <a href="/getbills?pageindex=${pageInfo.nextPage==0?pageInfo.pages:pageInfo.nextPage}&typeid=${tid}&begintime=${begin}&endtime=${end}"> 下一页 </a>
            <a href="/getbills?pageindex=${pageInfo.pages}&typeid=${tid}&begintime=${begin}&endtime=${end}"> 尾页 </a>
            总页数:${pageInfo.pages}
            总条数:${pageInfo.total}
            当前页:${pageInfo.pageNum}
        </td>
    </tr>
</table>
</body>
</html>

add.jsp

<%--
  Created by IntelliJ IDEA.
  User: Mr Wei
  Date: 2020/6/11
  Time: 14:14
  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>记账页面(add页面)</title>
</head>
<body>
<h1>记账</h1>
<form action="/addBills" method="post">
    <p>
        类型:
        <c:forEach items="${findtypes}" var="types">
           <input type="radio" value="${types.id}" name="typeId">${types.name}    <!--values值用来存入后台数据库-->
        </c:forEach>
    </p>
    <p>标题:<input type="text" name="title"></p>
    <p>日期:<input type="date" name="billTime">金额:<input type="text" name="price"></p>
    <p>说明:<textarea cols="20" rows="5" name="explain"></textarea></p>
    <p><input type="submit" value="保存"></p>
</form>
</body>
</html>

update.jsp

<%--
  Created by IntelliJ IDEA.
  User: Mr Wei
  Date: 2020/6/11
  Time: 18:00
  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" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>修改页面(update页面)</title>
</head>
<body>
<h1>修改记账</h1>
<form action="/updateBills" method="post">
    <input type="hidden" name="id" value="${findbyid.id}"><!--隐藏域-->
    <p>
        类型:
        <c:forEach items="${findtypes}" var="types">
            <input type="radio" value="${types.id}" name="typeId" ${findbyid.typeId==types.id?'checked':''}>${types.name}    <!--values值用来存入后台数据库-->
        </c:forEach>
    </p>
    <p>标题:<input type="text" name="title" value="${findbyid.title}"></p>
    <p>日期:<input type="text" name="billTime" value="<fmt:formatDate value="${findbyid.billTime}" pattern="yyyy-MM-dd"></fmt:formatDate>">金额:<input type="text" name="price" value="${findbyid.price}"></p>
    <p>说明:<textarea cols="20" rows="5" name="explain">${findbyid.explain}</textarea></p>
    <p><input type="submit" value="更新"></p>
</form>
</body>
</html>

8、启动tomcat测试

showbills页面.png
add页面.png
update页面.png
上一篇下一篇

猜你喜欢

热点阅读