MybatisGenerator 数据库逆向生成代码工具

本文详细介绍如何在SpringBoot项目中集成MyBatis框架,包括依赖配置、核心配置文件设置及Maven编译配置等步骤,并指导如何通过配置运行MyBatis代码生成器。
一、依赖jar包
<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.29</version>
		</dependency>

二、核心配置文件:generatorConfig.xml

<?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="GCCSMysql" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <property name="mergeable" value="false"></property>

        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>

        <commentGenerator>
            <!-- 抑制警告 -->
            <property name="suppressTypeWarnings" value="true"/>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="false"/>
            <property name="javaFileEncoding" value="UTF-8"/>
        </commentGenerator>


        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/db_test_user?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.mybatis.test.model.user"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.mybatis.test.mapper.user"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator targetPackage="com.mybatis.test.mapper.user"
                             targetProject="src/main/java"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="t_custom" domainObjectName="Custom"
               enableInsert="true"
               enableSelectByPrimaryKey="true"
               enableSelectByExample="true"
               enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true"
               enableDeleteByExample="true"
               enableCountByExample="true"
               enableUpdateByExample="true"
               selectByPrimaryKeyQueryId="true"
               selectByExampleQueryId="true">
        </table>

        <table tableName="t_seller" domainObjectName="Seller"
               enableInsert="true"
               enableSelectByPrimaryKey="true"
               enableSelectByExample="true"
               enableUpdateByPrimaryKey="true"
               enableDeleteByPrimaryKey="true"
               enableDeleteByExample="true"
               enableCountByExample="true"
               enableUpdateByExample="true"
               selectByPrimaryKeyQueryId="true"
               selectByExampleQueryId="true">
        </table>

    </context>
</generatorConfiguration>

三、maven pom添加编译配置

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<encoding>UTF-8</encoding>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>3.0.0</version>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<configuration>
					<verbose>false</verbose>
					<overwrite>false</overwrite>
				</configuration>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.41</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

四、运行

技术分享

技术分享

之后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate  -e

这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题。

技术分享

技术分享

如果添加成功,则会在run 选项中有“generator” 选项,如下:

技术分享

点击运行

相关推荐:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值