【配置】SpringBoot2.3.0整合MyBaits-(XML)

依赖

  • spring-boot-starter-jdbc
  • mybatis-spring-boot-starter
  • mysql-connector-java
  • druid-spring-boot-starter
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
    <groupId>org.mybatis.spring.bootgroupId>
    <artifactId>mybatis-spring-boot-starterartifactId>
    <version>2.1.2version>
dependency>
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <scope>runtimescope>
dependency>
<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druid-spring-boot-starterartifactId>
    <version>1.1.22version>
dependency>

目录结构

【配置】SpringBoot2.3.0整合MyBaits-(XML)_第1张图片

  • mapper.xml

    
    
    <mapper namespace="com.live.mapper.UserMapper">
        <select id="queryUsers" resultType="com.live.model.User">
            select * from jdbc_test
        select>
    mapper>
    
  • mybatis.xml

    
    
    <configuration>
        <settings>  
            <setting name="lazyLoadingEnabled" value="true"/>
            <setting name="aggressiveLazyLoading" value="false"/>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        settings>
    configuration>
    
    • 更多MyBatis配置官网

application.yml

  • config-location:指定mybatis配置文件路径

  • mapper-locations:指定mapper sql映射文件路径

  • 默认数据源(Jdbc)

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
    mybatis:
      config-location: classpath:mybatis/mybatis.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    
  • druid数据源

    spring:
      datasource:
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        druid:
          stat-view-servlet:
            enabled: true
            url-pattern: /druid/*
            login-username: admin
            login-password: 12345
          initial-size: 5
          min-idle: 5
          max-active: 20
          max-wait: 60000
          time-between-eviction-runs-millis: 60000
          min-evictable-idle-time-millis: 300000
          validation-query: SELECT 1 FROM DUAL
          test-while-idle: true
          test-on-borrow: false
          test-on-return: false
          pool-prepared-statements: true
          max-pool-prepared-statement-per-connection-size: 20
          filters: [stat,wall,log4j]
          filter:
            stat:
              merge-sql: true
              slow-sql-millis: 5000
    mybatis:
      config-location: classpath:mybatis/mybatis.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    

Java

  • POJO

    package com.live.model;
    
    import lombok.Data;
    
    @Data
    public class User {
    
        private int id;
        private String username;
        private String password;
    
    }
    
    
  • UserMapper

    package com.live.mapper;
    
    import com.live.model.User;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    @Mapper
    public interface UserMapper {
    
        List<User> queryUsers();
    
    }
    
    
  • Controller

    package com.live.controler;
    
    import com.live.mapper.UserMapper;
    import com.live.model.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    public class MyBatisController {
    
        @Autowired
        private UserMapper userMapper;
    
        @GetMapping("/MyBatis")
        public List<User> test() {
            return userMapper.queryUsers();
        }
    
    }
    
    

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J2WiINQE-1590929424577)(G:\微云同步\同步文件\Microsoft同步\文档\images\image-20200531204939658.png)]

完!

你可能感兴趣的:(spring-boot)