DB2生成表model与mybatis的mapper中的常用方法 博客分类: mybatis db2mybatis
程序员文章站
2024-03-23 11:23:16
...
已经用这个工具很久了感觉很方便,该工具会将bd2中的表生成model与mapper以及mapper.xml中的增删改查的常用方法,话不多说,上菜...
1.配置
<?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="E:\db2jcc_license_cu.jar" />
<classPathEntry location="E:\db2jcc4.jar" />
<classPathEntry location="E:\db2java.jar" />
<context id="context1" defaultModelType="flat" targetRuntime="MyBatis3">
<!-- 为了防止生成的代码中有很多注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 注释控制完毕 -->
<jdbcConnection driverClass="com.ibm.db2.jcc.DB2Driver"
connectionURL="数据库地址" userId="用户名"
password="密码">
</jdbcConnection>
<!--targetPackage包路径 targetProject:src绝对路径 -->
<!-- bean example -->
<javaModelGenerator targetPackage="com.test.model"<!--此处的targetPackage为要生成的model的项目路径,对应到要应用项目的路径-->
targetProject="E:\test"><!--此处的targetProject为该model生成到本地的地址-->
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper.xml -->
<sqlMapGenerator targetPackage="com.fuiou.fwp.db.mapper"<!--此处的targetPackage为要生成的model的项目路径,对应到要应用项目的路径-->
targetProject="E:\test"><!--此处的targetProject为该mapper生成到本地的地址-->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mapper接口 -->
<javaClientGenerator targetPackage="com.fuiou.fwp.db.mapper"
targetProject="E:\test" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--数据库表 -->
<table schema="" tableName="xxxx" enableSelectByExample="true"<!--tableName为你要生成的表的名字-->
enableDeleteByExample="true" enableCountByExample="true"
enableUpdateByExample="true">
<property name="useActualColumnNames" value="false" />
</table>
</context>
</generatorConfiguration>
2.执行 main方法
package com.***.test;
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 MybatisTool {
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
String genCfg = "mbgConfiguration.xml";
System.out.println(MybatisTool.class.getResource(genCfg));
File configFile = new File(MybatisTool.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();
}
}
}
附件为详细的jar包以及代码,谢谢...
1.配置
<?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="E:\db2jcc_license_cu.jar" />
<classPathEntry location="E:\db2jcc4.jar" />
<classPathEntry location="E:\db2java.jar" />
<context id="context1" defaultModelType="flat" targetRuntime="MyBatis3">
<!-- 为了防止生成的代码中有很多注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 注释控制完毕 -->
<jdbcConnection driverClass="com.ibm.db2.jcc.DB2Driver"
connectionURL="数据库地址" userId="用户名"
password="密码">
</jdbcConnection>
<!--targetPackage包路径 targetProject:src绝对路径 -->
<!-- bean example -->
<javaModelGenerator targetPackage="com.test.model"<!--此处的targetPackage为要生成的model的项目路径,对应到要应用项目的路径-->
targetProject="E:\test"><!--此处的targetProject为该model生成到本地的地址-->
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper.xml -->
<sqlMapGenerator targetPackage="com.fuiou.fwp.db.mapper"<!--此处的targetPackage为要生成的model的项目路径,对应到要应用项目的路径-->
targetProject="E:\test"><!--此处的targetProject为该mapper生成到本地的地址-->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mapper接口 -->
<javaClientGenerator targetPackage="com.fuiou.fwp.db.mapper"
targetProject="E:\test" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--数据库表 -->
<table schema="" tableName="xxxx" enableSelectByExample="true"<!--tableName为你要生成的表的名字-->
enableDeleteByExample="true" enableCountByExample="true"
enableUpdateByExample="true">
<property name="useActualColumnNames" value="false" />
</table>
</context>
</generatorConfiguration>
2.执行 main方法
package com.***.test;
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 MybatisTool {
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
String genCfg = "mbgConfiguration.xml";
System.out.println(MybatisTool.class.getResource(genCfg));
File configFile = new File(MybatisTool.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();
}
}
}
附件为详细的jar包以及代码,谢谢...