Code generator for MyBatis and iBATIS.
它将为所有版本的MyBatis生成代码,以及版本2.2.0之后的iBATIS版本。它将内省一个数据库表(或多个表),并生成可用于访问表的构件。这减少了设置对象和配置文件与数据库表交互的最初麻烦。MBG试图对简单CRUD(创建、检索、更新、删除)的大量数据库操作产生重大影响。
基于官方源码更改,仅供测试和学习使用。GitHub地址
包结构:
.
├── dao
│ └── UserMapper.java
├── dto
│ └── User.java
├── example
│ └── UserExample.java
├── mapping
│ └── UserMapper.xml
└── po
└── UserPoService.java
在逆向工程的同时帮你生成好poService,下面为后续演示实例的最的poService生成效果。
@Service
public class UserPoService {
@Resource
private UserMapper userMapper;
/**
* javaPoServiceGenerator标签默认生成
*/
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
}
/**
* javaPoServiceGenerator标签默认生成
*/
public int updateByPrimaryKeySelective(User record) {
return userMapper.updateByPrimaryKeySelective(record);
}
/**
* enableSelectByExample = true
*/
public List queryUserMobile(String mobile) {
return queryUserMobile(mobile, null);
}
/**
* enableSelectByExample = true
*/
public List queryUserMobile(String mobile, Short registerType) {
UserExample e = new UserExample();
UserExample.Criteria criteria = e.createCriteria();
if (mobile != null) { criteria.andMobileEqualTo(mobile);}
if (registerType != null) { criteria.andRegisterTypeEqualTo(registerType);}
return userMapper.selectByExample(e);
}
/**
* enableSelectByExample = true
*/
public List queryUserId(String userId) {
UserExample e = new UserExample();
UserExample.Criteria criteria = e.createCriteria();
if (userId != null) { criteria.andUserIdEqualTo(userId);}
return userMapper.selectByExample(e);
}
/**
* enableUpdateByExample="true"和enablePostPositionQuery="true" 都为true时,生成的更新方法会同时查询最新的数据
*
* @return
*/
public List updateUserMobileByCondition(User dto, String mobile, Short registerType) {
UserExample e = new UserExample();
UserExample.Criteria criteria = e.createCriteria();
if (mobile != null) { criteria.andMobileEqualTo(mobile);}
if (registerType != null) { criteria.andRegisterTypeEqualTo(registerType);}
userMapper.updateByExampleSelective(dto, e);
return userMapper.selectByExample(e);
}
/**
* enableUpdateByExample="true" 只会进行更新,没有返回结果
*/
public void updateIdByCondition(User dto, Long id) {
UserExample e = new UserExample();
UserExample.Criteria criteria = e.createCriteria();
if (id != null) { criteria.andIdEqualTo(id);}
userMapper.updateByExampleSelective(dto, e);
}
}
jar都在当前目录下:
在jar文件目录下打开终端直接执行
java -jar mybatis-generator-core-1.3.7-SNAPSHOT.jar -configfile E:\IDEA\generator\Hello.xml -overwrite -verbose
maven核心配置如下
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
dev
dev
注意,这个插件没有时,你可能需要先下载到本地,再导入到你的仓库中才能被使用。
org.mybatis.generator.cx
mybatis-generator-maven-plugin
1.3.7-SNAPSHOT
配置信息:
org.mybatis.generator.cx
mybatis-generator-maven-plugin
1.3.7-SNAPSHOT
${mybatis.jdbc.driver}
${mybatis.jdbc.url}
${mybatis.jdbc.username}
${mybatis.jdbc.password}
true
true
mysql
mysql-connector-java
5.1.46
在项目src/java/resource下建立generatorConfig.xml配置文件,这里和方式区别的地方在于jdbcConnection改为读取pom里的配置(固定写死在xml配置也是没问题的)。
默认情况下生成的每一个poService都有2个方法,主键更新和主键查询:
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
}
public int updateByPrimaryKeySelective(User record) {
return userMapper.updateByPrimaryKeySelective(record);
}
generatedMethods:包含了一组generatedMethod标签。
generatedMethod:要生产的方法列表,它有5个属性:
属性 | 描述 | 实例 |
---|---|---|
methodName | 方法名,程序会基于此参数进行生成查询和更新方法 | userMobile -> queryUserMobile、updateUserMobileByCondition |
columns | 要查询的字段名,多个用‘,’分割,注意使用的是数据库的原始字段名 | 单个:“user_id”,多个:“mobile,register_type” |
enableSelectByExample | 生成查询方法 | true |
enableUpdateByExample | 生成更新方法 | true |
enablePostPositionQuery | 不能单独使用,和更新方法配合使用,启用时将在更新完毕后查询最新的数据库数据,见实例poService的updateUserMobileByCondition 和 updateIdByCondition方法 | true |
懒出高效,未完待续。Github地址:https://github.com/Ccccx/generator
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。