DB2生成表model与mybatis的mapper中的常用方法

已经用这个工具很久了感觉很方便,该工具会将bd2中的表生成model与mapper以及mapper.xml中的增删改查的常用方法,话不多说,上菜...

[size=x-large][color=red][b]1.配置[/b][/color][/size]
[color=blue]<?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>[/color]

[size=x-large][color=red][b]2.执行 main方法[/b][/color][/size]

[color=blue]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();
}
}
}


[/color]


附件为详细的jar包以及代码,谢谢...
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis是一个优秀的ORM框架,可以帮助我们快速地进行数据库操作。而Mybatis代码自动生成工具可以帮助我们快速生成Java类和Mybatis对应的SQL语句,从而提高开发效率。以下是两种常用Mybatis代码自动生成工具: 1. Mybatis Generator Mybatis Generator是Mybatis官方提供的代码自动生成工具,可以根据数据库生成Java类和Mybatis对应的SQL语句。使用Mybatis Generator需要编写一个XML配置文件,指定数据库连接信息、生成的Java类和SQL语句的包名、生成等信息。以下是一个简单的Mybatis Generator配置文件示例: ```xml <?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> <context id="DB2Tables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="root" password="root"> </jdbcConnection> <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"> </javaModelGenerator> <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> </javaClientGenerator> <table tableName="user"></table> </context> </generatorConfiguration> ``` 2. Mybatis-Plus Generator Mybatis-Plus是Mybatis的增强工具,在Mybatis的基础上提供了更多的功能。Mybatis-Plus Generator是Mybatis-Plus提供的代码自动生成工具,可以快速生成Entity、MapperMapper XML、Service、Controller等各个模块的代码。使用Mybatis-Plus Generator需要编写一个Java类,指定数据库连接信息、生成的Java类和SQL语句的包名、生成等信息。以下是一个简单的Mybatis-Plus Generator配置类示例: ```java public class CodeGenerator { public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.example"); pc.setEntity("model"); pc.setMapper("mapper"); pc.setXml("mapper.xml"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setController("controller"); mpg.setPackageInfo(pc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setInclude("user"); mpg.setStrategy(strategy); // 执行生成 mpg.execute(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值