Mybatis逆向工程-系统找不到指定路径问题解决方案
今天第一次使用mybatis的逆向工程,在跟着视频中一模一样操作下来之后,最后执行程序的时候却报错了,说系统找不到指定路径。
项目结构如下:
执行程序如下:
public class MBGTest {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.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);
}
}
mbg.xml配置文件如下:
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
userId="root"
password="3333"/>
<!--指定domain生成的位置-->
<javaModelGenerator targetPackage="com.ssh.domain" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--指定sql映射文件生成的位置-->
<sqlMapGenerator targetPackage="com.ssh.dao" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--指定dao接口生成的位置-->
<javaClientGenerator targetPackage="com.ssh.dao" targetProject=".\src\main\java" type="XMLMAPPER"/>
<!--指定每个表的生成策略-->
<table tableName="tbl_emp" domainObjectName="Employee"/>
<table tableName="tbl_dept" domainObjectName="Department" />
</context>
</generatorConfiguration>
所有操作都是跟着视频做的,但是视频中能成功执行,我这里却报错了,错误如下:
路径应该是正确的,但是却提示找不到文件。
后来通过查找百度,试验了n种方法后,总算成功了。把配置文件和java代码种的所有的路径改成绝对路径后,就能成功执行程序了。如下:
- 配置文件:
- 代码:
最终结果:
成功生成dao等文件。遇到相同问题的小伙伴可以尝试一下。至于为什么按照视频种的路径写会报错,目前也不是很清楚,希望有大佬能指点一下。