• 具有SSM框架的CRUD与多条件查询


    概述

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

    详细

    一、功能展示

    部门CRUD:

    image.png

    image.png

    员工CRUD:

    image.png

    image.png

    多条件查询与分页:

    image.png

    二、代码结构

    image.pngimage.png

    三、操作过程

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

    image.png

    2:>启动tomcat服务器

    image.png

    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大师发表,拒绝转载,转载需要作者授权

  • 相关阅读:
    谷雨路
    ObjectiveC_2.0大全.pdf iOS 5 Programming Cookbook http://www.devdiv.com
    http://www.120ask.com/zjamk/13/13658784.htm
    iPhone学习系列、代码教程___持续更新中
    30天精通iPhone手机编程
    font
    iPhone开发学习资料汇总
    tools
    http://blog.csdn.net/likendsl
    http://blog.csdn.net/iukey
  • 原文地址:https://www.cnblogs.com/demodashi/p/9453016.html
Copyright © 2020-2023  润新知