SpringBoot员工管理系统(整合Mybatis+mysql)

前部分:https://blog.csdn.net/weixin_43501359/article/details/112714664
成品:https://download.csdn.net/download/weixin_43501359/14928697

文章目录

  • SpringBoot员工管理系统(整合Mybatis+mysql)
    • 1.导入依赖
    • 2.配置数据源(datasource)和Mybatis配置
    • 3.数据库构建文件
    • 4.编写xxxxMapper接口类
    • 5.编写xxxxMapper.xml配置类
    • 6.service层调用dao层
    • 7.Controller层调用Service层

1.导入依赖

pom.xml

         <!--mybaits--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- alibaba的druid数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.9</version></dependency>

2.配置数据源(datasource)和Mybatis配置

application.yaml

server:port: 8089spring:messages:basename: i18n.loginmvc:format:date: yyyy-MM-dddatasource:name: mysql_testtype: com.alibaba.druid.pool.DruidDataSource#druid相关配置druid:#监控统计拦截的filtersfilters: statdriver-class-name: com.mysql.cj.jdbc.Driver#基本属性url: jdbc:mysql://127.0.0.1:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghaiusername: rootpassword: 123456
mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.kuang.pojo

3.数据库构建文件

CREATE DATABASE mydatabase;DROP TABLE IF EXISTS `Employee`;
CREATE TABLE `Employee` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`lastname` varchar(50) NOT NULL DEFAULT '',`email` varchar(50) DEFAULT '',`gander` int DEFAULT NULL,`departmentId` int(10) DEFAULT NULL,`birth` DATE NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;BEGIN;
INSERT INTO `Employee` VALUES (1001, 'AA1', '24734674@qq.com', 1, 101, NOW());
INSERT INTO `Employee` VALUES (1002, 'AA2', '24734674@qq.com', 0, 102, NOW());
INSERT INTO `Employee` VALUES (1003, 'AA3', '24734674@qq.com', 1, 103, NOW());
INSERT INTO `Employee` VALUES (1004, 'AA4', '24734674@qq.com', 0, 104, NOW());
INSERT INTO `Employee` VALUES (1005, 'AA5', '24734674@qq.com', 1, 105, NOW());
COMMIT;DROP TABLE IF EXISTS `Department`;
CREATE TABLE `Department` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`departmentName` varchar(50) DEFAULT '',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;BEGIN;
INSERT INTO `Department` VALUES (101,'教学部');
INSERT INTO `Department` VALUES (102,'市场部');
INSERT INTO `Department` VALUES (103,'教研部');
INSERT INTO `Department` VALUES (104,'运营部');
INSERT INTO `Department` VALUES (105,'后勤部');
COMMIT;

4.编写xxxxMapper接口类

EmployeeMapper.java

package com.kuang.dao;import com.kuang.pojo.Employee;
import org.apache.ibatis.annotations.Mapper;import java.util.Collection;@Mapper
public interface EmployeeMapper {//增加一位员工void add(Employee employee);//更新员工信息void update(Employee employee);//查询全部员工Collection<Employee> getAll();//通过ID查询员工Employee getEmployeeById(Integer id);//通过ID删除一位员工void deleteEmployeeById(Integer id);
}

DepartmentMapper.java

package com.kuang.dao;import com.kuang.pojo.Department;
import org.apache.ibatis.annotations.Mapper;import java.util.Collection;@Mapper
public interface DepartmentMapper {//获取所有部门Collection<Department> getDepartments();//通过ID获取部门Department getDepartmentById(Integer id);
}

5.编写xxxxMapper.xml配置类

