mybatis自定义代码生成器(Generator)——自动生成model&dao代码
花了两天的时间研究了下mybatis的generator大体了解了其生成原理以及实现过程。感觉generator做的非常不错,给开发者也留足了空间。看完之后在generator的基础上实现了自定义的生成器。代码start.....
建立了一个maven工程(common)项目结构:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.yangchao</groupId>
- <artifactId>project-common</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>project-common Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-api</artifactId>
- <version>7.0</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.compass-project/compass -->
- <dependency>
- <groupId>org.compass-project</groupId>
- <artifactId>compass</artifactId>
- <version>2.0.2</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
- <dependency>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-core</artifactId>
- <version>1.3.2</version>
- </dependency>
- </dependencies>
- </project>
-------------------------------------------------------------------jdbc.properties--------------------------------------------------------------------
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/demo1
- jdbc.username=root
- jdbc.password=root
- initialSize=0
- maxActive=20
- maxIdle=20
- minIdle=1
- maxWait=60000
--------------------------------------------------------------------generatorConfig.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>
- <!--加载属性文件 -->
- <properties resource="jdbc.properties" />
- <context id="context1" targetRuntime="MyBatis3">
- <!-- 实现自定义的代码生成器plugin -->
- <plugin type="mybatis.PaginationPlugin" />
- <commentGenerator>
- <property name="suppressDate" value="true" />
- <!-- 是否去除自动生成的注释 true:是 : false:否 -->
- <property name="suppressAllComments" value="true" />
- </commentGenerator>
- <!-- 数据库连接URL,用户名,密码 -->
- <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
- password="${jdbc.password}" />
- <!--生成模型的包名和位置 -->
- <javaModelGenerator targetPackage="common.model" targetProject="project-common/src/main/java/" />
- <!--映射文件的包名和位置 -->
- <sqlMapGenerator targetPackage="common.model" targetProject="project-common/src/main/java/" />
- <!--DAO的包名和位置 -->
- <javaClientGenerator targetPackage="common.dao" targetProject="project-common/src/main/java" type="XMLMAPPER" />
- <!--要生成哪些表 -->
- <table tableName="%" enableSelectByExample="false" enableDeleteByExample="false"
- enableCountByExample="false" enableUpdateByExample="false"
- selectByExampleQueryId="false">
- <property name="rootClass" value="common.BaseEntity" />
- </table>
- </context>
- </generatorConfiguration>
-------------------------------------------------------PaginationPlugin.java------------------------------------------------------
- /**
- * @项目名称:project-common
- * @类名称:PaginationPlugin
- * @类描述:自定义代码生成器
- * @创建人:YangChao
- * @作者单位:北京宝库在线网络技术有限公司
- * @联系方式:YangChao@baoku.com
- * @创建时间:2016年9月5日 下午3:14:38
- * @version 1.0.0
- */
- public class PaginationPlugin extends PluginAdapter {
- /**
- * 生成dao
- */
- @Override
- public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
- FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("BaseDao<" + introspectedTable.getBaseRecordType() + ">");
- FullyQualifiedJavaType imp = new FullyQualifiedJavaType("common.BaseDao");
- interfaze.addSuperInterface(fqjt);// 添加 extends BaseDao<User>
- interfaze.addImportedType(imp);// 添加import common.BaseDao;
- interfaze.getMethods().clear();
- return true;
- }
- /**
- * 生成实体中每个属性
- */
- @Override
- public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass,
- IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
- return true;
- }
- /**
- * 生成实体
- */
- @Override
- public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
- addSerialVersionUID(topLevelClass, introspectedTable);
- return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
- }
- /**
- * 生成mapping
- */
- @Override
- public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
- return super.sqlMapGenerated(sqlMap, introspectedTable);
- }
- /**
- * 生成mapping 添加自定义sql
- */
- @Override
- public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
- String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();// 数据库表名
- List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
- XmlElement parentElement = document.getRootElement();
- // 添加sql——where
- XmlElement sql = new XmlElement("sql");
- sql.addAttribute(new Attribute("id", "sql_where"));
- XmlElement where = new XmlElement("where");
- StringBuilder sb = new StringBuilder();
- for (IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
- XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
- sb.setLength(0);
- sb.append(introspectedColumn.getJavaProperty());
- sb.append(" != null"); //$NON-NLS-1$
- isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
- where.addElement(isNotNullElement);
- sb.setLength(0);
- sb.append(" and ");
- sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
- sb.append(" = "); //$NON-NLS-1$
- sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
- isNotNullElement.addElement(new TextElement(sb.toString()));
- }
- sql.addElement(where);
- parentElement.addElement(sql);
- //添加getList
- XmlElement select = new XmlElement("select");
- select.addAttribute(new Attribute("id", "getList"));
- select.addAttribute(new Attribute("resultMap", "BaseResultMap"));
- select.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
- select.addElement(new TextElement(" select * from "+ introspectedTable.getFullyQualifiedTableNameAtRuntime()));
- XmlElement include = new XmlElement("include");
- include.addAttribute(new Attribute("refid", "sql_where"));
- select.addElement(include);
- parentElement.addElement(select);
- return super.sqlMapDocumentGenerated(document, introspectedTable);
- }
- @Override
- public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element,
- IntrospectedTable introspectedTable) {
- return false;
- }
- @Override
- public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
- return false;
- }
- @Override
- public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
- IntrospectedTable introspectedTable) {
- // LIMIT5,10; // 检索记录行 6-15
- // XmlElement isNotNullElement = new XmlElement("if");//$NON-NLS-1$
- // isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitStart >=0"));//$NON-NLS-1$ //$NON-NLS-2$
- // isNotNullElement.addElement(new
- // TextElement("limit ${limitStart} , ${limitEnd}"));
- // element.addElement(isNotNullElement);
- // LIMIT 5;//检索前 5个记录行
- return super.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
- }
- /**
- * mapping中添加方法
- */
- // @Override
- public boolean sqlMapDocumentGenerated2(Document document, IntrospectedTable introspectedTable) {
- String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();// 数据库表名
- List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
- // 添加sql
- XmlElement sql = new XmlElement("select");
- XmlElement parentElement = document.getRootElement();
- XmlElement deleteLogicByIdsElement = new XmlElement("update");
- deleteLogicByIdsElement.addAttribute(new Attribute("id", "deleteLogicByIds"));
- deleteLogicByIdsElement
- .addElement(new TextElement(
- "update "
- + tableName
- + " set deleteFlag = #{deleteFlag,jdbcType=INTEGER} where id in "
- + " <foreach item=\"item\" index=\"index\" collection=\"ids\" open=\"(\" separator=\",\" close=\")\">#{item}</foreach> "));
- parentElement.addElement(deleteLogicByIdsElement);
- XmlElement queryPage = new XmlElement("select");
- queryPage.addAttribute(new Attribute("id", "queryPage"));
- queryPage.addAttribute(new Attribute("resultMap", "BaseResultMap"));
- queryPage.addElement(new TextElement("select "));
- XmlElement include = new XmlElement("include");
- include.addAttribute(new Attribute("refid", "Base_Column_List"));
- queryPage.addElement(include);
- queryPage.addElement(new TextElement(" from " + tableName + " ${sql}"));
- parentElement.addElement(queryPage);
- return super.sqlMapDocumentGenerated(document, introspectedTable);
- }
- private void addSerialVersionUID(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
- CommentGenerator commentGenerator = context.getCommentGenerator();
- Field field = new Field();
- field.setVisibility(JavaVisibility.PRIVATE);
- field.setType(new FullyQualifiedJavaType("long"));
- field.setStatic(true);
- field.setFinal(true);
- field.setName("serialVersionUID");
- field.setInitializationString("1L");
- commentGenerator.addFieldComment(field, introspectedTable);
- topLevelClass.addField(field);
- }
- /*
- * Dao中添加方法
- */
- private Method generateDeleteLogicByIds(Method method, IntrospectedTable introspectedTable) {
- Method m = new Method("deleteLogicByIds");
- m.setVisibility(method.getVisibility());
- m.setReturnType(FullyQualifiedJavaType.getIntInstance());
- m.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "deleteFlag", "@Param(\"deleteFlag\")"));
- m.addParameter(new Parameter(new FullyQualifiedJavaType("Integer[]"), "ids", "@Param(\"ids\")"));
- context.getCommentGenerator().addGeneralMethodComment(m, introspectedTable);
- return m;
- }
- /*
- * 实体中添加属性
- */
- private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
- CommentGenerator commentGenerator = context.getCommentGenerator();
- Field field = new Field();
- field.setVisibility(JavaVisibility.PROTECTED);
- field.setType(FullyQualifiedJavaType.getIntInstance());
- field.setName(name);
- field.setInitializationString("-1");
- commentGenerator.addFieldComment(field, introspectedTable);
- topLevelClass.addField(field);
- char c = name.charAt(0);
- String camel = Character.toUpperCase(c) + name.substring(1);
- Method method = new Method();
- method.setVisibility(JavaVisibility.PUBLIC);
- method.setName("set" + camel);
- method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), name));
- method.addBodyLine("this." + name + "=" + name + ";");
- commentGenerator.addGeneralMethodComment(method, introspectedTable);
- topLevelClass.addMethod(method);
- method = new Method();
- method.setVisibility(JavaVisibility.PUBLIC);
- method.setReturnType(FullyQualifiedJavaType.getIntInstance());
- method.setName("get" + camel);
- method.addBodyLine("return " + name + ";");
- commentGenerator.addGeneralMethodComment(method, introspectedTable);
- topLevelClass.addMethod(method);
- }
- public boolean validate(List<String> arg0) {
- return true;
- }
- public static void generate() {
- String config = PaginationPlugin.class.getClassLoader().getResource("mybatisConfig.xml").getFile();
- String[] arg = { "-configfile", config, "-overwrite" };
- ShellRunner.main(arg);
- }
- public static void main(String[] args) {
- generate();
- }
- }
- /**
- * @项目名称:project-common
- * @类名称:BaseDao
- * @类描述:
- * @创建人:YangChao
- * @作者单位:北京宝库在线网络技术有限公司
- * @联系方式:YangChao@baoku.com
- * @创建时间:2016年9月5日 下午2:51:19
- * @version 1.0.0
- */
- public interface BaseDao<T> {
- public T selectByPrimaryKey(Integer id);
- public int deleteByPrimaryKey(Integer id);
- public int insertSelective(T t);
- public int updateByPrimaryKeySelective(T t);
- public List<T> getList(T t);
- // 获取数量
- public int getCountSelective(T t);
- /**
- *
- * @Title: findPage
- * @Description: TODO()
- * @param page
- * 分页参数
- * @param sql
- * mybatis sql语句
- * @param values
- * 命名参数,按名称绑定
- * @return 分页查询结果, 附带结果列表及所有查询时的参数.
- * @author YangChao
- * @date 2016年9月7日 下午5:30:28
- */
- public PageView<T> findPage(final PageView<T> page, final String sql, final Map<String, Object> values);
- }
- /**
- * @项目名称:project-common
- * @类名称:BaseEntity
- * @类描述:所有实体类的父类。可将公共的属性所有类序列化集中在此类中
- * @创建人:YangChao
- * @作者单位:北京宝库在线网络技术有限公司
- * @联系方式:YangChao@baoku.com
- * @创建时间:2016年9月5日 上午11:37:02
- * @version 1.0.0
- */
- public abstract class BaseEntity implements Serializable {
- private static final long serialVersionUID = 1L;
- private Integer id;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- }
---------------------------------------------------------------------------------最终生成dao&&model------------------------------------------------------------------------------------------------
- package common.dao;
- import common.BaseDao;
- import common.model.User;
- public interface UserMapper extends BaseDao<User> {
- }
---model---
- package common.model;
- import common.BaseEntity;
- public class User extends BaseEntity {
- private Integer accountId;
- private String loginname;
- private String password;
- private Integer status;
- private static final long serialVersionUID = 1L;
- public Integer getAccountId() {
- return accountId;
- }
- public void setAccountId(Integer accountId) {
- this.accountId = accountId;
- }
- public String getLoginname() {
- return loginname;
- }
- public void setLoginname(String loginname) {
- this.loginname = loginname;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Integer getStatus() {
- return status;
- }
- public void setStatus(Integer status) {
- this.status = status;
- }
- }
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="common.dao.UserMapper" >
- <resultMap id="BaseResultMap" type="common.model.User" >
- <id column="id" property="id" jdbcType="INTEGER" />
- <result column="account_id" property="accountId" jdbcType="INTEGER" />
- <result column="loginname" property="loginname" jdbcType="VARCHAR" />
- <result column="password" property="password" jdbcType="VARCHAR" />
- <result column="status" property="status" jdbcType="INTEGER" />
- </resultMap>
- <sql id="Base_Column_List" >
- id, account_id, loginname, password, status
- </sql>
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from user
- where id = #{id,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from user
- where id = #{id,jdbcType=INTEGER}
- </delete>
- <insert id="insertSelective" parameterType="common.model.User" >
- insert into user
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- id,
- </if>
- <if test="accountId != null" >
- account_id,
- </if>
- <if test="loginname != null" >
- loginname,
- </if>
- <if test="password != null" >
- password,
- </if>
- <if test="status != null" >
- status,
- </if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="id != null" >
- #{id,jdbcType=INTEGER},
- </if>
- <if test="accountId != null" >
- #{accountId,jdbcType=INTEGER},
- </if>
- <if test="loginname != null" >
- #{loginname,jdbcType=VARCHAR},
- </if>
- <if test="password != null" >
- #{password,jdbcType=VARCHAR},
- </if>
- <if test="status != null" >
- #{status,jdbcType=INTEGER},
- </if>
- </trim>
- </insert>
- <update id="updateByPrimaryKeySelective" parameterType="common.model.User" >
- update user
- <set >
- <if test="accountId != null" >
- account_id = #{accountId,jdbcType=INTEGER},
- </if>
- <if test="loginname != null" >
- loginname = #{loginname,jdbcType=VARCHAR},
- </if>
- <if test="password != null" >
- password = #{password,jdbcType=VARCHAR},
- </if>
- <if test="status != null" >
- status = #{status,jdbcType=INTEGER},
- </if>
- </set>
- where id = #{id,jdbcType=INTEGER}
- </update>
- <sql id="sql_where" >
- <where >
- <if test="accountId != null" >
- and account_id = #{accountId,jdbcType=INTEGER}
- </if>
- <if test="loginname != null" >
- and loginname = #{loginname,jdbcType=VARCHAR}
- </if>
- <if test="password != null" >
- and password = #{password,jdbcType=VARCHAR}
- </if>
- <if test="status != null" >
- and status = #{status,jdbcType=INTEGER}
- </if>
- </where>
- </sql>
- <select id="getList" resultMap="BaseResultMap" parameterType="common.model.User" >
- select * from user
- <include refid="sql_where" />
- </select>
- </mapper>
mybatis自定义代码生成器(Generator)——自动生成model&dao代码的更多相关文章
- MyBatis 使用Generator自动生成Model , Dao, mapper
最近 我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架. 听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资 ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- mybatis generator.xml 配置 自动生成model,dao,mapping
generator.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE gener ...
- SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码
1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...
- idea使用generator自动生成model、mapper、mapper.xml(转)
原文链接:http://www.mamicode.com/info-detail-445217.html TEP 0.在Intellij IDEA创建maven项目(本过程比较简单,略) STEP 1 ...
- 使用Generator 自动生成 model mapper mapping 文件
1.下载包 地址http://download.csdn.net/detail/u012909091/7206091 2.下载完成解压文件到任意目录 3.删除下mybatis-generator-co ...
- 利用generator自动生成model(实体)、dao(接口)、mapper(映射)
1 在MySQL数据库中创建相应的表 /* Navicat MySQL Data Transfer Source Server : 虚拟机_zeus01 Source Server Version : ...
- C# 连接SQLServer数据库自动生成model类代码
Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading ...
- mybatis自动生成model、dao及对应的mapper.xml文件
背景: 日常开发中,如果新建表,手动敲写model.dao和对应的mapper.xml文件,费时费力且容易出错, 所以采用mybatis自动生成model.dao及对应的mapper.xml文件.代码 ...
随机推荐
- Mahout系列之----kmeans 聚类
Kmeans是最经典的聚类算法之一,它的优美简单.快速高效被广泛使用. Kmeans算法描述 输入:簇的数目k:包含n个对象的数据集D. 输出:k个簇的集合. 方法: 从D中任意选择k个对象作为初始簇 ...
- 浅析GDAL库C#版本支持中文路径问题
GDAL库对于C#的支持问题还是蛮多的,对于中文路径的支持就是其中之一(另一个就是通过OGR库获取图形的坐标信息). 关于C#支持中文路径,看过我之前博客的应该都不陌生,如果使用的是我修改过的GDAL ...
- RB-tree (红黑树)相关问题
今天被问到了红黑树的规则,简述总结一下: 1.每个节点不是红色就是黑色. 2.根节点为黑色. 3.如果节点为红,其子节点必须为黑. 4.任一节点至NULL(树尾端)的任何路径,所含之黑节点数必须相同. ...
- gdb学习(二)[第二版]
查看运行时数据 print - 查看变量值 ptype – 查看变量类型 #ptype i #ptype "aaa" 打印字符串"aaa"的类型 #ptype ...
- Xcode模拟器中无法播放音频文件的原因分析
在本猫的Mac Mini上开发iOS app,发现当执行到播放音频的代码时,发生错误,log如下: 2015-10-05 07:22:17.122 LearnSpriteBuilder[10321:5 ...
- 关于使用Xcode自带的单元测试UnitTest的介绍
什么是单元测试? 单元测试就是为你的方法专门多写一个测试函数.以保证你的方法在不停的修改开发中.保持正确.如果出错,第一时间让你知道,这样从最小单位开始监控来保证软件的质量. 什么时候用到单元测试: ...
- TCP的核心系列 — 重传队列的更新和时延的采样(二)
在tcp_clean_rtx_queue()中,并非对每个ACK都进行时延采样.是否进行时延采样,跟这个ACK是否为 重复的ACK.这个ACK是否确认了重传包,以及是否使用时间戳选项都有关系. 本文主 ...
- Linux:进程通信之消息队列Message实例
/*send.c*/ /*send.c*/ #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h&g ...
- 一键安装 redmine on windows 和发邮件设置
一键安装 redmine on windows 和发邮件设置 1)使用http://bitnami.org/stack/redmine一键安装redmine (windows). 2)修改下面的文件: ...
- Media Player Classic - HC 源代码分析 4:核心类 (CMainFrame)(3)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...