SpringBoot——整合mybatis

  1. 导入依赖

      <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
     <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
  2. application.yml中编写数据库配置

    spring:
      datasource:
        # 数据源基本配置
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost/seckill?serverTimezone=Asia/Shanghai
        type: com.alibaba.druid.pool.DruidDataSource
    
        #数据源其他配置
        initialSize: 5
        minIdle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
    
  3. resources目录下创建mybatis包并编写配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <!--开启驼峰命名法-->
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <!--使用jdbc的useGeneratedKeys获取数据库自增主键值-->
            <setting name="useGeneratedKeys" value="true"/>
        </settings>
    </configuration>
    
  4. application.yml中编写mybatis配置

    #扫描mybatis主配置文件,*Mapper.xml文件
    mybatis:
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    
  5. mybatis包下面创建一个mapper包方便编写实体类的sql

  6. service层调用dao层

  7. controller层调用service层

  8. 开启服务测试!

posted @ 2020-03-06 14:57  Godlesss  阅读(179)  评论(0编辑  收藏  举报