EmployeeMapper.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.kuang.dao.EmployeeMapper" ><sql id="BASE_COLUMN">lastname,email,gander,departmentId,birth</sql><!--×××× 结果映射   --><resultMap id="EmployeeResultMap" type="Employee"><id property="id" column="id"/><result property="lastName" column="lastname"/><result property="email" column="email"/><result property="gander" column="gander"/><result property="department.id" column="departmentId"/><result property="department.departmentName" column="departmentName"/><result property="birth" column="birth"/></resultMap><select id="getAll" resultMap="EmployeeResultMap">SELECT * FROM employee,department WHERE employee.departmentId=department.id</select><insert id="add" parameterType="Employee">INSERT INTO employee<trim prefix="(" suffix=")"><include refid="BASE_COLUMN"/></trim><trim prefix="VALUES(" suffix=")">#{lastName},#{email},#{gander},#{department.id},#{birth}</trim></insert><update id="update" parameterType="Employee">UPDATE employee<set><if test="lastName != null">lastname = #{lastName},</if><if test="email != null">email = #{email},</if><if test="gander != null">gander = #{gander},</if><if test="department.id != null">departmentId = #{department.id},</if><if test="birth != null">birth = #{birth},</if></set><where>id=#{id}</where></update><select id="getEmployeeById" parameterType="int" resultMap="EmployeeResultMap">SELECT * FROM employee WHERE id=#{id}</select><delete id="deleteEmployeeById" parameterType="int">DELETE FROM employee WHERE id=#{id}</delete>
</mapper>

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.kuang.dao.DepartmentMapper" ><select id="getDepartments" resultType="Department">SELECT * FROM department</select><select id="getDepartmentById" resultType="Department" parameterType="int">SELECT * FROM department WHERE id=#{id}</select>
</mapper>

6.service层调用dao层

EmployeeService.java

package com.kuang.service;import com.kuang.dao.EmployeeMapper;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Collection;@Service
public class EmployeeService {@Autowiredprivate EmployeeMapper employeeMapper; //报红不用管//增加一位员工public void add(Employee employee){employeeMapper.add(employee);}//更新员工信息public void update(Employee employee){employeeMapper.update(employee);}//查询全部员工public Collection<Employee> getAll(){return employeeMapper.getAll();}//通过ID查询员工public Employee getEmployeeById(Integer id){return employeeMapper.getEmployeeById(id);}//通过ID删除一位员工public void deleteEmployeeById(Integer id){employeeMapper.deleteEmployeeById(id);}
}

DepartmentService.java

package com.kuang.service;import com.kuang.dao.DepartmentMapper;
import com.kuang.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Collection;@Service
public class DepartmentService {@Autowiredprivate DepartmentMapper departmentMapper;//报红不用管//获取所有部门public Collection<Department> getDepartments(){return departmentMapper.getDepartments();}//通过ID获取部门public Department getDepartmentById(Integer id){return departmentMapper.getDepartmentById(id);}
}

7.Controller层调用Service层

EmployeeController.java

package com.kuang.controller;import com.kuang.dao.DepartmentMapper;
import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import com.kuang.service.DepartmentService;
import com.kuang.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpSession;
import java.util.Collection;@Controller
public class EmployeeController {@Autowiredprivate DepartmentService departmentService;@Autowiredprivate EmployeeService employeeService;@RequestMapping("/emps")public String list(Model model){Collection<Employee> employees = employeeService.getAll();model.addAttribute("emps",employees);return "emp/list";}@GetMapping("/emp")public String toAddpage(Model model){//查出所有部门Collection<Department> departments = departmentService.getDepartments();model.addAttribute("departments",departments);return "emp/add";}@PostMapping("/emp")public String Add(Employee employee){//添加操作System.out.println(employee.toString());employeeService.add(employee);return "redirect:/emps";}@GetMapping("/emp/{id}")public String toUpdate(@PathVariable("id") Integer id,Model model){//查出原来的数据Employee employee = employeeService.getEmployeeById(id);model.addAttribute("emp",employee);//查出所有部门Collection<Department> departments = departmentService.getDepartments();System.out.println(departments.toString());model.addAttribute("departments",departments);return "emp/update";}@PostMapping("/emp/update")public String Update(Employee employee){employeeService.update(employee);return "redirect:/emps";}@RequestMapping("/emp/delete/{id}")public String Delete(@PathVariable("id") Integer id){employeeService.deleteEmployeeById(id);return "redirect:/emps";}@RequestMapping("/out")public String loginOut(HttpSession session){session.removeAttribute("loginUser");return "redirect:/";}
}

SpringBoot员工管理系统(整合Mybatis+mysql)相关推荐

  1. springboot实现快速整合mybatis+mysql

