创建了一个简单的例子但是 总是报空异常 代码如下
mybatis-generator .xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration SYSTEM
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!-- 配置jdbc驱动包的路径 -->
<!-- <classPathEntry location="F:\WorkSpace\javaEE\mybatis2\WebContent\WEB-INF\lib\sqljdbc4.jar"/> -->
<classPathEntry location=".lib/sqljdbc4.jar"/>
<context id="account_table" targetRuntime="MyBatis3">
<!-- 配置注释 -->
<commentGenerator>
<!-- 是否去除所有自动生成的注释文件 -->
<property name="suppressAllComments" value="true"/>
<!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection
connectionURL="jdbc:sqlserver://localhost:1433;databaseName=mybank"
driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
userId="sa"
password="zxcv1234"
/>
<javaTypeResolver>
<!-- 把jdbc中的decimal与numberic类型转化为integer类型 -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 数据表对应的Model层 -->
<javaModelGenerator targetPackage="com.my.pojo" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- sql mapper 映射配置 -->
<sqlMapGenerator targetPackage="com.my.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- mapper 接口 -->
<javaClientGenerator targetPackage="com.my.inter" type="XMLMAPPER" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要对那些数据表操作 -->
<!-- schema你的数据库,tableName表明,domainObjectName对应你的javabean类名,是否生成相应的example -->
<table tableName="account" schema="mybatis_db" domainObjectName="Account"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false"
/>
</context>
</generatorConfiguration>
测试实现类 GenMain
package com.my.test;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GenTest {
@Test
public static void main(String[] args) {
List<String> warnings=new ArrayList<String>();
boolean overwrite=true;
String genCfg="/mbg_config.xml";
File configFile=new File(GenTest.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMLParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
try {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (InvalidConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("生成Mybatis配置成功!");
}
}
还没有改好 先写到这里