springBoot学习笔记(2)—— 整合mybatis

一、引入jar包和配置文件

1. 引入jar包

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.mybatis.spring.boot</groupId>
  8. <artifactId>mybatis-spring-boot-starter</artifactId>
  9. <version>1.3.1</version>
  10. </dependency>

2.完整pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>springboot-mybatis</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot-mybatis</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>mysql</groupId>
  30. <artifactId>mysql-connector-java</artifactId>
  31. <scope>runtime</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.mybatis.spring.boot</groupId>
  35. <artifactId>mybatis-spring-boot-starter</artifactId>
  36. <version>1.3.1</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. <optional>true</optional>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. <configuration>
  55. <excludes>
  56. <exclude>
  57. <groupId>org.projectlombok</groupId>
  58. <artifactId>lombok</artifactId>
  59. </exclude>
  60. </excludes>
  61. </configuration>
  62. </plugin>
  63. </plugins>
  64. </build>
  65. </project>

3. application.yml配置文件内容

  1. spring:
  2. datasource:
  3. # 数据源基本配置
  4. username: root
  5. password: 123456
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. url: jdbc:mysql://101.34.49.127:3306/springBootAll?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
  8. mybatis:
  9. mapper-locations: classpath:mybatis/mapper/*.xml #配置mapper文件的映射位置
  10. server:
  11. port: 8082 # 配置项目启动端口

二、具体代码内容

1.创建表格

  1. DROP TABLE IF EXISTS `user`;
  2. CREATE TABLE `user` (
  3. `id` int NOT NULL,
  4. `user_name` varchar(255) NOT NULL,
  5. `idCardNumber` varchar(255) NOT NULL,
  6. `phone_number` varchar(255) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

2.创建实体类

  1. @Data
  2. public class User {
  3. /**
  4. * 主键id
  5. */
  6. private Long id;
  7. /**
  8. * 用户名
  9. */
  10. private String userName;
  11. /**
  12. * 身份证号码
  13. */
  14. private String idCardNumber;
  15. /**
  16. * 手机号码
  17. */
  18. private String phoneNumber;
  19. }

说明:

  1. 此处的@Data注解使用的是lombok需要下载lombok插件才不会报错,作用是在编译项目是自动生成get,set方法。

3.Controller控制层方法

  1. @RestController
  2. @RequestMapping("userController")
  3. public class UserController {
  4. @Resource
  5. UserService userService;
  6. /***
  7. * description: 通过用户id查询用户数据集
  8. * version: 1.0 ->
  9. * date: 2021/12/28 14:42
  10. * author: xiaYZ
  11. * iteration: 迭代说明
  12. * @param userId
  13. * @return com.example.springbootmybatis.entity.User
  14. */
  15. @ResponseBody
  16. @RequestMapping("findUserById")
  17. public String findUserById(Long userId){
  18. User user = new User();
  19. try{
  20. user = userService.findUserById(userId);
  21. }catch (Exception e){
  22. e.printStackTrace();
  23. System.out.println("查询用户错误");
  24. return "查询用户错误";
  25. }
  26. return user.toString();
  27. }
  28. }

4.service服务层方法

  1. @Service
  2. @Mapper
  3. public interface UserDao {
  4. /**
  5. * description: 通过用户id查询用户信息
  6. * version: 1.0
  7. * date: 2021/12/28 14:36
  8. * author: xiaYZ
  9. * iteration: 迭代说明
  10. * @param userId
  11. * @return
  12. */
  13. User findUserById(Long userId);
  14. }

5.dao持久层方法

  1. @Mapper
  2. public interface UserDao {
  3. /**
  4. * description: 通过用户id查询用户信息
  5. * version: 1.0
  6. * date: 2021/12/28 14:36
  7. * author: xiaYZ
  8. * iteration: 迭代说明
  9. * @param userId
  10. * @return
  11. */
  12. User findUserById(Long userId);
  13. }

说明:

  1. @Mapper注解是说明此类为一个持久层接口,如果没有这个注解的话SpringBoot项目会扫码不到接口方法报错
  2. 当然你也可以选择在SpringBoot项目的main方法上面加一个@MapperScan(value = “com.example.springbootmybatis.dao”),两个方法任选其一即可。

6.mapper方法

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.springbootmybatis.dao.UserDao">
  4. <select id="findUserById" resultType="com.example.springbootmybatis.entity.User">
  5. select id, user_name userName, idcardNumber, phone_number phoneNumber
  6. from user
  7. where id = #{userId}
  8. </select>
  9. </mapper>

可也使用@select注解在接口上面写sql语句,如:

  1. @Select("select * from user where id = #{userId}")
  2. User findUserById(Long userId);

两张方法任选其一即可,不过当sql语句十分复杂时建议使用第一种方法。

7运行截图

2. 整合mybatis - 图1