345353 发表于 2016-7-8 09:17:36

使用mybatis-generator工具加快开发速度

在使用mybatis时,需要书写大量的mapping映射文件,手动书写工作量大并且容易出错。值得庆幸的是,Mybatis-Generator可以用来帮助我们自动生成这些文件,大幅度提高开发效率。    1.准备工作
从https://github.com/mybatis/generator/releases下载mybatis-generator的jar包;

从http://www.grepcode.com下载数据库驱动包。

新建如下结构目录:

2.generator.xml介绍
生成相关文件前,需要进行配置,配置文件名称任意,后缀为xml。(属性相关解释在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>
    <!--数据库驱动-->
    <classPathEntrylocation="mysql-connector-java-5.1.36.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
      <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
      </commentGenerator>
      <!--数据库链接地址账号密码-->
      <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mybatis" userId="root" password="yxc.">
      <!--oracle数据库
      <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
          connectionURL="jdbc:oracle:thin:@localhost:1521:ORCL"
          userId="username"
          password="password">
      </jdbcConnection>
      -->
      </jdbcConnection>
      <!--默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
         true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal   
      -->   
      <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
      </javaTypeResolver>
      <!--生成Model类存放位置-->
      <javaModelGenerator targetPackage="yb.model" targetProject="src">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格-->
            <property name="trimStrings" value="true"/>
      </javaModelGenerator>
      <!--生成映射文件存放位置-->
      <sqlMapGenerator targetPackage="yb.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
      </sqlMapGenerator>
      <!--生成Dao类存放位置-->
      <javaClientGenerator type="XMLMAPPER" targetPackage="yb.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
      </javaClientGenerator>
      <!--生成对应表及类名-->
      <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名
      若要生成例子可将enableCountByExample等设为true, 就会生成一个对应domainObjectName的Example类,false则不生成,默认策略是true。
      类似的还有enableUpdateByExample、enableDeleteByExample、enableSelectByExample、selectByExampleQueryId属性。
      -->
      <table tableName="phone" domainObjectName="Phone" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>
3.运行
运行有四种:命令生成(最简单)、Java生成、ant生成、maven生成。这里说前面两种,有兴趣其余的可以在mybatis官网去学习。
1)打开cmd命令窗口,cd到工作目录中,运行如下命令
java - jar mybatis-generator包的文件路径-configfilegenerator.xml的文件路径-overwrite 命令。
本例为:

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite
成功时输出:MyBatis Generator finished successfully.
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等等。
5.注意点
1)generator.xml格式:必须是以UTF-8无BOM格式编码,用notepad++转换。
2)注意数据库包的可用性,无效的数据库包转换会报错。

页: [1]
查看完整版本: 使用mybatis-generator工具加快开发速度