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

    1,以插件的形式加入到eclipse/dropins中;

    2,设置mybatis-generator.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>
        <!-- 引入配置文件 -->
        <properties resource="init.properties"/>
        
        <!-- 指定数据连接驱动jar地址 -->
        <classPathEntry location="${classPath}" />
        
        <!-- 一个数据库一个context -->
        <context id="infoGuardian">
            <!-- 注释 -->
            <commentGenerator >
                <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
                <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
            </commentGenerator>
            
            <!-- jdbc连接 -->
            <jdbcConnection driverClass="${jdbc_driver}"
                connectionURL="${jdbc_url}" userId="${jdbc_user}"
                password="${jdbc_password}" />
            
            <!-- 类型转换 -->
            <javaTypeResolver>
                <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
            
            <!-- 生成实体类地址 -->    
            <javaModelGenerator targetPackage="com.oop.eksp.user.model"
                targetProject="${project}" >
                <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
                <property name="enableSubPackages" value="false"/>
                <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            
            <!-- 生成mapxml文件 -->
            <sqlMapGenerator targetPackage="com.oop.eksp.user.data"
                targetProject="${project}" >
                <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
                <property name="enableSubPackages" value="false" />
            </sqlMapGenerator>
            
            <!-- 生成mapxml对应client,也就是接口dao -->    
            <javaClientGenerator targetPackage="com.oop.eksp.user.data"
                targetProject="${project}" type="XMLMAPPER" >
                <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
                <property name="enableSubPackages" value="false" />
            </javaClientGenerator>
            
            <!-- 配置表信息 -->    
            <table schema="${jdbc_user}" tableName="s_user"
                domainObjectName="UserEntity" enableCountByExample="false"
                enableDeleteByExample="false" enableSelectByExample="false"
                enableUpdateByExample="false">
                <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 
                    是否生成 example类   -->
                
                <!-- 忽略列,不生成bean 字段 -->
                <ignoreColumn column="FRED" />
                <!-- 指定列的java数据类型 -->
                  <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
            </table>
    
        </context>
    </generatorConfiguration>

    properties 文件

    #Mybatis Generator configuration
    project = EKSP
    classPath=E:/workplace/EKSP/WebContent/WEB-INF/lib/ojdbc14.jar
    jdbc_driver = oracle.jdbc.driver.OracleDriver
    jdbc_url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
    jdbc_user=INFOGUARDIAN
    jdbc_password=info_idap132

    3,右键点击该文件,生成;

    mysql 

    <?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>
        <!-- 制定mysql的驱动包的路径 千万别放中文路径下 -->
        <classPathEntry location="D:JavaJarCollectionmysqlmysql-connector-java-5.0.8-bin.jar" />
        <!-- 配置数据源和生成的代码所存放的位置 -->
        <context id="context1">
            <commentGenerator>
                <!-- 去除自动生成的注释 -->
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/test?user=root&amp;password=123456&amp;useUnicode=true&amp;characterEncoding=UTF-8"
                  />
            <!-- 所生成的实体类的位置默认资源包src -->
            <javaModelGenerator targetPackage="com.tl.model" targetProject="TL2" />
            <!-- 所生成的sqlMap的影射文件的位置,默认资源包src -->
            <sqlMapGenerator targetPackage="com.tl.mapper" targetProject="TL2" />
            <!-- 生成mapxml对应的client,也就是dao -->
            <javaClientGenerator targetPackage="com.tl.dao" targetProject="TL2" type="XMLMAPPER" />
    
            <!--为哪些表生成代码 tableName:表名 schema:不用填写,其余属性是禁用例子查询的生成 -->
            <table schema="test" tableName="employees" domainObjectName="Employees"
                enableCountByExample="false" enableUpdateByExample="false"
                enableDeleteByExample="false" enableSelectByExample="false"
                selectByExampleQueryId="false" >
            </table>
            
    
        </context>
    </generatorConfiguration>
  • 相关阅读:
    Maven Nexus 上传jar到本地仓库。
    java Web与Flex通过HTTP service 通信 并解析Map 数据。
    JACOB调用WORD书签修改WORD文档,并生成html显示到页面中
    去除java list 中的 [] 括号
    2019-2020-2 20175222 《网络对抗技术》Exp9 Web安全基础
    2019-2020-2 20175222 《网络对抗技术》 Exp 8 Web基础
    2019-2020-2 20175222 《网络对抗技术》 Exp7 网络欺诈防范
    2019-2020-2 20175222 《网络对抗技术》 Exp6 MSF基础应用
    2019-2020-2 20175222《网络对抗技术》 Exp5 信息搜集与漏洞扫描
    2019-2020-2 20175222罗雨石 《网络对抗技术》 Exp4 恶意代码分析
  • 原文地址:https://www.cnblogs.com/stono/p/4509840.html
Copyright © 2011-2022 走看看