mybatis generator 自定义类名(修改mybatis-generator-1.3.2源码改dao类名)

网上找了一大堆的例子,感觉这个比较全,比较细,就是写的比较多,如果想看详细的,请看下面的链接,自动忽略我的:

http://www.blogjava.net/bolo/archive/2015/03/20/423683.html

本篇只写手头只有一个eclipse的情况下(maven插件已经装好了),怎么修改dao的类名

1.建maven项目:

1)

2)

3)

4)

5)如果没有src/main/java src/main/resources这样的结构,那么项目右键-》properties->java build path.选择libraries选项卡,edit,弹出编辑对话框,选择workspace default jre,确定后刷新就出来了。

2.修改pom.xml

自动生成的pom.xml如图

在16、17行之间添加如下依赖:

<dependency>  
    <groupId>log4j</groupId>  
    <artifactId>log4j</artifactId>  
    <version>1.2.17</version>  
</dependency> 
<dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant</artifactId>
    <version>1.7.1</version>
</dependency>

保存。右键-》maven->update project一下,等源码拷进来就不会报错了

完整的如下:

<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>cn.xdf</groupId>
 <artifactId>Mybatis-Generator-Maven1</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>Mybatis-Generator-Maven1 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>log4j</groupId> 
 <artifactId>log4j</artifactId> 
 <version>1.2.17</version> 
 </dependency> 
 <dependency>
  <groupId>org.apache.ant</groupId>
  <artifactId>ant</artifactId>
  <version>1.7.1</version>
 </dependency>
 </dependencies>
 <build>
 <finalName>Mybatis-Generator-Maven1</finalName>
 </build>
</project>



3.下载源码,项目右键-》maven->download sources。

这时会在本地库里找到这个源码:

把这个文件解压,把org所在的整个包拷贝到src/main/java下

将generatorConfig.xml、log4j.properties拷贝到src/main/resources下


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>
	<!-- 数据库驱动 -->
	<classPathEntry location="D:mysql-connector-java-5.1.9.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<!-- <plugin type="cn.dulinan.mybatis.PaginationPlugin" />  -->
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://127.0.0.1:3306/maventest" userId="root"
			password="root">
		</jdbcConnection>
		<!-- <javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver> -->
		<javaModelGenerator targetPackage="cn.dulinan.domain"
			targetProject="cn.dulinan.ssm">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<sqlMapGenerator targetPackage="cn.dulinan.mapper"
			targetProject="cn.dulinan.ssm">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="cn.dulinan.dao" targetProject="cn.dulinan.ssm">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<table tableName="t_user" domainObjectName="User"></table>
<!-- 		<table tableName="user" domainObjectName="User"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table> -->
	</context>
</generatorConfiguration>  

上面的是把mysql-connector-java-5.1.9.jar直接放到D盘了,另外mysql连接的数据库,用户名,密码,及包名结构需要自己改一下


log4j.properties

#u5B9Au4E49LOGu8F93u51FAu7EA7u522B  
log4j.rootLogger=DEBUG,Console,File
#u5B9Au4E49u65E5u5FD7u8F93u51FAu76EEu7684u5730u4E3Au63A7u5236u53F0  
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#u53EFu4EE5u7075u6D3Bu5730u6307u5B9Au65E5u5FD7u8F93u51FAu683Cu5F0FuFF0Cu4E0Bu9762u4E00u884Cu662Fu6307u5B9Au5177u4F53u7684u683Cu5F0F  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
  
#u6587u4EF6u5927u5C0Fu5230u8FBEu6307u5B9Au5C3Au5BF8u7684u65F6u5019u4EA7u751Fu4E00u4E2Au65B0u7684u6587u4EF6  
log4j.appender.File = org.apache.log4j.RollingFileAppender
#u6307u5B9Au8F93u51FAu76EEu5F55  
log4j.appender.File.File = logs/ssm.log
#u5B9Au4E49u6587u4EF6u6700u5927u5927u5C0F  
log4j.appender.File.MaxFileSize = 10MB
# u8F93u51FAu6240u4EE5u65E5u5FD7uFF0Cu5982u679Cu6362u6210DEBUGu8868u793Au8F93u51FADEBUGu4EE5u4E0Au7EA7u522Bu65E5u5FD7  
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n

log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

4.修改文件

主要修改如下三个地方:

1)IntrospectedTable.java中

第820行,将 sb.append("Mapper"); //$NON-NLS-1$ 改成sb.append("Dao"); //修改Dao包名

2)添加GeneratorConfig.java文件,作为生成的入口

package org.mybatis.generator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;


public class GeneratorConfig {

	public static void main(String[] args) throws Exception{
	       List<String> warnings = new ArrayList<String>();
	       boolean overwrite = true;
	       File configFile = new File(GeneratorConfig.class.getResource("/generatorConfig.xml").toURI());
	       ConfigurationParser cp = new ConfigurationParser(warnings);
	       Configuration config = cp.parseConfiguration(configFile);
	       DefaultShellCallback callback = new DefaultShellCallback(overwrite);
	       MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
	       myBatisGenerator.generate(null);
	}
}

3)DefaultShellCallback.java,由于运行时,如果找不到xml配置的包名,在此类中会抛出异常,因此在这里,修改一下,直接创建文件

在这个类中第50行处,注释掉1,添加2



此时已修改完毕,在GeneratorConfig.java的main函数运行后,刷新项目,会有如下代码生成

包名已改好


该程序的可运行源码的下载地址:http://download.csdn.net/detail/dulinanaaa/9623132

原文地址:https://www.cnblogs.com/dulinan/p/12033093.html