zoukankan      html  css  js  c++  java
  • mybatis 的 generator,

    首先,新建springboot文件,引入pom文件

            <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.4.0</version>
    </dependency>

    修改,maven_jar的插件版本

    <properties>
            <java.version>1.8</java.version>
            <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
        </properties>

    第二步,编写java文件

    只需要编写test文件,不用主入口文件

    package cn.taotao;
    
    import java.io.File;
    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.jupiter.api.Test;
    import org.mybatis.generator.api.MyBatisGenerator;
    import org.mybatis.generator.config.Configuration;
    import org.mybatis.generator.config.xml.ConfigurationParser;
    import org.mybatis.generator.exception.InvalidConfigurationException;
    import org.mybatis.generator.exception.XMLParserException;
    import org.mybatis.generator.internal.DefaultShellCallback;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class MybatisGeneratorApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
        @Test
        public void MybatisGenerator()  {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File("MybatisGenerator.xml");
            ConfigurationParser cp = new ConfigurationParser(warnings);
            try {
                Configuration config = cp.parseConfiguration(configFile);
                DefaultShellCallback callback = new DefaultShellCallback(overwrite);
                MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
                myBatisGenerator.generate(null);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            
            }
        }
    }

    第三步,编写xml文件,xml文件放到项目根目录,不是类路径。和pom.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>
    
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!-- 配置数据库连接 -->
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
                password="xxxxxx">
            </jdbcConnection>
    
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- 指定javaBean生成的位置 -->
            <javaModelGenerator targetPackage="cn.taotao.bean"
                targetProject="C:UsersOwnereclipse-workspaceTransactionDemosrcmainjava">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <!--指定sql映射文件生成的位置 -->
            <sqlMapGenerator targetPackage="mapper"
                targetProject="C:UsersOwnereclipse-workspaceTransactionDemosrcmain
    esources">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
    
            <!-- 指定dao接口生成的位置,mapper接口 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="cn.taotao.dao" targetProject="C:UsersOwnereclipse-workspaceTransactionDemosrcmainjava">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
    
    
            <!-- table指定每个表的生成策略 -->
            <table tableName="tbl_user" domainObjectName="User"
                enableCountByExample="false" enableDeleteByExample="false"
                enableUpdateByExample="false" enableSelectByExample="false"
                selectByExampleQueryId="false"></table>
            <table tableName="tbl_dept" domainObjectName="Dept"
                enableCountByExample="false" enableDeleteByExample="false"
                enableUpdateByExample="false" enableSelectByExample="false"
                selectByExampleQueryId="false"
            >
            <columnOverride column="deptinfo" javaType="java.lang.String" jdbcType="VARCHAR"></columnOverride>   <!--text类型的,转为varchar类型-->
            </table>
        </context>
    </generatorConfiguration>
  • 相关阅读:
    Java高级之类结构的认识
    14.8.9 Clustered and Secondary Indexes
    14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器
    14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构
    14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用
    14.8.1 Creating InnoDB Tables 创建InnoDB 表
    14.7.4 InnoDB File-Per-Table Tablespaces
    14.7.2 Changing the Number or Size of InnoDB Redo Log Files 改变InnoDB Redo Log Files的数量和大小
    14.7.1 Resizing the InnoDB System Tablespace InnoDB 系统表空间大小
    14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14469953.html
Copyright © 2011-2022 走看看