mybatis generator 生成数据库注释DAO源码

  1. 重写一个注释类
  2. 打包 安装到maven
  3. 设置pom
  4. generatorConfig
  5. 生成源码 mybatis-generator:generate

写一个 MyCommentGenerator 类继承 DefaultCommentGenerator 重写addFieldComment方法
程序如下

package org.improve4meaven.generator;


import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.internal.DefaultCommentGenerator;

public class MyCommentGenerator extends DefaultCommentGenerator {

	
	
	@Override
	public void addFieldComment(Field field, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {
		// 添加字段注释
//		第一种  注释
		
//		StringBuffer sb = new StringBuffer();
//		field.addJavaDocLine("/**");
//
//		field.addJavaDocLine(" * <pre>");
//		if (introspectedColumn.getRemarks() != null)
//			field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
//		sb.append(" * 表字段 : ");
//		sb.append(introspectedTable.getFullyQualifiedTable());
//		sb.append('.');
//		sb.append(introspectedColumn.getActualColumnName());
//		field.addJavaDocLine(sb.toString());
//		field.addJavaDocLine(" * </pre>");
//		field.addJavaDocLine(" * ");
//		// addJavadocTag(field, false);
//		field.addJavaDocLine(" */");
		
//		第二种注释
		StringBuffer sb = new StringBuffer();

		if (introspectedColumn.getRemarks() != null)
			sb.append(" //" + introspectedColumn.getRemarks());
		sb.append("  --- ");
		sb.append(introspectedTable.getFullyQualifiedTable());
		sb.append('.');
		sb.append(introspectedColumn.getActualColumnName());
		field.addJavaDocLine(sb.toString());
	}
}

2、导出jar 包 并且安装到 meaven

mvn install:install-file -Dfile=MyCommentGenerator.jar -DgroupId=org.improve4meaven.generator -DartifactId=MyCommentGenerator -Dversion=1.0.SNAPSHOT -Dpackaging=jar
详细解释

3、设置pom

根据别的普通包配置即可

<dependency>
			<groupId>org.improve4meaven.generator</groupId>
			<artifactId>MyCommentGenerator</artifactId>
			<version>1.0.SNAPSHOT</version>
		</dependency>
	

插件依赖配置

<build>
		<!-- 项目名称 -->
		<finalName>TestMybatis</finalName>
		<!-- 插件 -->
		<plugins>
			<plugin>
				<!-- mybatis-generator:generate 使用的插件 -->
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<configuration>
					<!--允许移动生成的文件 -->
					<verbose>true</verbose>
					<!--允许覆盖生成的文件 -->
					<overwrite>true</overwrite>
				</configuration>
				<!-- 插件所依赖的包 -->
				<dependencies>
					<dependency>
						<groupId>org.improve4meaven.generator</groupId>
						<artifactId>MyCommentGenerator</artifactId>
						<version>1.0.SNAPSHOT</version>
					</dependency>
				</dependencies>
			</plugin>

		</plugins>
	</build>

4、generatorConfig配置

注意:commentGenerator type=“org.improve4meaven.generator.MyCommentGenerator”>

<?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="D:\mysqldriver\mysql-connector-java-5.1.26.jar" />
	<context id="context1" targetRuntime="MyBatis3">
		<commentGenerator type="org.improve4meaven.generator.MyCommentGenerator">
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		
		
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://127.0.0.1:3309/test?characterEncoding=utf8&amp;useSSL=true&amp;createDatabaseIfNotExist=true&amp;serverTimezone=GMT&amp;nullNamePatternMatchesAll=true"
			userId="root" password="sadsad" >
			<property name="useInformationSchema" value="true"></property>
			</jdbcConnection>

		<javaModelGenerator targetPackage="org.share.domain.tree.dao"
			targetProject="src/main/java" />

		<sqlMapGenerator targetPackage="xml" targetProject="src/main/resources" />

		<javaClientGenerator targetPackage="org.share.domain.tree.mapper"
			targetProject="src/main/java" type="XMLMAPPER" >
			 <property name="enableSubPackages" value="true"/>
			 </javaClientGenerator>

		<!-- shema 数据库 tableName表明 -->
		<table schema="test" tableName="sm_user" domainObjectName="SMUser" 
	  			enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true">
		</table>
	</context>
</generatorConfiguration>

5、
项目 右键–》run as --》 maven bulid --》弹出对话框 --》在goals中输入mybatis-generator:generate
或者 点击select --》选择你的mybatis插件 --》apply --》run

mybatis-generator:generate

生成三个文件,如下图
这里写图片描述

DAO 如下

package org.share.domain.tree.dao;

public class SMUser {
     //主键  --- sm_user.pk_user
    private Integer pkUser;

     //姓名  --- sm_user.name
    private String name;

    public Integer getPkUser() {
        return pkUser;
    }

    public void setPkUser(Integer pkUser) {
        this.pkUser = pkUser;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

大概如此,如果有什么不懂得 可以留言

参考地址
jar包下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值