MyBatis Generator简单使用方法

MyBatis Generator使用方法

  • 文章目录
    • 添加配置文件
    • 配置Maven
    • 执行Maven插件

文章目录

添加配置文件

  1. 在项目resource目录下创建mybatis-generator文件夹
    创建文件夹
  2. 在文件夹下创建generatorConfig.xml,配置需要生成代码的数据表


<generatorConfiguration>
    <properties resource="mybatis-generator/generator.properties"/>
    
    
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            
            <property name="suppressAllComments" value="true"/>
        commentGenerator>

        
        <jdbcConnection
                driverClass="${driverClassName}"
                connectionURL="${url}"
                userId="${username}"
                password="${password}">
        jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.shop.order.bean" 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>

        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.shop.order.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>

        
        <table tableName="book" domainObjectName="Book"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        
        
    context>
generatorConfiguration>

  1. 在文件夹下创建generator.properties配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/store?useUnicode=true&characterEncoding=UTF-8&relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull
username=root
password=root

配置Maven

pom.xml中引入依赖

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xmlconfigurationFile>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifactsid>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.3.5version>
                    dependency>
                    <dependency>
                        <groupId>mysqlgroupId>
                        <artifactId>mysql-connector-javaartifactId>
                        <version>5.1.30version>
                    dependency>
                dependencies>
            plugin>
        plugins>
    build>

执行Maven插件

  1. 双击运行mybatis-generator:generate
    MyBatis Generator简单使用方法_第1张图片
  2. 控制台输出结果,生产mapper和bean文件
    MyBatis Generator简单使用方法_第2张图片

你可能感兴趣的:(mybatis,mysql)