• ator自动生成mybatis配置和类信息


    generator自动生成mybatis的xml配置、model、map等信息: 
    1、下载mybatis-generator-core-1.3.2.jar包。 
           网址:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator,下载mybatis-generator-core-1.3.2-bundle.zip,解压 
           找到lib下的需要jar包。 
    2、编写genertor的xml文件,名下:generator.xml 

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE generatorConfiguration  
    3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
    4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
    5.   
    6. <generatorConfiguration>  
    7.   <!-- classPathEntry:数据库的JDBC驱动的jar包地址-->  
    8.   <classPathEntry location="E:oracleproduct10.2.0db_1jdbclibojdbc14.jar" />  
    9. <context id="DB2Tables" targetRuntime="MyBatis3">  
    10.   <commentGenerator>  
    11.     <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
    12.     <property name="suppressAllComments" value="true" />  
    13.     <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->  
    14.   </commentGenerator>  
    15.   <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"  
    16.           connectionURL="jdbc:oracle:thin:@198.17.1.1:1521:ORCL"  
    17.           userId="unuser"  
    18.           password="password">  
    19.   </jdbcConnection>  
    20.     <!--  默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer   
    21.          true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal   
    22.      -->   
    23.   <javaTypeResolver >  
    24.      <property name="forceBigDecimals" value="false" />  
    25.   </javaTypeResolver>  
    26.   <!-- targetProject:自动生成代码的位置 -->  
    27.   <javaModelGenerator targetPackage="com.soft.model" targetProject="E:WebWorkSpaceworkspace_jsdownAttachdemosrc">  
    28.       <!-- enableSubPackages:是否让schema作为包的后缀 -->       
    29.       <property name="enableSubPackages" value="true" />  
    30.     <!-- 从数据库返回的值被清理前后的空格  -->   
    31.       <property name="trimStrings" value="true" />  
    32.   </javaModelGenerator>  
    33.     
    34.   <sqlMapGenerator targetPackage="sqlmap"  targetProject="E:WebWorkSpaceworkspace_jsdownAttachdemoconf">  
    35.        <property name="enableSubPackages" value="false" />  
    36.   </sqlMapGenerator>  
    37.     
    38.   <javaClientGenerator type="XMLMAPPER" targetPackage="com.soft.mapping"  targetProject="E:WebWorkSpaceworkspace_jsdownAttachdemosrc">  
    39.     <property name="enableSubPackages" value="true" />  
    40.   </javaClientGenerator>  
    41.   <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->  
    42.   <table schema="untodo" tableName="mocha_t_app" domainObjectName="MochaTodoApp" >  
    43.     
    44.   </table>  
    45. </context>  
    46.    
    47. </generatorConfiguration>  



    table其他属性: 
    enableCountByExample="false" 
    enableUpdateByExample="false" 
    enableDeleteByExample="false" 
    enableSelectByExample="false" 
    selectByExampleQueryId="false" 
    schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类, 
    如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true, 
    这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时, 
    就不会生成对应的Example类了. 

    如果table里边不配置property,默认字段都生成为类属性。 
    <ignoreColumn column="FRED" />//忽略字段 
    <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。 

    3、运行有四种:命令生成(最简单)、Java生成、ant生成、maven生成。这里说两种,有兴趣其余的可以在mybatis官网去学习。 

    1)、运行-》cmd->java - jar jar包的文件路径  -configfile  generator.xml的文件路径  -overwrite 命令。 
    如下: 

    Java代码  收藏代码
    1. java -jar E:Websoftmybaitsmybatis-generator-core-1.3.2libmybatis-generator-core-1.3.2.jar -configfile E:WebWorkSpaceworkspace_jsdownAttachdemosrccommochasoftdowngenerator.xml -overwrite  


    成功时输出:MyBatis Generator finished successfully. 
    2)、java运行关键代码: 
     

    Java代码  收藏代码
      1. List<String> warnings = new ArrayList<String>();  
      2.   boolean overwrite = true;  
      3.   File configFile = new File("generatorConfig.xml");  
      4.   ConfigurationParser cp = new ConfigurationParser(warnings);  
      5.   Configuration config = cp.parseConfiguration(configFile);  
      6.   DefaultShellCallback callback = new DefaultShellCallback(overwrite);  
      7.   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);  
      8.   myBatisGenerator.generate(null);  
  • 相关阅读:
    基于JWT用户认证方式(前后端分离)
    git推送时报错:hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing
    (fields.E210) Cannot use ImageField because Pillow is not installed....解决方法
    python3.6报错:AttributeError: 'str' object has no attribute 'decode'
    于在Python3.6.7 +Ubuntu16.04下安装channels报错
    docsify 搭建优雅项目文档
    SQLSERVER 系统表查询
    数据立方建立-如何实现一对多,多对多
    从BI分析角度,数据立方的建立方法
    MYSQL库内所有表名及表结构获取
  • 原文地址:https://www.cnblogs.com/bkywanly/p/10765700.html
Copyright © 2020-2023  润新知