当前位置:   article > 正文

SpringBoot整合MyBatis最新_org.mybatis.spring.boot新的版本

org.mybatis.spring.boot新的版本

介绍:这里通过SpringBoot整合MyBatis做了对一张表的简单的增删查改的一个案例,这里不过多讲解,有问题请评论,我看到会回答。

一、运行环境

1、IDEA 2020年的3.3版本

2、SpringBoot 2.3.3

3、MySQL 5.7

4、navicat 12

二、数据库表

【employees】

这里的两个时间类型是timestamp,更改数据会默认插入当前时间

三、使用IDEA的向导Spring Initializr创建项目,添加web启动器和lombok插件

点击next完成。。。

四、pom文件导入依赖

 

  1. <!-- MyBatis-Spring启动器 -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>2.1.3</version>
  6. </dependency>
  7. <!-- MySQL驱动 -->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <version>5.1.47</version>
  12. <scope>runtime</scope>
  13. </dependency>
  14. <!--devtools热部署-->
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-devtools</artifactId>
  18. <optional>true</optional>
  19. <scope>runtime</scope>
  20. </dependency>
  21. <build>
  22. <plugins>
  23. <plugin>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-maven-plugin</artifactId>
  26. <configuration>
  27. <fork>true</fork>
  28. </configuration>
  29. </plugin>
  30. </plugins>
  31. <!-- 资源过滤 -->
  32. <resources>
  33. <resource>
  34. <directory>src/main/java</directory>
  35. <includes>
  36. <include>**/*.xml</include>
  37. </includes>
  38. <filtering>true</filtering>
  39. </resource>
  40. </resources>
  41. </build>

这里我加了资源过滤和热部署的配置。

五、resources中添加配置文件

 

(1)application.yml

  1. spring:
  2. profiles:
  3. active: dev
  4. devtools:
  5. restart:
  6. enabled: true #设置开启热部署
  7. additional-paths: src/main/java #重启目录
  8. exclude: WEB-INF/**
  9. freemarker:
  10. cache: false #页面不加载缓存,修改即时生效

 

(2)application-dev.yml

 

  1. spring:
  2. datasource:
  3. username: root
  4. password: 1234
  5. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&zeroDateTimeBehavior=convertToNull
  6. driver-class-name: com.mysql.jdbc.Driver
  7. mybatis:
  8. mapper-locations: classpath:com/ph/springboot/springbootmybatis/dao/*.xml
  9. #showSql
  10. logging:
  11. level:
  12. com:
  13. example:
  14. mapper : debug

六、java代码

【实体类】

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Employee {
  5. /**
  6. * 员工Id
  7. */
  8. private Integer empId;
  9. /**
  10. * 员工名字
  11. */
  12. private String empName;
  13. /**
  14. * 员工性别
  15. */
  16. private String empGender;
  17. /**
  18. * 部门id
  19. */
  20. private Integer deptId;
  21. /**
  22. * 员工创建时间
  23. */
  24. private Date createDate;
  25. /**
  26. * 员工修改时间
  27. */
  28. private Date updateDate;
  29. }

 

【dao层】

EmployeeMapper接口

 

  1. @Repository
  2. public interface EmployeeMapper {
  3. /**
  4. * 添加一个员工
  5. * @param employee 员工对象
  6. * @return 更新行数
  7. */
  8. int insertEmployee(@Param("employee") Employee employee);
  9. /**
  10. * 根据名字删除一个员工
  11. * @param empName 员工名字
  12. * @return 更新行数
  13. */
  14. int deleteEmployee(String empName);
  15. /**
  16. * 根据员工id修改一个员工
  17. * @param employee 员工对象
  18. * @return 更新行数
  19. */
  20. int updateEmployee(@Param("employee") Employee employee);
  21. /**
  22. * 查找全部员工
  23. * @return 员工集合
  24. */
  25. List<Employee> selectUserList();
  26. }

EmployeeMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.ph.springboot.springbootmybatis.dao.EmployeeMapper">
  6. <insert id="insertEmployee">
  7. insert into employees(emp_name,emp_gender,dept_id)
  8. values(#{employee.empName},#{employee.empGender},#{employee.deptId});
  9. </insert>
  10. <delete id="deleteEmployee">
  11. delete from employees where emp_name=#{empName};
  12. </delete>
  13. <update id="updateEmployee">
  14. update employees
  15. <set>
  16. <if test="employee.empName!=null and employee.empName!=''">
  17. emp_name=#{employee.empName}
  18. </if>
  19. <if test="employee.empGender and employee.empGender!=''">
  20. ,emp_gender=#{employee.empGender}
  21. </if>
  22. <if test="employee.deptId!=null">
  23. ,dept_id=#{employee.deptId}
  24. </if>
  25. where emp_id=#{employee.empId};
  26. </set>
  27. </update>
  28. <resultMap id="empMap" type="com.ph.springboot.springbootmybatis.entity.Employee">
  29. <result column="emp_id" property="empId"/>
  30. <result column="emp_name" property="empName"/>
  31. <result column="emp_gender" property="empGender"/>
  32. <result column="dept_id" property="deptId"/>
  33. <result column="create_date" property="createDate"/>
  34. <result column="update_date" property="updateDate"/>
  35. </resultMap>
  36. <select id="selectUserList" resultMap="empMap">
  37. select emp_id,emp_name,emp_gender,dept_id,create_date,update_date
  38. from employees;
  39. </select>
  40. </mapper>

【service层】

  1. public interface EmployeeService {
  2. /**
  3. * 添加一个员工
  4. * @param employee 员工对象
  5. * @return 更新行数
  6. */
  7. int addEmployee(@Param("employee") Employee employee);
  8. /**
  9. * 根据名字删除一个员工
  10. * @param empName 员工名字
  11. * @return 更新行数
  12. */
  13. int removeEmployee(String empName);
  14. /**
  15. * 根据员工id修改一个员工
  16. * @param employee 员工对象
  17. * @return 更新行数
  18. */
  19. int replaceEmployee(@Param("employee") Employee employee);
  20. /**
  21. * 查找全部员工
  22. * @return 员工集合
  23. */
  24. List<Employee> findUserList();
  25. }
  1. @Service
  2. public class EmployeeServiceImpl implements EmployeeService {
  3. @Resource
  4. private EmployeeMapper employeeMapper;
  5. @Override
  6. public int addEmployee(Employee employee) {
  7. return employeeMapper.insertEmployee(employee);
  8. }
  9. @Override
  10. public int removeEmployee(String empName) {
  11. return employeeMapper.deleteEmployee(empName);
  12. }
  13. @Override
  14. public int replaceEmployee(Employee employee) {
  15. return employeeMapper.updateEmployee(employee);
  16. }
  17. @Override
  18. public List<Employee> findUserList() {
  19. return employeeMapper.selectUserList();
  20. }
  21. }

【controller】

 

  1. @RestController
  2. public class EmployeeController {
  3. @Resource
  4. private EmployeeService employeeServiceImpl;
  5. @RequestMapping("/addEmp")
  6. public String addEmployee(Employee employee){
  7. int i = employeeServiceImpl.addEmployee(employee);
  8. if(i>0){
  9. return "<h1>成功添加一条员工数据!</h1>";
  10. }
  11. return "<h1>添加员工数据失败!</h1>";
  12. }
  13. @RequestMapping("/delEmp")
  14. public String delEmployee(String empName){
  15. int i = employeeServiceImpl.removeEmployee(empName);
  16. if (i>0){
  17. return "<h1>成功删除一条员工数据!</h1>";
  18. }
  19. return "<h1>删除员工数据失败!</h1>";
  20. }
  21. @RequestMapping("/updEmp")
  22. public String updEmployee(Employee employee){
  23. int i = employeeServiceImpl.replaceEmployee(employee);
  24. if (i>0){
  25. return "<h1>成功修改一条员工数据!</h1>";
  26. }
  27. return "<h1>修改员工数据失败!</h1>";
  28. }
  29. @RequestMapping("/getAllEmp")
  30. public List<Employee> getAllEmployee(){
  31. List<Employee> empList = employeeServiceImpl.findUserList();
  32. return empList;
  33. }
  34. }

五、项目结构如图

六、测试地址

增: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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/498199
推荐阅读
相关标签
  

闽ICP备14008679号