赞
踩
1、IDEA 2020年的3.3版本
2、SpringBoot 2.3.3
3、MySQL 5.7
4、navicat 12
【employees】
这里的两个时间类型是timestamp,更改数据会默认插入当前时间
点击next完成。。。
- <!-- MyBatis-Spring启动器 -->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.3</version>
- </dependency>
- <!-- MySQL驱动 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.47</version>
- <scope>runtime</scope>
- </dependency>
- <!--devtools热部署-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <optional>true</optional>
- <scope>runtime</scope>
- </dependency>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <fork>true</fork>
- </configuration>
- </plugin>
- </plugins>
- <!-- 资源过滤 -->
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
这里我加了资源过滤和热部署的配置。
(1)application.yml
- spring:
- profiles:
- active: dev
- devtools:
- restart:
- enabled: true #设置开启热部署
- additional-paths: src/main/java #重启目录
- exclude: WEB-INF/**
- freemarker:
- cache: false #页面不加载缓存,修改即时生效
(2)application-dev.yml
- spring:
- datasource:
- username: root
- password: 1234
- url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&zeroDateTimeBehavior=convertToNull
- driver-class-name: com.mysql.jdbc.Driver
-
- mybatis:
- mapper-locations: classpath:com/ph/springboot/springbootmybatis/dao/*.xml
-
- #showSql
- logging:
- level:
- com:
- example:
- mapper : debug
【实体类】
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Employee {
- /**
- * 员工Id
- */
- private Integer empId;
- /**
- * 员工名字
- */
- private String empName;
- /**
- * 员工性别
- */
- private String empGender;
- /**
- * 部门id
- */
- private Integer deptId;
- /**
- * 员工创建时间
- */
- private Date createDate;
- /**
- * 员工修改时间
- */
- private Date updateDate;
- }
【dao层】
EmployeeMapper接口
- @Repository
- public interface EmployeeMapper {
- /**
- * 添加一个员工
- * @param employee 员工对象
- * @return 更新行数
- */
- int insertEmployee(@Param("employee") Employee employee);
- /**
- * 根据名字删除一个员工
- * @param empName 员工名字
- * @return 更新行数
- */
- int deleteEmployee(String empName);
- /**
- * 根据员工id修改一个员工
- * @param employee 员工对象
- * @return 更新行数
- */
- int updateEmployee(@Param("employee") Employee employee);
- /**
- * 查找全部员工
- * @return 员工集合
- */
- List<Employee> selectUserList();
- }
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.ph.springboot.springbootmybatis.dao.EmployeeMapper">
- <insert id="insertEmployee">
- insert into employees(emp_name,emp_gender,dept_id)
- values(#{employee.empName},#{employee.empGender},#{employee.deptId});
- </insert>
-
- <delete id="deleteEmployee">
- delete from employees where emp_name=#{empName};
- </delete>
-
- <update id="updateEmployee">
- update employees
- <set>
- <if test="employee.empName!=null and employee.empName!=''">
- emp_name=#{employee.empName}
- </if>
- <if test="employee.empGender and employee.empGender!=''">
- ,emp_gender=#{employee.empGender}
- </if>
- <if test="employee.deptId!=null">
- ,dept_id=#{employee.deptId}
- </if>
- where emp_id=#{employee.empId};
- </set>
- </update>
-
- <resultMap id="empMap" type="com.ph.springboot.springbootmybatis.entity.Employee">
- <result column="emp_id" property="empId"/>
- <result column="emp_name" property="empName"/>
- <result column="emp_gender" property="empGender"/>
- <result column="dept_id" property="deptId"/>
- <result column="create_date" property="createDate"/>
- <result column="update_date" property="updateDate"/>
- </resultMap>
- <select id="selectUserList" resultMap="empMap">
- select emp_id,emp_name,emp_gender,dept_id,create_date,update_date
- from employees;
- </select>
- </mapper>
【service层】
- public interface EmployeeService {
- /**
- * 添加一个员工
- * @param employee 员工对象
- * @return 更新行数
- */
- int addEmployee(@Param("employee") Employee employee);
- /**
- * 根据名字删除一个员工
- * @param empName 员工名字
- * @return 更新行数
- */
- int removeEmployee(String empName);
- /**
- * 根据员工id修改一个员工
- * @param employee 员工对象
- * @return 更新行数
- */
- int replaceEmployee(@Param("employee") Employee employee);
- /**
- * 查找全部员工
- * @return 员工集合
- */
- List<Employee> findUserList();
- }
- @Service
- public class EmployeeServiceImpl implements EmployeeService {
- @Resource
- private EmployeeMapper employeeMapper;
-
- @Override
- public int addEmployee(Employee employee) {
- return employeeMapper.insertEmployee(employee);
- }
-
- @Override
- public int removeEmployee(String empName) {
- return employeeMapper.deleteEmployee(empName);
- }
-
- @Override
- public int replaceEmployee(Employee employee) {
- return employeeMapper.updateEmployee(employee);
- }
-
- @Override
- public List<Employee> findUserList() {
- return employeeMapper.selectUserList();
- }
- }
【controller】
- @RestController
- public class EmployeeController {
-
- @Resource
- private EmployeeService employeeServiceImpl;
-
- @RequestMapping("/addEmp")
- public String addEmployee(Employee employee){
- int i = employeeServiceImpl.addEmployee(employee);
- if(i>0){
- return "<h1>成功添加一条员工数据!</h1>";
- }
- return "<h1>添加员工数据失败!</h1>";
- }
- @RequestMapping("/delEmp")
- public String delEmployee(String empName){
- int i = employeeServiceImpl.removeEmployee(empName);
- if (i>0){
- return "<h1>成功删除一条员工数据!</h1>";
- }
- return "<h1>删除员工数据失败!</h1>";
- }
- @RequestMapping("/updEmp")
- public String updEmployee(Employee employee){
- int i = employeeServiceImpl.replaceEmployee(employee);
- if (i>0){
- return "<h1>成功修改一条员工数据!</h1>";
- }
- return "<h1>修改员工数据失败!</h1>";
- }
- @RequestMapping("/getAllEmp")
- public List<Employee> getAllEmployee(){
- List<Employee> empList = employeeServiceImpl.findUserList();
- return empList;
- }
- }
增:http://localhost:8080/addEmp?empName=徐脂虎&empGender=女&deptId=3
删:http://localhost:8080/delEmp?empName=黄阵图
改:http://localhost:8080/updEmp?empGender=%E5%A5%B3&deptId=2&empId=8
查:http://localhost:8080/getAllEmp
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。