三步搞定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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • html 页面加载中 请稍候,html 提示“数据在加载中,请稍后……”

    html 页面加载中 请稍候,html 提示“数据在加载中,请稍后……”项目完成了不过因为FileNet加载数据比较慢,所以3-4条记录加载也至少要10几秒,所以客户提出要有一个提示”提示数据加载,请稍后……“这个问题。这个东西开始实现起来不太容易。开始有一个解决方案就是利用一个div,在div里面使用背景图片,加载一个gif动态的图片,再利用div的display可以实现提示。不过这个方法明显的不合适,所以又换了一种实现方式。效果如下图所示。js代码如下varo…

    2025年7月7日
    0
  • N的阶乘(大数阶乘算法)

    N的阶乘(大数阶乘算法)题目描述输入一个正整数N,输出N的阶乘。输入描述:正整数N(0<=N<=1000)输出描述:输入可能包括多组数据,对于每一组输入数据,输出N的阶乘输入例子:4515输出例子:241201307674368000#include<iostream>#include<cstring>usingnames…

    2022年7月24日
    7
  • Java连接MySQL mysql-connector-java-bin.jar驱动包的下载与安装

    Java连接MySQL mysql-connector-java-bin.jar驱动包的下载与安装eclipse在连接mysql数据库的时候要通过mysql驱动包进行连接首先进入官网中—-官网地址:https://dev.mysql.com/进入官网中选择DOWNLOADS(下载)2.选择下载中的mysql-connectors3.选择connector/JJ指的是Java4.接下在选择操作系统,此处选择platformindependent(独立于平台)…

    2022年5月21日
    1.1K
  • 进销存excel_用Excel制作简单的进销存系统「建议收藏」

    进销存excel_用Excel制作简单的进销存系统「建议收藏」最近刚好帮一个朋友做一个进销存系统,因为使用者对电脑操作以及Excel应用能力较弱,我做的进销存系统没有用特别复杂的功能,非常有解决意义,我将手把手将你制作一个简单的进销存系统。需求描述朋友找人合伙开了一个女装店,想要用Excel记录每天的销售数据、定期的进货数据,以及定期盘点库存情况。朋友的合伙人对电脑操作、Excel数据管理能力较弱,前期购买过专用的进销存软件,但是经常会把数据搞乱,因此放弃了…

    2022年5月31日
    54
  • springbean生命周期通俗一点_spring为啥是单例模式

    springbean生命周期通俗一点_spring为啥是单例模式一、Spring核心模块介绍1.SpringCore:Core封装包是框架的最基础部分,提供IOC和依赖注入特性。这里的基础概念是BeanFactory,它提供对Factory模式的经典实现来消除对程序性单例模式的需要,并真正地允许你从程序逻辑中分离出依赖关系和配置。2.SpringContext:构建于Core封装包基础上的Context封装包,提供了一种框架式的对象访问方法,有些象JNDI注册器。Context封装包的特性得自于Beans封装包,并添加了对国际化(I18N).

    2022年9月18日
    0
  • java 怎样卸载一个类_Java 动态卸载类[通俗易懂]

    java 怎样卸载一个类_Java 动态卸载类[通俗易懂]通过反射,我们可以动态的将类加载到方法区中,但是卸载这个类,却有着比较严苛的条件1.该类所有的实例都已经被GC,也就是JVM中不存在该Class的任何实例。2.加载该类的ClassLoader已经被GC。3.该类的java.lang.Class对象没有在任何地方被引用,如不能在任何地方通过反射访问该类的方法.publicstaticvoidmain(String[]args)th…

    2022年5月18日
    39

发表回复

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

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