Springboot+thymeleaf整合
简单功能实现(增删改查)
功能分析方法参考https://www.cnblogs.com/djhzzl/p/14087746.html
访问,代码运行实现效果:
http://localhost:8080/admin
点击查询
换部门查询
添加操作
删除操作
点击即进行删除
编辑操作
代码部分:
开发工具:IDEA 2019.2
数据库:Mysql 5.7
在Mysql中创建表
-
employee表
CREATE TABLE `employee` ( `employee_id` int(11) NOT NULL AUTO_INCREMENT, `employee_name` varchar(255) DEFAULT NULL, `employee_age` int(11) DEFAULT NULL, `department_id` int(11) DEFAULT NULL, PRIMARY KEY (`employee_id`) ) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;employee表
-
department表
CREATE TABLE `department` ( `department_id` int(11) NOT NULL AUTO_INCREMENT, `department_name` varchar(255) DEFAULT NULL, PRIMARY KEY (`department_id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;department表
demo项目:
-
配置yml文件
spring: datasource: url: jdbc:mysql://localhost:3306/employee?serverTimezone=UTC username: root password: admin mybatis: mapper-locations: mapper/*.xml type-aliases-package: com.djh.demo.beanapplication.yml
-
配置pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.djh</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>pom.xm
实体类(pojo):
生成get\set方法、重写toString方法
-
Employee.class
package com.djh.demo.bean; /** * @author HP * @data 2020-12-04 */ public class Employee { private Integer employee_id; private String employee_name; private Integer employee_age; private Department department; public Integer getEmployee_id() { return employee_id; } public void setEmployee_id(Integer employee_id) { this.employee_id = employee_id; } public String getEmployee_name() { return employee_name; } public void setEmployee_name(String employee_name) { this.employee_name = employee_name; } public Integer getEmployee_age() { return employee_age; } public void setEmployee_age(Integer employee_age) { this.employee_age = employee_age; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Employee{" + "employee_id=" + employee_id + ", employee_name='" + employee_name + '\'' + ", employee_age=" + employee_age + '}'; } }Employee
-
Department.class
package com.djh.demo.bean; import java.util.List; /** * @author HP * @data 2020-12-04 */ public class Department { private Integer department_id; private String department_name; private List<Employee> employees; public Integer getDepartment_id() { return department_id; } public void setDepartment_id(Integer department_id) { this.department_id = department_id; } public String getDepartment_name() { return department_name; } public void setDepartment_name(String department_name) { this.department_name = department_name; } public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } @Override public String toString() { return "Department{" + "department_id=" + department_id + ", department_name='" + department_name + '\'' + ", employees=" + employees + '}'; } }Department
resources/mapper(Mapper.xml)
编写sql语句,实现增删改查操作
-
Employee1Mapper.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.djh.demo.dao.EmployeeDao"> <resultMap id="employeeMap" type="com.djh.demo.bean.Employee"> <id property="employee_id" column ="employee_id" /> <result property="employee_name" column = "employee_name"/> <result property="employee_age" column = "employee_age"/> <association property="department" javaType="com.djh.demo.bean.Department"> <id property="department_id" column ="department_id" /> <result property="department_name" column="department_name"/> </association> </resultMap> <select id="selectid" resultMap="employeeMap"> SELECT e.employee_id, e.employee_name, e.employee_age, d.department_name, d.department_id FROM employee AS e INNER JOIN department AS d ON e.department_id = d.department_id WHERE e.employee_id = #{id} </select> <select id="selectById" resultType="com.djh.demo.bean.Employee"> select * from employee where department_id = #{id} </select> <insert id="saveEmployee"> insert into employee (employee_name, employee_age, department_id) value (#{employee_name},#{employee_age},#{department_id}) </insert> <delete id="deleteEmployee"> delete from employee where employee_id = #{employee_id} </delete> <update id="updateEmployee"> update employee set employee_name = #{employee_name}, employee_age = #{employee_age}, department_id = #{department_id} where employee_id = #{employee_id} </update> </mapper>EmployeeMapper.xml
-
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.djh.demo.dao.DepartmentDao"> <select id="getall" resultType="com.djh.demo.bean.Department"> select * from department </select> </mapper>DepartmentMapper.xml
Dao层(Mapper)
接口
数据库操作
-
EmployeeDao
package com.djh.demo.dao; import com.djh.demo.bean.Employee; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; /** * @author HP * @data 2020-12-04 */ @Mapper public interface EmployeeDao { Employee selectid(Integer id); List<Employee> selectById( Integer id); void saveEmployee(@Param("employee_name") String employee_name,@Param("employee_age") String employee_age,@Param("department_id") Integer department_id); void deleteEmployee(Integer employee_id); void updateEmployee(@Param("employee_id") Integer employee_id,@Param("employee_name") String employee_name,@Param("employee_age") Integer employee_age,@Param("department_id") Integer department_id ); }EmployeeDao
-
DepartmentDao
package com.djh.demo.dao; import com.djh.demo.bean.Department; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * @author HP * @data 2020-12-04 */ @Mapper public interface DepartmentDao { List<Department> getall (); }DepartmentDao
Service层(Service)
-
Service(实现业务逻辑),面向接口编程 :接口类(多数面向dao)调用Dao方法、 实现类。
-
注解要写在实现类上 不能写到接口上,将这个类给spring管理。
Service接口
-
EmployeeService
package com.djh.demo.service; import com.djh.demo.bean.Employee; import java.util.List; /** * @author HP * @data 2020-12-04 */ public interface EmployeeService { List<Employee> selectById( Integer id); void saveEmployee ( String employee_name,String employee_age,Integer department_id); void deleteEmployee(Integer employee_id); void updateEmployee( Integer employee_id, String employee_name, Integer employee_age,Integer department_id ); }EmployeeService
-
DepartmentService
package com.djh.demo.service; import com.djh.demo.bean.Department; import java.util.List; /** * @author HP * @data 2020-12-04 */ public interface DepartmentService { List<Department> getall (); }DepartmentService
实现对应Service接口(lmpl)
-
EmployeeServicelmpl
package com.djh.demo.service.impl; import com.djh.demo.bean.Employee; import com.djh.demo.dao.EmployeeDao; import com.djh.demo.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author HP * @data 2020-12-04 * 实现类 * */ @Service public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeDao employeeDao; @Override public List<Employee> selectById(Integer id) { return this.employeeDao.selectById(id); } @Override public void saveEmployee(String employee_name, String employee_age, Integer department_id) { employeeDao.saveEmployee(employee_name,employee_age,department_id); } @Override public void deleteEmployee(Integer employee_id) { employeeDao.deleteEmployee(employee_id); } @Override public void updateEmployee(Integer employee_id, String employee_name, Integer employee_age ,Integer department_id ) { employeeDao.updateEmployee(employee_id,employee_name,employee_age,department_id ); } }EmployeeServicelmpl
-
DepartmentServicelmpl
package com.djh.demo.service.impl; import com.djh.demo.bean.Department; import com.djh.demo.dao.DepartmentDao; import com.djh.demo.service.DepartmentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author HP * @data 2020-12-04 */ @Service("DepartmentService") public class DepartmentServicelmpl implements DepartmentService{ @Autowired DepartmentDao departmentDao; @Override public List<Department> getall() { return departmentDao.getall(); } }DepartmentServicelmpl
Controller层
绑定数据、跳转页面、接受用户请求->将数据传到页面
-
EmployeeController
package com.djh.demo.controller; import com.djh.demo.bean.Department; import com.djh.demo.bean.Employee; import com.djh.demo.service.DepartmentService; import com.djh.demo.service.EmployeeService; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * @author HP * @data 2020-12-04 */ @Controller public class EmployeeController { @Autowired private EmployeeService employeeService; @Autowired private DepartmentService departmentService; @GetMapping("/queryEmpByDept") public String queryEmpByDept (ModelMap modelMap,Integer department_id){ List<Employee> employees = employeeService.selectById(department_id); modelMap.put("employees",employees); modelMap.put("department_id",department_id); List<Department> departments = departmentService.getall(); modelMap.put("departments",departments); return "admin"; } @GetMapping("/saveEmployee") public String saveEmployee(ModelMap modelMap ,String employee_name , String employee_age , Integer department_id){ employeeService.saveEmployee(employee_name,employee_age,department_id); List<Department> departments = departmentService.getall(); modelMap.put("departments",departments); return "admin"; } @GetMapping("/deleteEmployee") public String deleteEmployee(Integer employee_id){ employeeService.deleteEmployee(employee_id); return "/admin"; } @GetMapping("/updateEmployee") public String updateEmployee(ModelMap modelMap,Integer employee_id , String employee_name , Integer employee_age ,Integer department_id ){ employeeService.updateEmployee(employee_id,employee_name,employee_age,department_id ); List<Department> departments = departmentService.getall(); modelMap.put("departments",departments); return "/admin"; } }EmployeeController
-
DepartmentController
package com.djh.demo.service.impl; import com.djh.demo.bean.Department; import com.djh.demo.dao.DepartmentDao; import com.djh.demo.service.DepartmentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author HP * @data 2020-12-04 */ @Service("DepartmentService") public class DepartmentServicelmpl implements DepartmentService{ @Autowired DepartmentDao departmentDao; @Override public List<Department> getall() { return departmentDao.getall(); } }DepartmentController
前端HTML界面
admin(后台界面)
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="queryEmpByDept"> <select name="department_id"> <!--/*@thymesVar id="department_id" type=""*/--> <option th:each="department: ${departments}" th:text="${department.department_name}" th:value="${department.department_id}" th:selected="${department.department_id}==${department_id}?true:false"></option> </select> <input type="submit" value="查询"> <a href="/toadd">添加</a> <a href="/toupdate">编辑</a> </form> <table border="1" cellpadding="0" cellspacing="0" width="500"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr th:each="employee : ${employees}"> <td th:text="${employee .employee_id}"></td> <td th:text="${employee .employee_name}"></td> <td th:text="${employee .employee_age}"></td> <td> <a th:href="@{'deleteEmployee'+'?employee_id='+${employee.employee_id}}">删除</a> <a th:href="@{'updateEmployee'+'?employee_id='+${employee.employee_id}}"></a> </td> </tr> </tbody> </table> </body> </html>admin.html
add(增加界面)
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="saveEmployee"> 姓名:<input type="text" name="employee_name" id="employee_name"> <br/> 年龄:<input type="number" name="employee_age" id="employee_age"> <br/> 部门: <select name="department_id"> <option th:each="department : ${departments}" th:value="${department.department_id}" th:text="${department.department_name}"></option> </select><br/> <input type="submit" value="添加"> </form> </body> </html>add.html
update(编辑界面)
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="updateEmployee"> 职员编号 :<input type="text" name="employee_id" id="employee_id"> <br/> 修改姓名 :<input type="text" name="employee_name" id="employee_name"> <br/> 修改年龄 :<input type="text" name="employee_age" id="employee_age"> <br/> 修改部门 : <select name="department_id"> <option th:each="department : ${departments}" th:value="${department.department_id}" th:text="${department.department_name}"></option> </select><br/> <input type="submit" value="修改"> </form> </body> </html>update