springboot整合mybatis(idea)

心已赠人 2024-04-20 11:29 119阅读 0赞

从idea新建项目

选择spring启动在这里插入图片描述* help、mvnw 文件可以删除

  • springBoot3.0需要的最小JDK是JDK17,当低于17的时候会报错。
    改成2.7.6

新建控制层Controller、Mapper层和Model文件夹

  • 必须在springBoot启动项下面新建,不然无法识别。
允许XML进入target

在pom.xml里面写入

  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.properties</include>
  7. <include>**/*.xml</include>
  8. </includes>
  9. <filtering>false</filtering>
  10. </resource>
  11. </resources>
  12. </build>
引入必要的依赖包
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-jdbc</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.projectlombok</groupId>
  11. <artifactId>lombok</artifactId>
  12. <optional>true</optional>
  13. </dependency>
  14. <dependency>
  15. <groupId>mysql</groupId>
  16. <artifactId>mysql-connector-java</artifactId>
  17. <version>8.0.28</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-test</artifactId>
  22. <scope>test</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-validation</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.xmlunit</groupId>
  34. <artifactId>xmlunit-core</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.mybatis.spring.boot</groupId>
  38. <artifactId>mybatis-spring-boot-starter</artifactId>
  39. <version>2.0.1</version>
  40. </dependency>

配置数据库账号密码

application.properties

  1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  2. spring.datasource.url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf8&useSSL=false
  3. spring.datasource.username=testUser
  4. spring.datasource.password=testnlp#123

在model导入一个新的类

和数据库对应,构建一个类
在这里插入图片描述

实现GetSet方法、各种有参构造、无参构造。
通过@value实现默认值设定

  1. @Data
  2. @Component
  3. @AllArgsConstructor
  4. @NoArgsConstructor
  5. public class Cat {
  6. @NotNull(message="必须为空")
  7. Integer age;
  8. @Value("dog01")
  9. String name;
  10. @Value("1")
  11. Integer id;
  12. }

写入mapper文件

在mapper文件夹下写入一个接口

  1. @Mapper
  2. public interface CatMapper {
  3. @Insert("insert into cat(id, name, age) values(#{id}, #{name}, #{age})")
  4. void addOneCat(Cat cat);
  5. }
  • 加入@mapper注解或者在主类加入@MapperScan(“com/example/test02/test02/mapper”)
  • 可以在接口上写注释实现简单的SQL查询

写入一个控制层

  • 类上加入控制注解
  • 将mapper注入这个类
  • 实现控制类方法

    @RestController
    public class testController {

    1. @Resource
    2. CatMapper catMapper;
    3. @RequestMapping(value = "/cat", method = RequestMethod.POST)
    4. @ResponseBody
    5. Cat addCat(Cat cat) {
    6. catMapper.addOneCat(cat);
    7. return cat;
    8. }

    }

测试接口

启动SpringBoot服务
在这里插入图片描述
使用postman发送对应的请求,检查返回值
在这里插入图片描述

检查数据库是否有新增的数据:
在这里插入图片描述
成功写入数据。

发表评论

表情:
评论列表 (有 0 条评论,119人围观)

还没有评论,来说两句吧...

相关阅读