SpringBoot员工管理系统(整合Mybatis+mysql)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot员工管理系统(整合Mybatis+mysql)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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.pojo3.數據庫構建文件
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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 爬取链家北京租房数据并做简单分析
- 下一篇: 使用Nero 7 刻录DVD镜像文件(L