zoukankan      html  css  js  c++  java
  • Mybatis Generator

    最近DBA不允许使用Mybatis注解和tk进行数据库操作,要求使用XML的方式以便统一管理SQL语句。这可简直是要为难我胖虎啊,如果表字段少还好说,如果字段多我岂不是要累死?思索了一翻,不如使用Mybatis Generator逆向生成curd的xml, mapper接口和实体类,哈哈,这样还不是美滋滋。

    下面记录一下使用MBG需要注意的地方,
    先贴一个完整的例子

    <?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 中的属性要按规定的顺序排列,否则会报错-->
        <!-- MyBatis3Simple 不会生成Example相关的信息 如果是Mybatis3就会生成-->
        <context id="test" targetRuntime="MyBatis3Simple">
            <commentGenerator>
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="false" />
            </commentGenerator>
    
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/mybatis"
                            userId="root" password="root">
            </jdbcConnection>
    
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <javaModelGenerator targetPackage="me.gacl.domain"
                                targetProject="src/main/java">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
                <!-- 从数据库返回的值被清理前后的空格 -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <sqlMapGenerator targetPackage="mapping"
                             targetProject="src/main/resource">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
    
            <javaClientGenerator type="XMLMAPPER" targetPackage="mapper" targetProject="src/main/java">
                <!-- enableSubPackages:是否让schema作为包的后缀 -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
    
            <table tableName="person" schema=""
                   enableCountByExample="false"
                   enableDeleteByExample="false" enableUpdateByExample="false"
                   enableSelectByExample="false" selectByExampleQueryId="false">
    
                <generatedKey column="id" sqlStatement="MYSQL" identity="true" />
            </table>
    
        </context>
    
    </generatorConfiguration>
    
    1. 如果不希望生成和Example查询有关的内容,则可以按照如下方法进行配置。
     <context id=” Mysql” targetRuntime=”MyBatis3Simple” defaultModelType=”flat ” > 
    
    1. 如果使用的是targetRuntime=Mybatis3,但仍不想生成Example相关的东西,则可以在table子标签的属性值添加如下属性
     <table tableName="person" schema=""
                   enableCountByExample="false"
                   enableDeleteByExample="false" enableUpdateByExample="false"
                   enableSelectByExample="false" selectByExampleQueryId="false">
    
                <generatedKey column="id" sqlStatement="MYSQL" identity="true" />
            </table>
    
    targetRuntime 生成的代码编译版本
    Mybatis3 JAVA5,Mybatis 3.0
    MyBatis3Simple JAVA5,Mybatis 3.0
    MyBatis3DynamicSql JAVA8,Mybatis 3.4.2
    1. context中的子标签有严格的配置顺序,不按顺序写子标签会报错

    执行

    要让Mybatis执行生成代码和sql配置有五种方式

    1. 命令行
    2. Ant插件
    3. Maven插件
    4. Java代码
    5. Eclipse插件
      我使用的是Java代码的方式,其他四种我还没有试过
    public class GeneratorTool {
        public static void main(String[] args) throws Exception {
    
            List<String> warnings = new ArrayList<>();
            boolean overwrite = true;
            File configFile = new File(GeneratorTool.class.getResource("/generatortest.xml").getPath());
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        }
    }
    
  • 相关阅读:
    三分
    知识点整合摘取
    Codeforces Round #533 C. Ayoub and Lost Array
    判断当前VC 是push还是present的
    自动布局
    获取当前的 viewController
    UIImageView 点击放大缩小
    UIButton 在UIScrollView里面 点击效果不明显的问题
    IOS AutoLayout 遍历修改约束
    GIT 命令 操作 记录
  • 原文地址:https://www.cnblogs.com/lanqi/p/9273989.html
Copyright © 2011-2022 走看看