美文网首页
mybatis generation的使用

mybatis generation的使用

作者: 行走的鸡汤哥 | 来源:发表于2019-12-29 22:12 被阅读0次

    pom依赖

    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <!-- mapperScan的依赖 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.6</version>
    </dependency>
    
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
            </plugin>
        </plugins>
    </build>
    

    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>
        <!-- 指定jdbc驱动 -->
        <classPathEntry location="C:\Users\r.x\.m2\repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar" />
    
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/data_source?serverTimezone=UTC"
                            userId="root"
                            password="root">
            </jdbcConnection>
    
            <javaTypeResolver >
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <javaModelGenerator targetPackage="com.yrx.datasourcemanager.manager.pojo" targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <sqlMapGenerator targetPackage="com.yrx.datasourcemanager.manager.xml"  targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
    
            <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.yrx.datasourcemanager.manager.dao"  targetProject=".\src\main\java">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
    
            <!-- 目标表 -->
            <table tableName="blog">
                <property name="useActualColumnNames" value="false"/>
                <generatedKey column="ID" sqlStatement="mysql" identity="true" />
            </table>
        </context>
    </generatorConfiguration>
    

    生成代码的脚本

    mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
    

    最后还要指定spring扫描的mapper包路径

    @Configuration
    @MapperScan("com.yrx.datasourcemanager.manager.dao")
    public class MybatisConfig {
    
    }
    

    相关文章

      网友评论

          本文标题:mybatis generation的使用

          本文链接:https://www.haomeiwen.com/subject/fspuoctx.html