????該項(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