springboot整合mybatis+mysql+druid数据源

springboot整合mybatis+mysql+druid数据源

  1. 导入依赖包
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--druid数据源 start-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!--druid数据源 end-->
  1. 在pom.xml文件中添加如下
<!--maven打包的时候就会将src/main/java和src/main/resources相同包下的文件合并到同一包中-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
  1. 在application.yml中添加配置
spring:
  datasource:
    url: jdbc:mysql://39.97.234.52:3306/LearnWeb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
  1. 在启动类上添加注解@MapperScan注解,扫描mapper.xml文件所在的位置
@SpringBootApplication
@MapperScan("com.example.vueblog.mapper")
public class VueblogApplication {

    public static void main(String[] args) {
        SpringApplication.run(VueblogApplication.class, args);
    }

}

springboot整合mybatis+mysql+druid数据源

上一篇:MySQL基础(二)--数据操纵语言DML


下一篇:linux 安装mysql-8以及远程连接步骤(图文并茂)