概述

居于ssm版本的crud跟多添加查询, 并带分页的demo

详细

一、功能展示

部门CRUD:

员工CRUD:

多条件查询与分页:

二、代码结构

三、操作过程

1>下载源码, 使用idea导入

2:>启动tomcat服务器

3>打开浏览器访问

http://localhost:8888/employee/list.do

四、关键代码

员工的表现层

package com.langfeiyes.ssm.web.controller;

import com.langfeiyes.ssm.domain.Employee;
import com.langfeiyes.ssm.query.EmployeeQueryObject;
import com.langfeiyes.ssm.query.QueryObject;
import com.langfeiyes.ssm.service.IDepartmentService;
import com.langfeiyes.ssm.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("employee")
public class EmployeeController {
@Autowired
private IEmployeeService employeeService; @Autowired
private IDepartmentService departmentService; @RequestMapping("list")
public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) throws Exception{ model.addAttribute("result", employeeService.query(qo));
model.addAttribute("currentMenu", "employee");
model.addAttribute("depts", departmentService.list()); return "employee/list";
} @RequestMapping("input")
public String input(Long id, Model model) throws Exception{ if(id != null){
model.addAttribute("entity", employeeService.get(id));
} model.addAttribute("depts", departmentService.list());
model.addAttribute("currentMenu", "employee");
return "employee/input";
} @RequestMapping("saveOrUpdate")
public String saveOrUpdate(Employee entity) throws Exception{ if(entity.getId() != null){
employeeService.update(entity);
}else{
employeeService.save(entity);
}
return "redirect:/employee/list.do";
} @RequestMapping("delete")
public String input(Long id) throws Exception{
if(id != null){
employeeService.delete(id);
}
return "redirect:/employee/list.do";
}
}

员工列表页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%@include file="/WEB-INF/views/common/header.jsp"%>
<style>
.page-head-line {
font-size: 30px;
text-transform: uppercase;
color: #337ab7;
font-weight: 800;
padding-bottom: 20px;
border-bottom: 2px solid #00a7ff;
margin-bottom: 10px;
}
</style>
<script type="text/javascript">
$(function(){
$("#pagination").twbsPagination({
totalPages:${result.totalPage},
visiblePages:${result.pageSize},
startPage:${qo.currentPage},
first:"首页",
prev:"上一页",
next:"下一页",
last:"尾页",
onPageClick:function(event,page){
$("#currentPage").val(page);
$("#searchForm").submit();
}
}); $("#query").click(function(){
$("#currentPage").val(1);
$("#searchForm").submit();
}); $("#cancel").click(function () {
$("#dept").val("-1");
$("#keyword").val("");
$("#currentPage").val(1);
$("#searchForm").submit();
});
});
</script>
</head>
<body> <div class="container " style="margin-top: 20px">
<div class="row">
<div class="col-sm-3">
<%@include file="/WEB-INF/views/common/menu.jsp"%>
</div>
<div class="col-sm-9">
<div class="row">
<div class="col-sm-12">
<h1 class="page-head-line">员工管理</h1>
</div>
</div> <!--高级查询--->
<form class="form-inline" id="searchForm" action="/employee/list.do" method="post">
<input type="hidden" name="currentPage" id="currentPage" value="${qo.currentPage}">
<input type="hidden" name="pageSize" id="pageSize" value="${qo.pageSize}">
<div class="form-group">
<label for="keyword">关键字:</label>
<input type="text" class="form-control" id="keyword" name="keyword" placeholder="请输入姓名/邮箱" value="${qo.keyword}">
</div>
<div class="form-group">
<label for="dept">部门:</label>
<select class="form-control" id="dept" name="deptId">
<option value="-1">全部</option>
<c:forEach items="${depts}" var="d">
<option value="${d.id}" ${qo.deptId == d.id? 'selected':''}>${d.name}</option>
</c:forEach>
</select>
</div> <button type="button" id="query" class="btn btn-default">查询</button>
<button type="button" id="cancel" class="btn btn-default" >重置</button> <a class="btn btn-success" href="/employee/input.do">
<span class="glyphicon glyphicon-plus"></span>添加
</a> </form> <table class="table table-striped table-hover" >
<thead>
<tr>
<th>编号</th>
<th>名称</th>
<%--<th>密码</th>--%>
<th>email</th>
<th>年龄</th>
<th>部门</th>
<th>操作</th>
</tr>
</thead>
<c:forEach items="${result.list}" var="e" varStatus="vs">
<tr>
<td>${vs.count}</td>
<td>${e.name}</td>
<%--<td>${e.password}</td>--%>
<td>${e.email}</td>
<td>${e.age}</td>
<td>${e.dept.name}</td>
<td>
<a class="btn btn-info btn-xs" href="/employee/input.do?id=${e.id}">
<span class="glyphicon glyphicon-pencil"></span>编辑
</a>
<a href="/employee/delete.do?id=${e.id}" class="btn btn-danger btn-xs" >
<span class="glyphicon glyphicon-trash"></span>删除
</a>
</td>
</tr>
</c:forEach>
</table>
<div style="text-align: center;">
<ul id="pagination" class="pagination"></ul>
</div>
</div>
</div>
</div>
</body>
</html>

