记录一次 springboot 集成 mybaits + H2 DaTaBase

 pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dnslin</groupId>
    <artifactId>dnslin_bot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 使用springboot的parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>

        <dependencies>
            <!-- 使用simbot的parent进行版本控制 -->
            <dependency>
                <groupId>love.forte.simple-robot</groupId>
                <artifactId>parent</artifactId>
                <version>2.2.1</version><!--vv-->
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <!--使用mirai组件-springboot-starter -->
        <dependency>
            <groupId>love.forte.simple-robot</groupId>
            <artifactId>component-mirai-spring-boot-starter</artifactId>

        </dependency>


        <!-- 使用spring的logging -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>


        <!-- 热部署 -->
        <!-- devtools可以实现页面热部署(即页面修改后会立即生效,
            这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现) -->
        <!-- 实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。 -->
        <!-- 即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),
            注意:因为其采用的虚拟机机制,该项重启是很快的 -->
        <!-- (1)base classloader (Base类加载器):加载不改变的Class,例如:第三方提供的jar包。 -->
        <!-- (2)restart classloader(Restart类加载器):加载正在开发的Class。 -->
        <!-- 为什么重启很快,因为重启的时候只是加载了在开发的Class,没有重新加载第三方的jar包。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <!-- optional=true, 依赖不会传递, 该项目依赖devtools;
                之后依赖boot项目的项目如果想要使用devtools, 需要重新引入 -->
            <optional>true</optional>
        </dependency>

        <!--测试相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 内存数据库h2-->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.200</version>
        </dependency>


        <!---mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

        <!-- lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
        </dependency>

        <!--hutools-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.13</version>
        </dependency>

        <!--jsoup-->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

        <!-- jackson json处理 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>

        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

        <!--Excel处理-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Springboot打包插件。使用 mvn package 进行打包。 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.project.lombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <dependencies>
                    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
                    <dependency>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                        <version>1.4.200</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <!--配置文件的路径-->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
config.properties
jdbc.driverClass=org.h2.Driver
jdbc.url=jdbc:h2:mem:address;MODE=MySQL
jdbc.user=root
jdbc.password=root123

group.package=com.dnslin.dnslin_bot
catalog.name=address
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <properties resource="config.properties"/>
    <context id="testTables" targetRuntime="MyBatis3">
        <property name="javaFileEncoding" value="UTF-8"></property>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <property name="lombok" value="Getter,Setter,ToString,Accessors"/>
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="${jdbc.driverClass}"
            connectionURL="${jdbc.url}"
            userId="${jdbc.user}"
            password="${jdbc.password}">
        </jdbcConnection>
    
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="${group.package}.pojo"
            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="mapper"
            targetProject="src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="${group.package}.mapper"
            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定数据库表 -->
        <table schema="" tableName="address_all"></table>
    </context>
</generatorConfiguration>
 application.yml
simbot:
  core:
    # 扫描 simbot-bots目录下的所有 .bot 文件。
    bot-resource-type: RESOURCE_FIRST
  component:
    mirai:
      # 如果不为空,此处代表指定一个 deviceInfo 的 json文件路径。
      deviceInfo-file: src/main/resources/device.json
spring:
  # 热部署
  devtools:
    restart:
      additional-paths: src/main/java
  # 数据源
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:db/schema.sql
#    data: classpath:db/addressAll.sql
    url: jdbc:h2:mem:address;MODE=MySQL
    username: root
    password: root123
  h2:
    console:
      enabled: true
      path: /h2
      settings:
        trace: true
        web-allow-others: true
#  配置mybatis:
mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

# 日志
logging:
  level:
    love.forte.simbot: debug
    com.dnslin.dnslin_bot: debug
最后修改:2021 年 09 月 25 日 09 : 08 PM
如果觉得我的文章对你有用,请随意赞赏