SSM框架實(shí)戰(zhàn)(記賬項(xiàng)目)

????該項(xiàng)目主要實(shí)現(xiàn)賬單的增均牢、刪、改才睹、分頁(yè)處理以及模糊查詢徘跪,可以很好的鍛煉我們之前學(xué)的框架知識(shí)。
????首先我們先搭建SSM框架琅攘,如上兩章節(jié)所示垮庐,這里不做過(guò)多闡述。

1坞琴、數(shù)據(jù)庫(kù)

bills表.png

bill_type表.png

2哨查、Bills和BillType實(shí)體類

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實(shí)現(xiàn)類

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 {

    //分頁(yè)+模糊查
    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);

    //根據(jù)主鍵查找
    public Bills findbyid(int sid);
    //根據(jù)主鍵查找進(jìn)行修改
    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接口類的實(shí)現(xiàn)類寒亥,所以配置文件得配置好)

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 {
    //分頁(yè)+模糊查
    public List<Bills> findAll(Map map);
    //查詢類別下拉信息
    public List<BillType> findtypes();
    //記賬(增加)方法
    public int add(Bills bills);
    //刪除方法
    public int delete(int id);

    //根據(jù)主鍵查找
    public Bills findbyid(int sid);
    //根據(jù)主鍵查找進(jìn)行修改
    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>

    <!--根據(jù)主鍵查找-->
    <select id="findbyid" resultMap="r1">
        select * from bills where id=#{sid}
    </select>
    <!--根據(jù)主鍵查找進(jìn)行更新-->
    <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;

       //定義每一頁(yè)有幾行數(shù)據(jù)
       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";
       }

       /**
        * 從數(shù)據(jù)庫(kù)中查詢類別信息,顯示到記賬(add)頁(yè)面中
        * */
       @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();
           }
       }

       /**
        * 根據(jù)主鍵查詢
        * */
       @RequestMapping("/findbyid")
      public String findbyid(int id,ModelMap map){
           //查找類型
           List<BillType> findtypes = billsService.findtypes();
           map.addAttribute("findtypes",findtypes);
           //根據(jù)主鍵查找
           Bills findbyid = billsService.findbyid(id);
           map.addAttribute("findbyid",findbyid);
           return "update";
       }

    /**
     * 根據(jù)主鍵查詢進(jìn)行修改
     * */
    @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忍啤、各個(gè)前端頁(yè)面

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>首頁(yè)</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相當(dāng)于取別名-->
                <option value="${type.id}" ${type.id==tid?'selected':''}>${type.name}</option>
              </c:forEach>
            </select>
        時(shí)間:從:<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>標(biāo)題</td>
        <td>記賬時(shí)間</td>
        <td>類別</td>
        <td>金額</td>
        <td>說(shuō)明</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}"> 首頁(yè) </a>
            <a href="/getbills?pageindex=${pageInfo.prePage==0?1:pageInfo.prePage}&typeid=${tid}&begintime=${begin}&endtime=${end}"> 上一頁(yè) </a>
            ${pageInfo.pageNum}
            <a href="/getbills?pageindex=${pageInfo.nextPage==0?pageInfo.pages:pageInfo.nextPage}&typeid=${tid}&begintime=${begin}&endtime=${end}"> 下一頁(yè) </a>
            <a href="/getbills?pageindex=${pageInfo.pages}&typeid=${tid}&begintime=${begin}&endtime=${end}"> 尾頁(yè) </a>
            總頁(yè)數(shù):${pageInfo.pages}
            總條數(shù):${pageInfo.total}
            當(dāng)前頁(yè):${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>記賬頁(yè)面(add頁(yè)面)</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值用來(lái)存入后臺(tái)數(shù)據(jù)庫(kù)-->
        </c:forEach>
    </p>
    <p>標(biāo)題:<input type="text" name="title"></p>
    <p>日期:<input type="date" name="billTime">金額:<input type="text" name="price"></p>
    <p>說(shuō)明:<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>修改頁(yè)面(update頁(yè)面)</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值用來(lái)存入后臺(tái)數(shù)據(jù)庫(kù)-->
        </c:forEach>
    </p>
    <p>標(biāo)題:<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>說(shuō)明:<textarea cols="20" rows="5" name="explain">${findbyid.explain}</textarea></p>
    <p><input type="submit" value="更新"></p>
</form>
</body>
</html>

8加勤、啟動(dòng)tomcat測(cè)試

showbills頁(yè)面.png

add頁(yè)面.png

update頁(yè)面.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市同波,隨后出現(xiàn)的幾起案子鳄梅,更是在濱河造成了極大的恐慌,老刑警劉巖未檩,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件戴尸,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡冤狡,警方通過(guò)查閱死者的電腦和手機(jī)孙蒙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)筒溃,“玉大人马篮,你說(shuō)我怎么就攤上這事×保” “怎么了浑测?”我有些...
    開封第一講書人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我迁央,道長(zhǎng)掷匠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任岖圈,我火速辦了婚禮讹语,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蜂科。我一直安慰自己顽决,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開白布导匣。 她就那樣靜靜地躺著才菠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪贡定。 梳的紋絲不亂的頭發(fā)上赋访,一...
    開封第一講書人閱讀 52,457評(píng)論 1 311
  • 那天,我揣著相機(jī)與錄音缓待,去河邊找鬼蚓耽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛旋炒,可吹牛的內(nèi)容都是我干的步悠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼国葬,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼贤徒!你這毒婦竟也來(lái)了芹壕?” 一聲冷哼從身側(cè)響起汇四,我...
    開封第一講書人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎踢涌,沒(méi)想到半個(gè)月后通孽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡睁壁,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年背苦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片潘明。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡行剂,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出钳降,到底是詐尸還是另有隱情厚宰,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站铲觉,受9級(jí)特大地震影響澈蝙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撵幽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一灯荧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧盐杂,春花似錦逗载、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至测垛,卻和暖如春捏膨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背食侮。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工号涯, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人锯七。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓链快,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親眉尸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子域蜗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容