总配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- -1: 配置扫描包 -->
<context:component-scan base-package="com.langfeiyes.ssm"/> <!--0:配置数据源-->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--1:配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--1.1:配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--1.2:配置mybatis.xml配置文件-->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!--1.3:配置mapper配置文件-->
<property name="mapperLocations" value="classpath:com/langfeiyes/ssm/mapper/*Mapper.xml"/>
<!--1.4:配置别名-->
<property name="typeAliasesPackage" value="com.langfeiyes.ssm.domain"/>
</bean> <!---2:配置mapper接口实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.langfeiyes.ssm.mapper"/>
</bean> <!--3:配置事务-->
<!--3w: who what when : I has dinner last night -->
<!--3.1:what 什么增强-->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!--3.2:when-->
<tx:advice id="txAdivce" transaction-manager="txManger">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="check*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!--3.3:who-->
<aop:config>
<aop:pointcut id="pc" expression="execution( * com.langfeiyes.ssm.service.*Service.*(..))"/>
<aop:advisor advice-ref="txAdivce" pointcut-ref="pc"/>
</aop:config>
</beans>

五、其他补充

暂时没有

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

最新文章

  1. webpack搭建前端一条龙服务
  2. unp TCP 客户端服务器回射程序中对SIGCHLD信号的处理
  3. Uva1515 Pool construction
  4. 会写网页 就会写手机APP -- Hybrid Mobile Apps for ASP.NET Developers
  5. HDU 1016 Prime Ring Problem
  6. HttpWebRequest代理访问网站
  7. [转]SqlPlus安装配置
  8. Unity3D之空间转换学习笔记(三):3D数学
  9. cocos2d-x 2.1.4学习笔记之HelloWorld分析
  10. 【转】.NET开发者必备的11款免费工具
  11. localhost 与 127.0.0.1 的区别
  12. 【转】10分钟了解设计模式(C#)
  13. javascript收集整理
  14. Fedora Linux 下安装配置C开发环境Code::Blocks
  15. angular2入门,就这一篇就够了
  16. awk:快速入门(简单实用19例+鸟哥书内容)
  17. SSD垃圾回收
  18. java常用设计模式三:原型模式
  19. 管道和FIFO 一
  20. ZT linux的mount(挂载)命令详解

热门文章

  1. 实用在线小工具 -- Google URL Shortener
  2. Codeforces Round #300 A. Cutting Banner 水题
  3. 《pyhton学习手册》 第33章 异常编码细节
  4. MySQL5.7添加授权账号及修改默认端口
  5. eclipse.ini 文件使用说明
  6. Appium+python自动化11-adb必知必会的几个指令
  7. java集合(ArrayList,Vector,LinkedList,HashSet,TreeSet的功能详解)
  8. jquery获取table指定行和列的数据(当前选中行、列)
  9. jquery的功能函数
  10. vc预处理