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">&times;</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 实现增删改查)的更多相关文章

  1. SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis)

    SSM 框架-06-详细整合教程(IDEA版)(Spring+SpringMVC+MyBatis) SSM(Spring.Spring MVC和Mybatis)如果你使用的是 Eclipse,请查看: ...

  2. SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis)

    SSM 框架-05-详细整合教程(Eclipse版)(Spring+SpringMVC+MyBatis) 如果你使用的是 Intellij IDEA,请查看: SSM的配置流程详细的写了出来,方便很少 ...

  3. SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)

    初学java EE,虽然知道使用框架会使开发更加便捷高效,但是对于初学者来说,感到使用框架比较迷惑,尤其是各种jar包的引用.各种框架的配置.注解的使用等等. 最好的学习方法就是实践,于是下载了一个现 ...

  4. 记录-项目java项目框架搭建的一些问题(maven+spring+springmvc+mybatis)

    伴随着项目框架的落成后,本以为启动就能成功的,but.... 项目启动开始报错误1:java.lang.ClassNotFoundException: org.springframework.web. ...

  5. Spring+SpringMVC+Mybatis(SSM)框架集成搭建

    Spring+SpringMVC+Mybatis框架集成搭建教程 一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以 ...

  6. Spring+SpringMvc+Mybatis框架集成搭建教程

    一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...

  7. 第三百零七节,Django框架,models.py模块,数据库操作——表类容的增删改查

    Django框架,models.py模块,数据库操作——表类容的增删改查 增加数据 create()方法,增加数据 save()方法,写入数据 第一种方式 表类名称(字段=值) 需要save()方法, ...

  8. 五 Django框架,models.py模块,数据库操作——表类容的增删改查

    Django框架,models.py模块,数据库操作——表类容的增删改查 增加数据 create()方法,增加数据 save()方法,写入数据 第一种方式 表类名称(字段=值) 需要save()方法, ...

  9. 10天学会phpWeChat——第九天:数据库增、删、改、查(CRUD)操作

    数据库的操作(CRUD)是一个现代化计算机软件的核心,尤其针对web应用软件.虽然在前面的几讲里,我们针对数据库操作大致有了一些了解,但今天我们需要再次强化下. 除了新瓶装老酒,我们今天还引入一个新的 ...

随机推荐

  1. 实例化类对象及类的属性set方法使用不当

    类的属性中set方法操作数据库,新建类对象并给其赋值时总会触发该set方法,而导致不期望的错乱: 库位类Storage,其中传感器状态SensorStatus和逻辑状态LogicStatus有一定的关 ...

  2. JUC知识点总结(知识点见内部目录)

    目录 JUC是什么 锁 Synchronized VS Lock 实现差异 Synchronized & Lock 总结 Synchronized锁的对象是什么 生产者&消费者 只有两 ...

  3. gitee 的使用

    Git安装教程(windows)   Git是当今最流行的版本控制软件,它包含了许多高级工具,这里小编就讲一下Git的安装. 下载地址:https://git-scm.com/downloads 首先 ...

  4. ArcMap线上均匀生成点

    面中均匀生成点方法: [创建渔网]→[要素转点](或[要素折点转点])→[裁剪](或[按位置选择]→[导出数据]),即可. ArcMap中随机生成点: [随机生成点]工具 最近博主的朋友有均匀布点的需 ...

  5. 单链表上的一系列操作(基于c语言)

    单链表的实现分为两种单链表(其实差别并不是很大):带头结点和不带头结点,分别对应下面图中的上下两种. 链表的每一个结点是由两个域组成:数据域和指针域,分别存放所含数据和下一个结点的地址(这都是很明白的 ...

  6. 6月25日 Django 分页 cookie、session

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  7. springcloud学习03-spring cloud eureka(上)

    对eureka一个大概介绍:https://blog.csdn.net/u010623927/article/details/88762525 这里面有个我做dubbo时的一个理解的错误:服务注册中不 ...

  8. SpringMVC实现文件上传功能

    文件上传 文件上传要求form表单的请求方式必须为post,并且添加属性enctype="multipart/form-data" SpringMVC中将上传的文件封装到Multi ...

  9. iOS全埋点解决方案-应用退出和启动

    前言 ​ 通过应用程序退出事件,可以分析应用程序的平均使用时长:通过应用程序的启动事件,可以分析日活和新增.我们可以通过全埋点方式 SDK 实现应用程序的退出和启动事件. 一.全埋点的简介 ​ 目前. ...

  10. babel和polyfill?

    Babel: Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转为 ES5 代码.注意:Babel 默认只转换新的 JavaScript 句法(syntax),而不转换新的 API ...