MybatisGenerator的使用方法

chanchaw 2018-10-14 09:52:26
我的JAVA项目采用SpringBoot + Mybatis + MybatisGenerator
我的MybatisGenerator 的配置文件是这样的:(其他配置不罗列了,下面只罗列生成的表的列表)
<table schema="rbac" tableName="%"></table> 

依据DB中的表生成ORM对应类时是将所有表一次性全部生成,这里要介绍我做开发的过程了:
DB中设计好一个模块的所有表,然后到JAVA上实现后端,然后DB中再设计下一个模块的表,在Eclipse中通过
MybatisGenerator生成对应的类(生成的类包括:model、dao、mapper),这样会将前面已经实现的模块用到的类
覆盖生成一遍,生成的dao类的代码如下:
package com.ccSoft.SBSMG.dao;

import com.ccSoft.SBSMG.model.User;
import com.ccSoft.SBSMG.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
long countByExample(UserExample example);

int deleteByExample(UserExample example);

int deleteByPrimaryKey(Integer uid);

int insert(User record);

int insertSelective(User record);

List<User> selectByExample(UserExample example);

User selectByPrimaryKey(Integer uid);

int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

int updateByExample(@Param("record") User record, @Param("example") UserExample example);

int updateByPrimaryKeySelective(User record);

int updateByPrimaryKey(User record);
}


可以看出来同时生成了example相关的代码,当涉及到较为复杂的SQL查询时,举例:
要查询获取某个部门下的所有用户,就不能直接使用上面代码中的selectByPrimaryKey,需要使用selectByExample,我的做法是在模型User对应的服务类UserServiceImpl中通过使用selectByExample来实现
这样就没有修改通过MybatisGenerator创建来的类,当制作下一个模块时就不会因为生成新模块的类而覆盖
已经实现的模块的类了(MybatisGenerator不生成UserService、UserServiceImpl)。
对于使用MybtisGenerator还有另外一种用法,就是在配置文件generatorConfig.xml中的
<table schema="rbac" tableName="%"></table>位置上每次只填写本次需要生成的表名。
那么问题来了,应该是采用我现在的做法 - 第一种做法,每次生成DB中全部表的对应类
还是应该采用第二种做法?请说出你的理由。

...全文
206 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chanchaw 2018-10-16
  • 打赏
  • 举报
回复
好吧,就你们2个建议了
咸哼酒家 2018-10-15
  • 打赏
  • 举报
回复
尝试封装mybatis-plus吧,只需转换数据库,各个项目随意使用,您的复制粘贴好帮手
tandy 2018-10-15
  • 打赏
  • 举报
回复
我一般是使用Generator每次只生成目前正在实现功能的表,功能实现完毕初步测试完毕后再生成下一个进行实现。
模块和模块之间也是分开实现的,比如在maven 下通过引入的方式。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