具有SSM框架的CRUD与多条件查询
概述
居于ssm版本的crud跟多添加查询, 并带分页的demo
详细
一、功能展示
部门CRUD:
员工CRUD:
多条件查询与分页:
二、代码结构
三、操作过程
1>下载源码, 使用idea导入
2:>启动tomcat服务器
3>打开浏览器访问
http://localhost:8888/employee/list.do
四、关键代码
员工的表现层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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" ; } } |
员工列表页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | <%@ 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 > |
总配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <? 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 > |
五、其他补充
暂时没有
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?