SpringBoot- xml方式整合Mybatis

拼搏现实的明天。 2022-10-05 14:54 307阅读 0赞

SpringBoot整合Mybatis

一.xml方式整合mybatis

xml方式在编写复杂SQL时,更适合。

1.导入依赖

  1. <!-- mysql驱动-->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. </dependency>
  6. <!-- druid连接-->
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>druid-spring-boot-starter</artifactId>
  10. <version>1.1.10</version>
  11. </dependency>
  12. <!-- mybatis-->
  13. <dependency>
  14. <groupId>org.mybatis.spring.boot</groupId>
  15. <artifactId>mybatis-spring-boot-starter</artifactId>
  16. <version>1.3.2</version>
  17. </dependency>

2.编写配置文件

  1. //准备实体类
  2. @Data
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class Air {
  6. private Integer id;
  7. private Integer districtId;
  8. private java.sql.Date monitorTime;
  9. private Integer pm10;
  10. private Integer pm25;
  11. private String monitoringStation;
  12. private java.sql.Date lastModifyTime;
  13. @Override
  14. public String toString() {
  15. return "Air{" +
  16. "id=" + id +
  17. ", districtId=" + districtId +
  18. ", monitorTime=" + monitorTime +
  19. ", pm10=" + pm10 +
  20. ", pm25=" + pm25 +
  21. ", monitoringStation='" + monitoringStation + '\'' +
  22. ", lastModifyTime=" + lastModifyTime +
  23. '}';
  24. }
  25. }

3.准备Mybatis

3.1接口

  1. @ComponentScan
  2. public interface AirMapper {
  3. List<Air> selectAll();
  4. }

3.2在启动类中,添加直接扫描Mapper接口所在的包

  1. @SpringBootApplication
  2. @MapperScan(basePackages = "boot.mapper")
  3. public class Springboot02Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Springboot02Application.class, args);
  6. }
  7. }

3.3准备映射文件

  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="boot.mapper.AirMapper">
  6. <!-- List<Air> selectAll();-->
  7. <select id="selectAll" resultType="boot.entity.Air">
  8. select * from air
  9. </select>
  10. </mapper>

3.4 yml文件

  1. <!-- 添加yml文件配置信息 -->
  2. # mybatis配置
  3. mybatis:
  4. # 扫描映射文件
  5. mapper-locations: classpath:mapper/*.xml
  6. # 配置别名扫描的包
  7. type-aliases-package: com.qf.firstspringboot.entity
  8. configuration:
  9. # 开启驼峰映射配置
  10. map-underscore-to-camel-case: true
  11. # 连接数据库的信息
  12. spring:
  13. datasource:
  14. driver-class-name: com.mysql.jdbc.Driver
  15. url: jdbc:mysql:///air?serverTimezone=UTC
  16. username: root
  17. password: 123
  18. type: com.alibaba.druid.pool.DruidDataSource

4.测试

  1. @SpringBootTest
  2. class AirMapperTest {
  3. @Autowired
  4. private AirMapper airMapper;
  5. @Test
  6. void selectAll() {
  7. List<Air> airs = airMapper.selectAll();
  8. for (Air air : airs) {
  9. System.out.println(air);
  10. }
  11. }
  12. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3F3cTE1MTgzNDY4NjQ_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3F3cTE1MTgzNDY4NjQ_size_16_color_FFFFFF_t_70 1

发表评论

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

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

相关阅读