ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查
SpringBoot 项目整合
一、项目准备
1.1 ssm 框架环境搭建
博客地址:ssm项目框架搭建
1.2 项目结构图如下

1.3 数据表结构图如下

CREATE TABLE `department` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`sn` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
1.4 运行结果

二、项目实现
1. EmployeeController 控制器
package com.yy.homework.web.controller;
import com.github.pagehelper.PageInfo;
import com.yy.homework.anno.RequiredPermission;
import com.yy.homework.domain.Department;
import com.yy.homework.qo.DepartmentPageObject;
import com.yy.homework.service.IDepartmentService;
import com.yy.homework.service.IDepartmentService;
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("/department")
public class DepartmentController {
@Autowired
private IDepartmentService departmentService;
@RequestMapping("/list")
@RequiredPermission(name = "部门查询", expression = "department:list")
public String selectAll(Model model, @ModelAttribute("qo")DepartmentPageObject qo) {
// Mybatis分页插件 PageHelper
//qo:页面模糊查询传入的值和分页查询传入的当前页和每页条数分装成 DepartmentPageObject 对象
PageInfo<Department> pageInfo = departmentService.selectForList(qo);
model.addAttribute("pageInfo", pageInfo);
return "department/list";
}
@RequestMapping("/input")
@RequiredPermission(name = "部门新增或修改", expression = "department:saveOrUpdate")
public String input(Department department, Model model) {
if (department.getId() != null) {
Department e = departmentService.selectById(department.getId());
model.addAttribute("department", e);
}
return "department/input";
}
@RequestMapping("/saveOrUpdate")
@RequiredPermission(name = "部门新增或修改", expression = "department:saveOrUpdate")
public String saveOrUpdate(Department department) {
if (department.getId() != null) {
departmentService.update(department);
} else {
departmentService.insert(department);
}
return "redirect:/department/list";
}
@RequestMapping("/delete")
@RequiredPermission(name = "部门删除", expression = "department:delete")
public String delete(Long id) {
departmentService.delete(id);
return "redirect:/department/list";
}
}
2. IDepartmentService 接口
package com.yy.homework.service;
import com.github.pagehelper.PageInfo;
import com.yy.homework.domain.Department;
import com.yy.homework.domain.Department;
import com.yy.homework.qo.DepartmentPageObject;
import java.util.List;
public interface IDepartmentService {
void insert(Department department);
void delete(Long id);
void update(Department department);
Department selectById(Long id);
PageInfo<Department> selectForList(DepartmentPageObject qo);
List<Department> selectAll();
}
3. DepartmentServiceImpl 接口实现类
package com.yy.homework.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.homework.domain.Department;
import com.yy.homework.domain.Department;
import com.yy.homework.mapper.DepartmentMapper;
import com.yy.homework.qo.DepartmentPageObject;
import com.yy.homework.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DepartmentServiceImpl implements IDepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
@Override
public void insert(Department department) {
departmentMapper.insert(department);
}
@Override
public void delete(Long id) {
departmentMapper.deleteByPrimaryKey(id);
}
@Override
public void update(Department department) {
departmentMapper.updateByPrimaryKey(department);
}
@Override
public Department selectById(Long id) {
return departmentMapper.selectByPrimaryKey(id);
}
@Override
public PageInfo<Department> selectForList(DepartmentPageObject qo) {
// 使用分页插件,传入当前页,每页显示数量
PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
List<Department> department = departmentMapper.selectForList(qo);
return new PageInfo<>(department);
}
@Override
public List<Department> selectAll() {
return departmentMapper.selectAll();
}
}
4. DepartmentMapper 接口
package com.yy.homework.mapper;
import com.yy.homework.domain.Department;
import com.yy.homework.qo.DepartmentPageObject;
import java.util.List;
public interface DepartmentMapper {
int deleteByPrimaryKey(Long id);
int insert(Department record);
Department selectByPrimaryKey(Long id);
List<Department> selectForList(DepartmentPageObject qo);
int updateByPrimaryKey(Department record);
List<Department> selectAll();
}
5. DepartmentMapper.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" >
<mapper namespace="com.yy.homework.mapper.DepartmentMapper" >
<resultMap id="BaseResultMap" type="com.yy.homework.domain.Department" >
<id column="id" property="id" />
<result column="name" property="name" />
<result column="sn" property="sn" />
</resultMap>
<delete id="deleteByPrimaryKey" >
delete from department
where id = #{id}
</delete>
<insert id="insert" useGeneratedKeys="true" keyProperty="id" >
insert into department (name, sn)
values (#{name}, #{sn})
</insert>
<update id="updateByPrimaryKey" >
update department
set name = #{name},
sn = #{sn}
where id = #{id}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" >
select id, name, sn
from department
where id = #{id}
</select>
<select id="selectForList" resultMap="BaseResultMap" >
select id, name, sn
from department
</select>
<select id="selectAll" resultMap="BaseResultMap">
select id, name, sn
from department
</select>
</mapper>
6. list.ftl 页面(可以不使用 FreeMarker 模板,自己定义)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>部门管理</title>
<!-- 使用相对当前模板文件的路径 再去找另一个模板文件 -->
<#include "/common/link.ftl">
<script>
$(function () {
/* 添加 */
$('.btn-input').click(function () {
let tr = this.parentNode.parentNode;
console.log(tr);
if (tr.childNodes[1].innerText) {
$('input[name=id]').val(tr.childNodes[1].innerText);
$('input[name=name]').val(tr.childNodes[5].innerText);
// console.log($('#exampleInputEmail1').val());
$('input[name=sn]').val(tr.childNodes[7].innerText);
}
$('.modal').modal('show'); //官方文档中表示通过该方法即可弹出模态框
});
/*$('.btn-input').click(function () {
$('.modal').modal('show'); //官方文档中表示通过该方法即可弹出模态框
});*/
});
</script>
</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
<#include "/common/navbar.ftl">
<!--定义一个变量 用于菜单回显-->
<#assign currentMenu="department"/>
<#include "/common/menu.ftl">
<div class="content-wrapper">
<section class="content-header">
<h1>部门管理</h1>
</section>
<section class="content">
<div class="box">
<!--高级查询--->
<form class="form-inline" id="searchForm" action="/department/list" method="post">
<input type="hidden" name="currentPage" id="currentPage" value="1">
<a href="#" class="btn btn-success btn-input" style="margin: 10px">
<span class="glyphicon glyphicon-plus"></span> 添加
</a>
</form>
<!--编写内容-->
<div class="box-body table-responsive ">
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>编号</th>
<th>部门名称</th>
<th>部门缩写</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<#list pageInfo.list as department>
<tr>
<td style="display: none">${department.id}</td>
<td>${department_index + pageInfo.startRow}</td>
<td>${department.name}</td>
<td>${department.sn}</td>
<td>
<a href="#" class="btn btn-info btn-xs btn-input">
<span class="glyphicon glyphicon-pencil"></span> 编辑
</a>
<a class="btn btn-danger btn-xs btn-delete" data-url="/department/delete?id=${department.id}">
<span class="glyphicon glyphicon-trash"></span> 删除
</a>
</td>
</tr>
</#list>
</tbody>
</table>
<!--分页-->
<#include "/common/page.ftl" >
</div>
</div>
</section>
</div>
<#include "/common/footer.ftl" >
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span>
</button>
<h4 class="modal-title">添加/修改部门</h4>
</div>
<form action="/department/saveOrUpdate" method="post">
<input type="hidden" name="id">
<div class="modal-body">
<div class="form-group">
<label for="exampleInputEmail1">部门名称</label>
<input type="text" name="name" class="form-control" id="exampleInputEmail1"
placeholder="部门名称">
</div>
<div class="form-group">
<label for="exampleInputPassword1">部门缩写</label>
<input type="text" name="sn" class="form-control" id="exampleInputPassword1"
placeholder="部门编号">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">保存</button>
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
</body>
</html>
总结
以上就是 ssm 框架实现增删改查的操作了,代码仅供参考,欢迎讨论交流。
SpringBoot 项目搭建详细介绍请看我下一篇博客
博客地址:SpringBoot 项目整合源码
ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)的更多相关文章
- SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis)
SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis) SSM(Spring.Spring MVC和Mybatis)如果你使用的是 Eclipse,请查看: ...
- SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis)
SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis) 如果你使用的是 Intellij IDEA,请查看: SSM的配置流程详细的写了出来,方便很少 ...
- SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
初学java EE,虽然知道使用框架会使开发更加便捷高效,但是对于初学者来说,感到使用框架比较迷惑,尤其是各种jar包的引用.各种框架的配置.注解的使用等等. 最好的学习方法就是实践,于是下载了一个现 ...
- 记录-项目java项目框架搭建的一些问题(maven+spring+springmvc+mybatis)
伴随着项目框架的落成后,本以为启动就能成功的,but.... 项目启动开始报错误1:java.lang.ClassNotFoundException: org.springframework.web. ...
- Spring+SpringMVC+Mybatis(SSM)框架集成搭建
Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- 第三百零七节,Django框架,models.py模块,数据库操作——表类容的增删改查
Django框架,models.py模块,数据库操作——表类容的增删改查 增加数据 create()方法,增加数据 save()方法,写入数据 第一种方式 表类名称(字段=值) 需要save()方法, ...
- 五 Django框架,models.py模块,数据库操作——表类容的增删改查
Django框架,models.py模块,数据库操作——表类容的增删改查 增加数据 create()方法,增加数据 save()方法,写入数据 第一种方式 表类名称(字段=值) 需要save()方法, ...
- 10天学会phpWeChat——第九天:数据库增、删、改、查(CRUD)操作
数据库的操作(CRUD)是一个现代化计算机软件的核心,尤其针对web应用软件.虽然在前面的几讲里,我们针对数据库操作大致有了一些了解,但今天我们需要再次强化下. 除了新瓶装老酒,我们今天还引入一个新的 ...
随机推荐
- SQL从零到迅速精通【实用函数(1)】
语法是一个编程语言的基础,真的想玩的6得飞起还是要靠自己定义的函数和变量. 1.使用DECLARE语句创建int数据类型的名为@mycounter的局部变量,输入语句如下: DECLARE @myco ...
- laravel 框架登录 实际操作
//登录中间件 Route::group(['middleware'=>'checkage'],function (){ Route::get('/mou/list','MouControlle ...
- JS-购物车
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL Performance Schema详解
MySQL的performance schema 用于监控MySQL server在一个较低级别的运行过程中的资源消耗.资源等待等情况. 1 performance schema特点 提供了一种在数据 ...
- VS Code配置Python环境
Visual Studio Code配置Python环境 目录 Visual Studio Code配置Python环境 1.安装Python环境 2.安装VS Code 2.1 下载 2.2 配置中 ...
- 比较 Java 静态工厂方法与构造函数
1 什么是静态工厂方法 Java 静态工厂方法是在方法前加上 public static,让这个方法变为公开.静态的方法.该方法返回该类的一个实例,就好像一个工厂生产出一个产品.所以称之为静态工厂方法 ...
- 解决jira配置gmail邮箱报错
具体报错: AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 ...
- 微信Native支付
微信Native支付对接(扫码) 由于有业务需求对接了微信和paypal支付,这边做个记录 微信支付开发文档:https://pay.weixin.qq.com/wiki/doc/api/index. ...
- ArcGIS Server 禁用/rest/services路径(禁用服务目录)
ArcGIS Server服务目录(路径如:http://<hostname>:6080/arcgis/rest/services)默认可以不需要登陆直接打开.效果如下图. ArcGIS服 ...
- Prim算法求最小生成树
首先在介绍这个算法之前我们要之明确一下什么是最小生成树的概念: 由 V 中的全部 n 个顶点和 E 中 n−1 条边构成的无向连通子图被称为 G 的一棵生成树,其中边的权值之和最小的生成树被称为无向图 ...