1、创建工程 

2、配置

1)pom.xml 部分配置需更改

        <!--mybatis 数据相关-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

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

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
 
            <!--mybatis 生成工具插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

 

2)application.yml 部分配置需更改

server:
  port: 8080


mybatis:
  type-aliases-package: com.xxx.xxx.model
  mapper-locations: classpath:/mapper/*.xml
  config-location: classpath:/mybatis-config.xml
#  classpath为 resources/

spring:
  datasource:
    url: jdbc:mysql://ip:port/db_name?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

 

3)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>
    <classPathEntry  location="mysql-connector-java-8.0.13.jar所在地方,一般在mavend的repository下"/>

    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/> 
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://ip:port/db_name?serverTimezone=UTC"
                        userId="root"
                        password="123456"/>


        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.xxx.model" targetProject="src/main/java/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.idao" targetProject="src/main/java/">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        
        <table tableName="table1"
               domainObjectName="Table1"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true"
                 catalog="db_name">
        </table>
        
    </context>
</generatorConfiguration>

4)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="cacheEnabled" value="true"/>-->
        <!--<setting name="lazyLoadingEnabled" value="true"/>-->
        <!--<setting name="multipleResultSetsEnabled" value="true"/>-->
        <setting name="useColumnLabel" value="true"/>
        <setting name="useGeneratedKeys" value="false"/>
        <setting name="autoMappingBehavior" value="PARTIAL"/>
        <!--<setting name="defaultExecutorType" value="SIMPLE"/>-->
        <!--<setting name="defaultStatementTimeout" value="25"/>-->
        <!--<setting name="safeRowBoundsEnabled" value="false"/>-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--<setting name="localCacheScope" value="SESSION"/>-->
        <!--<setting name="jdbcTypeForNull" value="OTHER"/>-->
        <!--<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>-->
        <!--优先使用log4j-->
        <!--<setting name="logImpl" value="LOG4J"/>-->
    </settings>  
</configuration>

3、终端输入 mvn mybatis-generator:generate 创建mapper、xml、model

spring boot 集成mybatis

4、创建 dao、service、controller

注意事项:

1)加注解@Service  @Repository等

2)@MapperScan("com.xxx.mapper") application中加,mapper下为xxxMapper.class所在

 

 

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2021-10-30
  • 2021-12-16
  • 2022-12-23
  • 2021-10-03
  • 2021-08-01
猜你喜欢
  • 2022-12-23
  • 2021-07-01
  • 2022-01-02
  • 2021-09-30
  • 2021-08-12
相关资源
相似解决方案