springboot 集成druid 集成mybatise

发布时间 2023-08-04 20:26:25作者: 花开如梦

spring加载druid和mybatise

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springbootdruidmybatise</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdruidmybatise</name>
    <description>springbootdruidmybatise</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--====================== druid=====================================-->
        <!-- spring-boot-starter-jdbc 里面也加载了 spring-jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
<!--    直接加载 spring-jdbc 也行    -->
<!--        <dependency>-->
<!--            <groupId>org.springframework</groupId>-->
<!--            <artifactId>spring-jdbc</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.18</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>

        <!--mybatis mapper生成插件-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.2</version>
        </dependency>
        <!-- mybatis 启动 注意与springboot版本适配-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!-- Mybatis-Generator插件,自动生成代码 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!--必須要引入数据库驱动-->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <!--必须制定版本-->
                        <version>8.0.33</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

application.yml  分别是

  • druid 连接池配置   
  • 表初始化配置  
  • mybatis配置
spring:
  mvc:
    pathmatch.matching-strategy: ant_path_matcher
  messages:
    basename: i18n.message
  web:
    locale-resolver: accept_header

  datasource:
    url: jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      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
      pool-prepared-statements: true

      filters: stat,wall
      max-pool-prepared-statement-per-connection-size: 20
      use-global-data-source-stat: true
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
      #开启druid 监控台servlet 登录地址:http://localhost:8080/druid/index.html
      stat-view-servlet:
        enabled: true
        #监控台登录账号密码
        login-username: root
        login-password: 123456
      #开启druid 监控filter
      web-stat-filter:
        enabled: true
  #插入表,插入一次就关掉
  sql:
    init:
      schema-locations: classpath:sql/mytest.sql
#      mode: always
mybatis:
  mapper-locations: classpath:com/example/springboot04_mocmvc/mapper/*Mapper.xml
  type-aliases-package: com.example.springboot04_mocmvc.pojo
  configuration:
    map-underscore-to-camel-case: true

#指定mybatis配置文件。以前使用配置文件的方式
#  config-location: classpath:mybatis-config.xml

访问druid监控台:http://localhost:8080/druid/index.html

generatorConfig.xml

<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="simple" targetRuntime="MyBatis3Simple">

        <!--数据源-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mytest"
                        userId="root"
                        password="123456"/>

        <!-- pojo JAVA实体生成的规则
            targetPackage:生成到哪个包下面
            targetProject:当前文件的哪个相对路径下
        -->
        <javaModelGenerator targetPackage="com.example.springboot04_mocmvc.pojo" targetProject="src/main/java"/>


        <!--mapper xml映射文件 生成的规则
            targetPackage:生成到哪个包下面
            targetProject:当前文件的哪个相对路径下

            注意:sqlMapGenerator 在 javaClientGenerator 前面,注意语句顺序
        -->
        <sqlMapGenerator targetPackage="com.example.springboot04_mocmvc.mapper" targetProject="src/main/resources"></sqlMapGenerator>


        <!--mapper 接口 生成的规则
            targetPackage:生成到哪个包下面
            targetProject:当前文件的哪个相对路径下
            type:指定生成的方式
                1.使用注解的方式
                2.使用接口绑定的方式生成 (要配置sqlMapGenerator)
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springboot04_mocmvc.mapper" targetProject="src/main/java"/>


        <!--配置哪些表进行代码生成
                tableName:表明
                domainObjectName:指定pojo 类名 (可省略自动生成)
                mapperName:指定mapper mapper接口 和 xml映射文件 用的同一个名字 (可省略自动生成)
        -->
        <table tableName="mytable"/>
        <table tableName="person"/>
    </context>
</generatorConfiguration>
注意此处是使用插件方式生成代码,

代码方式生成:

 @Test
    public void MybatisTool() throws XMLParserException, IOException, 
            InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src/main/resources/mybatis-jenerator.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }