generator自动生成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



<generatorConfiguration>
    
    
    <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">
        jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="sy.model" targetProject="D:\study\mybatis\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="sy.mapping" targetProject="D:\study\mybatis\src">
            <property name="enableSubPackages" value="true" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="sy.dao" targetProject="D:\study\mybatis\src">
            <property name="enableSubPackages" value="true" />
        javaClientGenerator>
        
        <table tableName="tbug" domainObjectName="Bug" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    context>
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,默认字段都生成为类属性。
//忽略字段
//无论字段是什么类型,生成的类属性都是varchar。
更多的配置信息大家可以去网上找找。

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

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

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

2)、java运行关键代码:

List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
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);

其实Java运行,细分可以分两种,还有一种可以去官网学习。
4、生成代码之后,根据自己的实际项目架构,可以对生成的代码进行适当的修改,如把数据库管理交有spring等等。
配置好,自动生成,减少大量搬砖的工作。

你可能感兴趣的:(mybatis)