    springboot如今火爆的程度大有将传统的ssm比下去的势头,究其原因,还是springboot被越来越多的公司使用,其简洁的配置,优秀的封装性,便捷的全注解开发,同时由于和越来越多的其他框架进行 ...

  2. 狂神说JAVA SpringBoot员工管理系统

    SpringBoot员工管理系统(没有数据库) 成品:https://download.csdn.net/download/weixin_43501359/14927226 SpringBoot员工管 ...

  3. SpringBoot 实战 (九) | 整合 Mybatis

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,本文通过注解的形式实 ...

  4. java计算机毕业设计酒店员工管理系统源码+mysql数据库+系统+lw文档+部署

    java计算机毕业设计酒店员工管理系统源码+mysql数据库+系统+lw文档+部署 java计算机毕业设计酒店员工管理系统源码+mysql数据库+系统+lw文档+部署 本源码技术栈: 项目架构:B/S ...

  5. 利用IDEA搭建SpringBoot项目,整合mybatis,实现简单的登录功能。

    利用IDEA搭建SpringBoot项目,整合mybatis,实现简单的登录功能. 仅供参考!!! 仅供参考!!! 仅供参考!!! 利用闲余时间想自己搭建一个springboot+mybatis的项目 ...

  6. java计算机毕业设计HTML5企业员工管理系统源码+mysql数据库+系统+lw文档+部署

    java计算机毕业设计HTML5企业员工管理系统源码+mysql数据库+系统+lw文档+部署 java计算机毕业设计HTML5企业员工管理系统源码+mysql数据库+系统+lw文档+部署 本源码技术栈 ...

  7. jsp员工管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

    一.源码特点   jsp 员工管理系统 是一套完善的web设计系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发.开发环境为TOMCAT7.0,M ...

  8. springBoot员工管理系统

    员工管理系统 1.准备工作 静态资源:链接:https://pan.baidu.com/s/1qtUDuJNVupr872kVDO-veg 提取码:fabo gitee:https://gitee.c ...

  9. (附源码)springboot员工管理系统 毕业设计 021430

    员工管理系统的设计与实现 摘 要 由于数据库和数据仓库技术的快速发展,企业员工管理系统建设越来越向模块化.智能化.自我服务和管理科学化的方向发展.员工管理系统对处理对象和服务对象,自身的系统结构,处理 ...

最新文章

  1. 评分卡模型、WOE(Weight of Evidence)和IV(Information Value)
  2. Linux的sysctl 命令参数详解
  3. 介绍一款贼美的Vue+Element开源后台管理UI
  4. mavengradle 依赖指定版本范围或者最新版本
  5. java继承和派生4.4- 4.52020.3.24
  6. C/C++函数调用的压栈模型
  7. 蜗牛星际网卡驱动_矿渣“蜗牛星际“折腾NAS黑群晖全过程
  8. vue跳转页面增加等待_vue跳转页面方法
  9. 请说明Request和Session的生命周期
  10. python classmethod_Python classmethod()
  11. SSI(Server Side Includeds)使用详解(转载)
  12. libmesh 思维导图(类接口设计)
  13. (转)泊松分布和指数分布:10分钟教程
  14. .NET Core 2.1 Preview 2带来网络方面的改进
  15. Java Web J2EE下的两大框架SSH和SSM对比
  16. iPhone配置实用工具介绍
  17. 华为eSight网络监控平台安装
  18. java 进制转换十进制
  19. 知识表示的方法(1)——产生式表示法
  20. 14.css中的定位的参照物

热门文章

  1. Java中Integer.MAX_VALUE/Integer.MIN_VALUE的含义
  2. python 随机选择list或numpy.ndarray中n个元素
  3. 洛谷1373小a和uim之大逃离
  4. 云笔记有什么功能作用,浏览器如何添加云笔记插件
  5. linux leanote云笔记搭建
  6. Uva 11374 - Airport Express(枚举+最短路)
  7. 技术/研发经理介绍和创业的一些感想
  8. COTS应用程序开发框架简介(二)
  9. 優しさの理由 (冰菓op)歌词
  10. 编码通信与魔术初步(四)——通信编码魔术的基本原理