1. 程式人生 > >mybatis generator 生成資料庫註釋DAO原始碼

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;
    }
}

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