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


相关推荐

  • 北京异地居住证怎么办理(半异地居住证手续)

    让开,让我来,现在的回答都太远古了!我来给大家分享下流程:异地办理护照提供材料1、按居住证类型预约身份证(原件及复印件)+居住证(原件及复印件)2、按就业人员类型预约身份证+户口本+暂住证(工作居住证)或居住证身份证需正反面复印+户口本首页+本人页+暂住证或工作居住证复印有效期页的所有信息页3、按非本市配偶类型预约身份证+户口本+结婚证+配偶一方的户口本的复印件…

    2022年4月11日
    100
  • JAVASCRIPT 上传文件的几种方式「建议收藏」

    JAVASCRIPT 上传文件的几种方式「建议收藏」方法1:使用ajax,通过formdata传参//注意:FormData只兼容到IE10varformData=newFormData();varfile=$(‘.import-file-btn’).get(0).files[0];formData.append(‘file’,file);formData.append(‘id’,id);formData.append(‘name’,name);$.ajax({url:’user/validate_impo

    2022年10月22日
    0
  • top命令详解(转载)「建议收藏」

    top命令详解(转载)「建议收藏」top命令用法top命令经常用来监控linux的系统状况,是常用的性能分析工具,能够实时显示系统中各个进程的资源占用情况。top的使用方式top[-dnumber]|top[-bnp]参数解释:-d:number代表秒数,表示top命令显示的页面更新一次的间隔。默认是5秒。-b:以批次的方式执行top。-n:与-b配合使用,表示需要进行几次top命令的输出结果。-p:指定特定的pid进程号进行观察。在top命令显示的页面还可以输…

    2022年9月24日
    0
  • 流量矿场[通俗易懂]

    第二个流量魔盒,流量矿场,扫码注册,实名秒通过不刷脸,现在活动:实名通过秒送1台魔盒机,25天产13个币,现在币开盘价格0.11美元,估计后期会涨,不要错过第二个流量魔盒。速度撸起来。线下已经5块回收了!注册链接:http://sina.lt/fyFz苹果APP下载地址:https://9dun.cn/s/zjmalls 安卓APP下载地址:https://9dun.cn/s/zjmallss…

    2022年4月18日
    36
  • phpstrom2021.5激活码【在线注册码/序列号/破解码】「建议收藏」

    phpstrom2021.5激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    45
  • 机房搬迁是什么意思_计算机机房管理规定

    机房搬迁是什么意思_计算机机房管理规定原标题:盘点机房搬迁中最容易出现的五个问题企业要更换办公地址的时候,最头疼的问题就是搬迁机房,机房的搬迁可不是搬家那么简单,是否能顺利搬迁,将成为保障企业业务连续性的关键要素。在企业机房的搬迁中,最容易出现哪些问题?盘点机房搬迁中最容易出现的五个问题(1)领导不明确在规划阶段最常见的错误是未能建立明确的领导。这意味着在迁移过程中的各个阶段中要确定谁负责明确的沟通,带领团队。在一个单一的部门,默认…

    2022年10月30日
    0

发表回复

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

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