MybatisGenerator的使用方法
我的JAVA项目采用SpringBoot + Mybatis + MybatisGenerator
我的MybatisGenerator 的配置文件是这样的:(其他配置不罗列了,下面只罗列生成的表的列表)
依据DB中的表生成ORM对应类时是将所有表一次性全部生成,这里要介绍我做开发的过程了:
DB中设计好一个模块的所有表,然后到JAVA上实现后端,然后DB中再设计下一个模块的表,在Eclipse中通过
MybatisGenerator生成对应的类(生成的类包括:model、dao、mapper),这样会将前面已经实现的模块用到的类
覆盖生成一遍,生成的dao类的代码如下:
可以看出来同时生成了example相关的代码,当涉及到较为复杂的SQL查询时,举例:
要查询获取某个部门下的所有用户,就不能直接使用上面代码中的selectByPrimaryKey,需要使用selectByExample,我的做法是在模型User对应的服务类UserServiceImpl中通过使用selectByExample来实现
这样就没有修改通过MybatisGenerator创建来的类,当制作下一个模块时就不会因为生成新模块的类而覆盖
已经实现的模块的类了(MybatisGenerator不生成UserService、UserServiceImpl)。
对于使用MybtisGenerator还有另外一种用法,就是在配置文件generatorConfig.xml中的
<table schema="rbac" tableName="%"></table>位置上每次只填写本次需要生成的表名。
那么问题来了,应该是采用我现在的做法 - 第一种做法,每次生成DB中全部表的对应类
还是应该采用第二种做法?请说出你的理由。
我的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中全部表的对应类
还是应该采用第二种做法?请说出你的理由。
...全文
相关推荐
MybatisGenerator
MybatisGenerator主要用于生成Mapping的映射文件,生成dao,mapping,model方便开发,提高开发效率。
MybatisGenerator源码
MybatisGenerator源码详情请参照http://www.cnblogs.com/daviddai/p/3485595.html
MyBatisGenerator
NULL 博文链接:https://jw1314.iteye.com/blog/1178997
mybatisGenerator-master
mybatisGenerator,可以逆向生成Model,DAO,Mapper,省时省力,提高开发效率
MybatisGenerator.rar
mybatis逆向工程工具项目,下载后只需要修改数据库配置信息便可运行使用。


Web 开发
