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


相关推荐

  • java下载 文件_Java下载文件的几种方式「建议收藏」

    java下载 文件_Java下载文件的几种方式「建议收藏」1.以流的方式下载.publicHttpServletResponsedownload(Stringpath,HttpServletResponseresponse){try{//path是指欲下载的文件的路径。Filefile=newFile(path);//取得文件名。Stringfilename=file.getName();//取得文件的后缀名。Strin…

    2022年7月7日
    59
  • react脚手架有哪些_vue脚手架3搭建项目

    react脚手架有哪些_vue脚手架3搭建项目前言如何快速搭建一个httprunner项目呢?我们可以使用脚手架,脚手架就是自动地创建一些目录,形成一个项目的架构,不需要我们再手动的去创建查看创建新项目的命令先来查看一下帮助命令httpr

    2022年8月6日
    4
  • 【架构】Lambda架构

    【架构】Lambda架构一、出现的背景1.1从传统数据库到NoSQL,再到Hadoop很多人学习大数据都听说过以下发展进程,MySQL/Oracle/SQLServer→Hadoop/Hive/Spark。但还有一个时期,是大家容易忽略的——NoSQL。我们不能忽略掉它。其实,NoSQL的发展和推广要比Hadoop更早,在没有Hadoop的大数据过渡期,随着数据量急剧膨胀,大家纷纷从传统的关系型数据库转变到NoSQL数据库,各种各样的NoSQL数据库应用而生。有了NoSQL数据库,可以轻易将机器的数量扩展到.

    2022年6月25日
    34
  • eclipse怎么配置svn插件_dip插件有哪几种

    eclipse怎么配置svn插件_dip插件有哪几种Eclipse安装插件的几种方法(eclipse安装SVN插件)在这里啰嗦一句,其实更新的版本mar2更好用。但是由于在低电压版的本本上,mar2跑不起来,经常假死,kepler可以很流畅。 so,如果为了携带了轻便低电压版CPU的本本,mar2跑不起来,就用kepler吧。以Eclipse(Kepler)安装SVN插件为例,记录eclipse安装插件的几种方法。 1eclipsemarket…

    2022年10月10日
    2
  • WSAStartup函数的用途

    我们先来看看WSAStartup函数的原型intPASCALFARWSAStartup(WORDwVersionRequired,LPWSADATAlpWSAData);    可以这么理解,第一个参数提供必要信息(为了加载套接字库),第二个参数获取相关信息(获取相关信息)。看看下面的程序就明白了(其中wVersion比较重要):          WSACl

    2022年4月7日
    55
  • goland20.2激活码【在线注册码/序列号/破解码】

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

    2022年3月19日
    51

发表回复

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

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