三步搞定maven中的mybatis Generator代码生成器

三步搞定maven中的mybatis Generator代码生成器

1.配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yrp</groupId>
    <artifactId>mygen</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
    </dependencies>
</project>

二.在src/resources下新建generatorConfig.xml

这边需要修改两个地方:
(1)数据库连接的信息:驱动类、连接地址、用户名、密码
(2)指定数据库表

<?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="testTables" targetRuntime="MyBatis3">
		<!-- 生成实体类实现序列化-->
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin"  />
		<!-- 实体类中包含toString() -->
		<plugin type="org.mybatis.generator.plugins.ToStringPlugin" ></plugin>
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/ego" userId="root"
			password="admin">
		</jdbcConnection>
		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO类的位置 -->
		<javaModelGenerator targetPackage="com.yrp.pojo"
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.yrp.mapperxml"
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.yrp.mapper"
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定数据库表 -->
		<table  tableName="tb_content"   enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_content_category" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_item" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_item_cat" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_item_desc" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_item_param" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_item_param_item" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_manager" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_order_item" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_order_shipping" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>
		<table  tableName="tb_user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="true" enableUpdateByExample="false"></table>



	</context>
</generatorConfiguration>

三.在src/main下新建类GeneratorSqlmap,并运行



import java.io.File;
import java.io.IOException;
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.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class GeneratorSqlmap {
   

	public void generator() throws Exception{
   

		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		//指定 逆向工程配置文件,注意,运行时如果报找不到xml文件,可以使用绝对路径试看看
		File configFile = new File("generatorConfig.xml");
		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);

	} 
	public static void main(String[] args) throws Exception {
   
		try {
   
			GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
			generatorSqlmap.generator();
		} catch (Exception e) {
   
			e.printStackTrace();
		}
		
	}

}

四.运行,完成!!!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2346.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • struts2标签详解[通俗易懂]

    struts2标签详解[通俗易懂]struts2标签讲解要使用Struts2的标签,只需要在JSP页面添加如下一行定义即可:<%@taglibprefix="s"uri="/struts-t

    2022年7月2日
    19
  • Java基础知识总结(2021版)「建议收藏」

    前言大家好,我是素小暖,2012年毕业,2016年通过培训转行java开发,今天2021年1月9日,转行之路跌跌绊绊,蓦然回首,已经满满的4年工作经验了?但感觉知识还是相当的匮乏,没自信,也许是努力程度还不够吧。很感谢CSDN,因为是它给了我学习的动力,之前写了一篇记录CSDN博客访问量的文章,也许大家感觉很幼稚,但真的很有用,很有效果,仿佛磕了药一样,努力学习,进步。2020年,是我较为成功的一年,工作上,跳了槽,涨了工资;学习上,啃了几本名著(EffectiveJava、重构改善既.

    2022年4月7日
    39
  • python安装不了whl文件_Python安装whl文件过程图解

    python安装不了whl文件_Python安装whl文件过程图解Python安装whl文件过程图解这篇文章主要介绍了Python安装whl文件过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下在命令指示符下(cmd)的Python3安装命令为:pip3install文件名.whl安装出错:matplotlib-2.0.0-cp34-cp34m-win_amd64.whlisnotasuppor…

    2022年5月29日
    68
  • vue的watch用法_vuewatch参数

    vue的watch用法_vuewatch参数watch:{“xx”:functtion(newValue,oldValue){console.log(newValue,oldValue)},immediate:true,//首次绑定值是否触发deep:true//深度监听}watch:{xx(newValue,oldValue){…

    2025年6月28日
    2
  • KAZE与SIFT算法比较

    KAZE与SIFT算法比较比较KAZE与SIFT的算法。根据测试结果,两种算法对于ubc、bikes、trees和boat四种图集都有很好的鲁棒性,能够准确将图像匹配起来。两种算法的差异主要是在bark、graf、leuven和wall图集中表现出来的。bark图集主要检验特征算法对旋转和缩放的鲁棒性。可见KAZE算法有效检测的特征点少于SIFT算法,而SIFT算

    2022年6月18日
    22
  • Spring Data JPA 之 JpaRepository

    Spring Data JPA 之 JpaRepositoryJpaRepository是Spring提供的非常强大的基本接口。1JpaRepository1.1JpaRepository接口定义JpaRepository接口的官方定义如下:publicinterfaceJpaRepository&lt;T,ID&gt;extendsPagingAndSortingRepository&lt;T,ID&gt;,Q…

    2022年10月20日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号