mybatis-逆向工程配置,以及自定义注释类

本文介绍了如何配置Mybatis的逆向工程,并提供了详细步骤,包括pom.xml中引入jar,创建dbconfig.properties,配置generatorConfig.xml以使用自定义注释类。还提醒了在逆向工程启动后需要刷新项目。此外,文章提到通过自定义CommentGenerator类并设置suppressAllComments属性为false来启用自定义注释。最后,分享了一种简便方法避免为每个表手动配置避免生成Example类。
摘要由CSDN通过智能技术生成

1.pom.xml引入jar

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>

建dbconfig.properties:

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=mysql

2.generatorConfig.xml

<commentGenerator type=".....">自己重写注释类要加type

<?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>
    <context id="DB2Tables" targetRuntime="MyBatis3">
                <!-- 自动生成序列化 -->
                <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
                <!-- 自动生成toString-->
                <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
                <!-- 是否去除自动生成的注释 -->
		<commentGenerator type=".....">
                        <property name="suppressDate" value="false"/>
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<!-- 配置数据库连接 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/ssm_crud" userId="root"
			password="mysql">
		</jdbcConnection>
                <!-- 默认为false,把JDBC DECIMAL 和NUMERIC类型解析为Integer,为true时把JDBC DECIMAL 和NUMERIC类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- 指定javaBean生成的位置,targetPackage:表示生成的路径,targetProject:表示生成的文件/项目下-->
		<javaModelGenerator targetPackage="cn.zhou.shu.ssm.bean" targetProject=".\src\main\java">
		    <!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="true" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!--mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
		    <!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<!-- dao接口生成的位置,mapper接口 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="cn.zhou.shu.ssm.dao" targetProject=".\src\main\java">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>


		<!-- table指定每个表的生成策略 -->
		<table tableName="tbl_emp" domainObjectName="Employee"></table>
		<table tableName="tbl_dept" domainObjectName="Department"></table>
	</context>

</generatorConfiguration>

3.逆向工程启动类:

package cn.zhou.shu.ssm.test;

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 test {
	
	public static void main(String[] args) throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		File configFile = new File("src/main/resources/generatorConfig.xml");
		// 创建配置解析器
		ConfigurationParser cp = new ConfigurationParser(warnings);
		// 调用解析器创建配置对象()
		Configuration config = cp.parseConfiguration(configFile);
		// 创建一个ShellCallback对象,shellCallback接口是处理文件的创建和合并,默认是不支持文件合并的。
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
                // 创建一个MyBatisGenerator对象。MyBatisGenerator类是真正用来执行生成动作的类
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);
		myBatisGenerator.generate(null);
	}

}

注意运行后要刷新项目

项目结构图:

------------------------------------------------------------------------------------------------------------------------------------

https://blog.csdn.net/u011781521/article/details/78161201

https://blog.csdn.net/sinat_34104446/article/details/88675224 

补充:

生成自定义注释:

1.MyBatis逆向工程默认使用的是org.mybatis.generator.api.CommentGenerator类的

org.mybatis.generator.internal.DefaultCommentGenerator子类生成的注释,

所以我们可以自己实现一个注释类:

2.generatorConfig.xml中

<!-- 通过type指定自定义的注释 -->
<commentGenerator type="cn.zhou.shu.ssm.test.MyCommentGenerator">
	   <!-- 不要开启,否则将不会使用自定义注释 -->
	   <!-- <property name="suppressAllComments" value=&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值