zoukankan      html  css  js  c++  java
  • Mybatis Generator的使用

    在写代码过程中,常常要写一些简单的CURD操作,为了能够把时间用在业务逻辑上,看了Mybatis Generator生成工具,根据官网的文档,改成适合自己使用的生成器。

    mybatis generator的配置文件 如下:

    <?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>
        <!--读取配置文件-->
        <properties resource="generator.properties" />
    
        <context id="MySQLContext" targetRuntime="MyBatis3">
            <!--设置文件编码-->
            <property name="javaFileEncoding" value="UTF-8"/>
    
            <!--配置去掉所有生成的注释-->
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
    
            <!--设置数据库连接驱动-->
            <jdbcConnection driverClass="${jdbc.driverClass}"
                            connectionURL="${jdbc.url}"
                            userId="${jdbc.username}"
                            password="${jdbc.password}">
            </jdbcConnection>
    
            <!--当字段类型是 DECIMAL或者 NUMERIC时,是否强制转换为BigDecimal,否的话会自动根据规模的大小选择合适的类型  -->
            <javaTypeResolver >
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- 生成模型的包名和位置-->
            <javaModelGenerator targetPackage="me.xueyao.model" targetProject=".srcmainjava">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <!-- 生成映射文件的包名和位置-->
            <sqlMapGenerator targetPackage="me.xueyao.mapper"  targetProject=".srcmain
    esources">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
    
            <!-- 生成DAO的包名和位置-->
            <javaClientGenerator type="XMLMAPPER" targetPackage="me.xueyao.mapper"
                                 targetProject=".srcmainjava">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
    
            <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名,需要根据自己的需求修改-->
            <table  tableName="candidate" domainObjectName="Candidate" enableCountByExample="false"
                enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">
                <generatedKey column="id" sqlStatement="MySql" identity="true" />
            </table>
    
        </context>
    </generatorConfiguration>

    mybatis generator的执行文件 如下:

    package me.xueyao;
    
    import org.mybatis.generator.api.MyBatisGenerator;
    import org.mybatis.generator.config.Configuration;
    import org.mybatis.generator.config.xml.ConfigurationParser;
    import org.mybatis.generator.internal.DefaultShellCallback;
    
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Description: Mybatis Generator 生成器
     * @Author: Simon.Xue
     * @Date: 2019/1/18 13:44
     */
    public class Generator {
    
        public static void main(String[] args) throws Exception {
            //警告信息集合
            List<String> warnings = new ArrayList<String>();
            //读取生成器的配置文件
            InputStream resourceAsStream = Generator.class.getResourceAsStream("/mybatis-generator.xml");
            //创建配置解析器
            ConfigurationParser configurationParser = new ConfigurationParser(warnings);
            //解析配置文件
            Configuration configuration = configurationParser.parseConfiguration(resourceAsStream);
            resourceAsStream.close();
            //true时,如果有相同的文件则覆盖文件
            DefaultShellCallback defaultShellCallback = new DefaultShellCallback(true);
            //创建生成器对象
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, defaultShellCallback, warnings);
            //执行生成代码
            myBatisGenerator.generate(null);
            //输出警告信息
            for (String warning : warnings) {
                System.out.println(warning);
            }
        }
    }

    源代码托管在GitHub

  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/twodog/p/12135243.html
Copyright © 2011-2022 走看看