欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot集成Mybatis3

程序员文章站 2022-05-25 12:00:59
...

前题条件:spring boot版本是 2.0.5.RELEASE,Mybatis Starter版本是 2.0.1

1,  引入mybatis starter依懒 

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

2,  在application.yml文件中增加如下配置

# mybatis配置
mybatis:
  config-location: classpath:/com/xxx/xxx/core/mybatis/mybatis-config.xml
  mapper-locations: classpath:/com/xxx/xxx/core/mapper/member/*.xml
  # 定义模块中使用到的类型别名
  type-aliases-package: com.xxx.xxx.core.entity.member

# 打印sql语句
logging:
  level:
      com:
        xxx:
          xxx:
            core:
              dao: debug

3,  在对应的路径上创建mybatis-config.xml配置文件

<?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"/>
    </settings>
</configuration>

4,  在对应的路径上创建generatorConfig.xml****配置文件生成对应的实体类、Mapper接口类、映射文件,注意把xxx路径换成自己项目中的路径。

<?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>
    <!--<classPathEntry
            location="C:/Oracle/Middleware/wlserver_10.3/server/lib/ojdbc6.jar"/>-->

    <context id="mySqlTables" targetRuntime="MyBatis3">

        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>

        <!-- 生成的pojo,将implements Serializable-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>

        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <!--禁止生成注释-->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库连接属性配置-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/xxx?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull" userId="root"
                        password=""/>

        <javaTypeResolver>
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
            NUMERIC 类型解析为java.math.BigDecimal -->
            <property name="forceBigDecimals" value="false"/>
            <!-- This property is used to specify whether MyBatis Generator should force the use of JSR-310 data types for DATE, TIME,
            and TIMESTAMP fields, rather than using java.util.Date -->
            <property name="useJSR310Types" value="true"/>
        </javaTypeResolver>

        <!--生成的实体类相关配置-->
        <javaModelGenerator targetPackage="com.xxx.xxx.core.entity.member"
                            targetProject="E:\appdata\git\xxx\core\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="false"/>
            <!--设置父类-->
            <property name="rootClass" value="com.xxx.xxx.core.entity.BaseEntity" />
        </javaModelGenerator>

        <!--生成映射文件-->
        <sqlMapGenerator targetPackage="com.xxx.xxx.core.mapper.member"
                         targetProject="E:\appdata\git\xxx\core\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--生成DAO文件-->
        <javaClientGenerator targetPackage="com.xxx.xxx.core.dao.member"
                             targetProject="E:\appdata\git\xxx\core\src\main\java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--可以配置多个表-->
        <table tableName="member_wechat_user" domainObjectName="WechatUser"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
            <!--<domainObjectRenamingRule searchString="^member_" replaceString="" />-->
            <!--<columnRenamingRule searchString="^member" replaceString=""/>-->
            <!--<property name="useActualColumnNames" value="false"/>-->
        </table>

    </context>
</generatorConfiguration>

5,  增加maven插件,运行generate命令。

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>src/main/resources/com/xxx/xxx/core/mybatis-generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>

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

                    <!--引入父类所在的项目-->
                    <dependency>
                        <groupId>com.xxx</groupId>
                        <artifactId>core</artifactId>
                        <version>1.0-SNAPSHOT</version>
                    </dependency>

                </dependencies>
            </plugin>

6,  在springboot起动类增加Mapper接口扫描注解

@MapperScan("com.xxx.xxx.core.dao.member")

PS: 映射文件中的 namespace 属性必须是Mapper接口的全路径名,因为这个是通过动态代理实现的。