使用mybatis-generator生成PO中遇到的问题
配置文件
<?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>
<!-- 加载配置信息(数据库连接参数) -->
<properties resource="properties/db.properties" />
<!-- 数据库与java连接解决jar包
注 : 需要该包完整路径, 且目录变更为"/"
-->
<classPathEntry location="${classPath}" />
<!-- 一个库对应一个context -->
<context id="mysqlTables" targetRuntime="MyBatis3">
<!-- driverClass : mysql驱动
注 : 新版本5.7mysql, 驱动为 : com.mysql.cj.jdbc.Driver
-->
<!-- connectionURL:连接地址
注 : 我的地址 jdbc:mysql://localhost:3306/kindleio?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8
1. "jdbc:mysql://"别重复(xml,properties中只能出现一次)
2. "kindleio"为库名
3. "characterEncoding=utf8"为编码格式
4. "useSSL=false"高版本会要求是否使用ssl连接,如果没有此参数,会报错提示必须加上ssl属性
5. "serverTimezone=GMT%2b8"时区格式,如果没有此参数, 会报timezone错误
-->
<!-- 用户名 -->
<!-- password密码 -->
<jdbcConnection driverClass="${driver}"
connectionURL="${url}" userId="${user}"
password="${password}">
</jdbcConnection>
<!-- 自动类型转换 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成类位置 -->
<javaModelGenerator targetPackage="io.kindleclipt.entity.po"
targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper生成位置 -->
<sqlMapGenerator targetPackage="test.xml"
targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 接口位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="io.kindleclipt.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定表名,且必须 -->
<table tableName="t_books">
</table>
</context>
</generatorConfiguration>
Java代码
用来解析xml生成po类
package io.kindleclipt.utils;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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 POGenerator {
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
String genCfg = "/configs/mybatis-generator.xml";
File configFile = new File(POGenerator.class.getResource(genCfg).getFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
没有成功,明天继续调试