使用java代码运行mybatis-generator-config.xml

今天在使用mybatis的generator代码生成器时出现了一些小问题,在这里记录一下。

首先是generator的jar包,可以到http://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core

直接下载jar包或者用maven添加依赖都可以。

首先是关于XML文件的配置,如下所示,这其中最关键的问题就是这个驱动location,很容易出错误,最好就用绝对路径来写。还有就是表名一定实在数据库中就存在的。url也是容易出错的地方,要注意把&变成&还有就是mysql的高版本可能需要写更多的参数如ssl和时区等。

<?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>
    <!--数据库驱动-->
    <classPathEntry    location="C:\eeworkplace\mybatis-generator-core-1.3.5.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis_test?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false" userId="root" password="zj1314">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="model" targetProject="src\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="generator" targetProject="src\java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="src\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="user_pri" domainObjectName="Pri" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

然后就是执行xml文件的java代码

http://www.mybatis.org/generator/running/runningWithJava.html上已经明确给出了使用xml配置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);

我从书上看到的是用inputstram来接收,那样总是会报Exception in thread "main" java.net.MalformedURLException的错误,后来发现读取出来的流是空的。。。。还有就是文件路径最好还是使用绝对路径,不让很容易出现路径错误。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值