本文是使用SpringBoot完成CRUP操作 源码在这里:
pom.xml文件信息
<?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.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kuang</groupId> <artifactId>springboot-webapp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-webapp</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- 数据层 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--webjars--> <!--<dependency>--> <!--<groupId>org.webjars</groupId>--> <!--<artifactId>jquery</artifactId>--> <!--<version>3.4.1</version>--> <!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
实体类
package com.kuang.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; @Data @AllArgsConstructor @NoArgsConstructor public class Department { private Integer id; private String departmentName; }
package com.kuang.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor public class Employee { private Integer id; private String lastName; private String email; //1 male, 0 female private Integer gender; private Integer department; private Date birth; private Department eDepartment; }
数据反问层:
<?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.mapper.DepartmentMapper"> <select id="getDepartments" resultType="Department"> select * from department; </select> <select id="getDepartment" resultType="Department" parameterType="int"> select * from department where id = #{id}; </select> </mapper>
package com.kuang.mapper; import com.kuang.pojo.Department; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; //@Mapper : 表示本类是一个 MyBatis 的 Mapper @Mapper @Repository public interface DepartmentMapper { // 获取所有部门信息 List<Department> getDepartments(); // 通过id获得部门 Department getDepartment(Integer id); }
上面的代码应该很熟了吧!
<?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.mapper.EmployeeMapper"> <resultMap id="EmployeeMap" type="Employee"> <id property="id" column="eid"/> <result property="lastName" column="last_name"/> <result property="email" column="email"/> <result property="gender" column="gender"/> <result property="birth" column="birth"/> <association property="eDepartment" javaType="Department"> <id property="id" column="did"/> <result property="departmentName" column="dname"/> </association> </resultMap> <select id="getEmployees" resultMap="EmployeeMap"> select e.id as eid,last_name,email,gender,birth,d.id as did,d.department_name as dname from department d,employee e where d.id = e.department </select> <insert id="save" parameterType="Employee"> insert into employee (last_name,email,gender,department,birth) values (#{lastName},#{email},#{gender},#{department},#{birth}); </insert> <update id="update" parameterType="Employee"> update employee set last_name = #{lastName},email=#{email},gender=#{gender},department=#{department},birth=#{birth} where id = #{id} ; </update> <select id="get" resultType="Employee"> select * from employee where id = #{id} </select> <delete id="delete" parameterType="int"> delete from employee where id = #{id} </delete> </mapper>
package com.kuang.mapper; import com.kuang.pojo.Employee; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; //@Mapper : 表示本类是一个 MyBatis 的 Mapper @Mapper @Repository public interface EmployeeMapper { // 获取所有员工信息 List<Employee> getEmployees(); // 新增一个员工 int save(Employee employee); // 修改员工信息 int update(Employee employee); // 通过id获得员工信息 Employee get(Integer id); // 通过id删除员工 int delete(Integer id); }
控制层:
package com.kuang.controller; import com.kuang.mapper.DepartmentMapper; import com.kuang.mapper.EmployeeMapper; import com.kuang.pojo.Department; import com.kuang.pojo.Employee; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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 java.util.List; @Controller public class EmployeeController { @Autowired EmployeeMapper employeeMapper; @Autowired DepartmentMapper departmentMapper; //查询所有员工,返回列表页面 @GetMapping("/emps") public String list(Model model){ List<Employee> employees = employeeMapper.getEmployees(); //将结果放在请求中 model.addAttribute("emps",employees); return "emp/list"; } //to员工添加页面 @GetMapping("/emp") public String toAddPage(Model model){ //查出所有的部门,提供选择 List<Department> departments = departmentMapper.getDepartments(); model.addAttribute("departments",departments); return "emp/add"; } //员工添加功能 //接收前端传递的参数,自动封装成为对象[要求前端传递的参数名,和属性名一致] @PostMapping("/emp") public String addEmp(Employee employee){ System.out.println(employee); employeeMapper.save(employee); //保存员工信息 //回到员工列表页面,可以使用redirect或者forward return "redirect:/emps"; } //to员工修改页面 @GetMapping("/emp/{id}") public String toUpdateEmp(@PathVariable("id") Integer id,Model model){ //根据id查出来员工 Employee employee = employeeMapper.get(id); System.out.println(employee); //将员工信息返回页面 model.addAttribute("emp",employee); //查出所有的部门,提供修改选择 List<Department> departments = departmentMapper.getDepartments(); model.addAttribute("departments",departments); return "emp/update"; } @PostMapping("/updateEmp") public String updateEmp(Employee employee){ employeeMapper.update(employee); //回到员工列表页面 return "redirect:/emps"; } @GetMapping("/delEmp/{id}") public String delEmp(@PathVariable("id") Integer id){ employeeMapper.delete(id); return "redirect:/emps"; } }
前端页面面的展示模板引擎的使用
查询页面的展示
<div class="container-fluid"> <div class="row"> <div th:replace="~{commons/bar::sitebar(activeUrl='emps')}"></div> <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"> <!--添加员工按钮--> <h2> <a class="btn btn-sm btn-success" href="emp" th:href="@{/emp}">添加员工</a> </h2> <div class="table-responsive"> <table class="table table-striped table-sm"> <thead> <tr> <th>id</th> <th>lastName</th> <th>email</th> <th>gender</th> <th>department</th> <th>birth</th> <!--我们还可以在显示的时候带一些操作按钮--> <th>操作</th> </tr> </thead> <tbody> <tr th:each="emp:${emps}"> <td th:text="${emp.id}"></td> <td>[[${emp.lastName}]]</td> <td th:text="${emp.email}"></td> <td th:text="${emp.gender==0?'女':'男'}"></td> <td th:text="${emp.EDepartment.departmentName}"></td> <!--<td th:text="${emp.birth}"></td>--> <!--使用时间格式化工具--> <td th:text="${#dates.format(emp.birth,'yyyy-MM-dd HH:mm')}"></td> <!--操作--> <td> <a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">编辑</a> <a class="btn btn-sm btn-danger" th:href="@{/delEmp/}+${emp.id}">删除</a> </td> </tr> </tbody> </table> </div> </main> </div> </div>
增加数据页面展示
div class="container-fluid"> <div class="row"> <div th:replace="~{commons/bar::sitebar(activeUrl='emps')}"></div> <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"> <form th:action="@{/emp}" method="post"> <div class="form-group"> <label>LastName</label> <input type="text" class="form-control" name="lastName" placeholder="kuangshen"> </div> <div class="form-group"> <label>Email</label> <input type="email" class="form-control" name="email" placeholder="24736743@qq.com"> </div> <div class="form-group"> <label>Gender</label><br/> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="gender" value="1"> <label class="form-check-label">男</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="gender" value="0"> <label class="form-check-label">女</label> </div> </div> <div class="form-group"> <label>department</label> <!--提交的是部门的ID--> <select class="form-control" name="department"> <option th:each="dept:${departments}" th:text="${dept.departmentName}" th:value="${dept.id}">1</option> </select> </div> <div class="form-group"> <label>Birth</label> <input type="text" class="form-control" name="birth" placeholder="kuangstudy"> </div> <button type="submit" class="btn btn-primary">添加</button> </form> </main> </div> </div>
修改页面数据展示
<!--引入抽取的topbar--> <!--模板名 : 会使用thymeleaf的前后缀配置规则进行解析 使用~{模板::标签名}--> <div th:replace="~{commons/bar::topbar}"></div> <div class="container-fluid"> <div class="row"> <div th:replace="~{commons/bar::sitebar(activeUrl='emps')}"></div> <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"> <form th:action="@{/updateEmp}" method="post"> <input name="id" type="hidden" class="form-control" th:value="${emp.id}"> <div class="form-group"> <label>LastName</label> <input name="lastName" type="text" class="form-control" th:value="${emp.lastName}"> </div> <div class="form-group"> <label>Email</label> <input name="email" type="email" class="form-control" th:value="${emp.email}"> </div> <div class="form-group"> <label>Gender</label><br/> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp.gender==1}"> <label class="form-check-label">男</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="gender" value="0" th:checked="${emp.gender==0}"> <label class="form-check-label">女</label> </div> </div> <div class="form-group"> <label>department</label> <!--提交的是部门的ID--> <select class="form-control" name="department"> <option th:selected="${dept.id == emp.department}" th:each="dept:${departments}" th:text="${dept.departmentName}" th:value="${dept.id}">1 </option> </select> </div> <div class="form-group"> <label>Birth</label> <input name="birth" type="text" class="form-control" th:value="${#dates.format(emp.birth,'yyyy-MM-dd HH:mm')}"> </div> <button type="submit" class="btn btn-primary">修改</button> </form> </main> </div> </div>