新西兰服务器

IDEA Maven Mybatis generator自动生成代码的示例分析


IDEA Maven Mybatis generator自动生成代码的示例分析

发布时间:2021-07-10 14:05:55 来源:高防服务器网 阅读:181 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关IDEA Maven Mybatis generator自动生成代码的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、安装配置maven以及在Idea中配置maven

安装过程步骤可以看上面的博文,里面介绍得很详细。

二、建数据表

DROP TABLE IF EXISTS `t_user`;  CREATE TABLE `t_user` (   `id` varchar(100) NOT NULL,   `username` varchar(20) DEFAULT NULL,   `password` varchar(20) DEFAULT NULL,   `headerPic` varchar(60) DEFAULT NULL,   `email` varchar(60) DEFAULT NULL,   `sex` varchar(2) DEFAULT NULL,   `create_time` datetime DEFAULT NULL,   `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,   `is_delete` int(1) DEFAULT NULL,   `address` varchar(200) DEFAULT NULL,   `telephone` varchar(15) DEFAULT NULL,   PRIMARY KEY (`id`)  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

三、Idea创建maven项目

1、点击create new project-》maven-》create from archetype->maven-archetype-webapp,然点击next,步骤如图:

2、填写groupId和ArtifactId:(这两个参数值都是自己定义的),下面这段文字,是网上抄来的,以便大家更好地了解这两个参数。

groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。

一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。

比如我创建一个项目,我一般会将groupId设置为cn.laok,cn表示域为中国,laok是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.laok.testProj打头的,如果有个UserDao,它的全路径就是cn.laok.testProj.dao.UserDao

3、点击next,配置maven信息,如图:

4、点击next,填写项目名称,如图:

5、创建完成后,项目的结构如图,在生成代码之前,不需要创建其他文件夹,但是需要把resources文件夹设置成Resources Root(右键点击resources文件夹-》Mark Directory As->Resources Root)

四、配置pom.xml和generatorConfig.xml

1、在pom.xml中加入以下配置:

<build>   <finalName>create-code</finalName>   <plugins>   <plugin>    <groupId>org.mybatis.generator</groupId>    <artifactId>mybatis-generator-maven-plugin</artifactId>    <version>1.3.2</version>    <configuration>    <verbose>true</verbose>    <overwrite>true</overwrite>    </configuration>   </plugin>   </plugins>  </build>

2、在resources源文件夹下面创建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>   <classPathEntry location="D:/Java/lib/mysql-connector-java-5.1.43-bin.jar" />   <context id="test" targetRuntime="MyBatis3">    <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>    <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>    <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>    <commentGenerator>     <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->     <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->     <property name="suppressDate" value="true" />     <!-- 是否去除自动生成的注释 true:是 : false:否 -->     <property name="suppressAllComments" value="false" />    </commentGenerator>    <!--数据库链接URL,用户名、密码 -->    <jdbcConnection driverClass="com.mysql.jdbc.Driver"        connectionURL="jdbc:mysql://localhost:3306/article" userId="root" password="">    </jdbcConnection>    <javaTypeResolver>     <!-- This property is used to specify whether MyBatis Generator should      force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->     <property name="forceBigDecimals" value="false" />    </javaTypeResolver>    <!-- 生成模型的包名和位置 文件夹自己定义-->    <javaModelGenerator targetPackage="com.test.model"         targetProject="target">     <property name="enableSubPackages" value="true" />     <property name="trimStrings" value="true" />    </javaModelGenerator>    <!-- 生成映射文件的包名和位置 文件夹自己定义-->    <sqlMapGenerator targetPackage="com.test.mapping"         targetProject="target">     <property name="enableSubPackages" value="true" />    </sqlMapGenerator>    <!-- 生成DAO的包名和位置 文件夹自己定义-->    <javaClientGenerator type="XMLMAPPER"          targetPackage="com.test.dao" implementationPackage="com.test.dao.impl" targetProject="target">     <property name="enableSubPackages" value="true" />    </javaClientGenerator>       <!-- 要生成哪些表 -->    <table tableName="t_user" domainObjectName="user"      enableCountByExample="false" enableUpdateByExample="false"      enableDeleteByExample="false" enableSelectByExample="false"      selectByExampleQueryId="false"></table>   </context>  </generatorConfiguration>

3、配置完成后,一定要点击Build->Rebuild project,生成target文件夹,不然生产代码的时候是生产在target文件下下面,没有这个文件夹会报错,当然也可以配置生成在其他文件夹下面。项目结构如图:

特别注意的一点:一定要在配置文件中加入本地的mysql-connector-java-5.1.43-bin.jar,

下载地址https://dev.mysql.com/downloads/connector/j/

然后解压到本地,我的配置如下:<classPathEntry location="D:/Java/lib/mysql-connector-java-5.1.43-bin.jar" />

这个需要大家根据自己存放的路径配置。

五、执行生成代码

1、点击run->Edit configurations,如图:

2、之后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate -e”
这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题。

3、配置完成后,点击run-》run generator,不出意外的话,在控制台中会出现BUILD SUCCESS的info信息。完整的效果如图所示:

感谢各位的阅读!关于“IDEA Maven Mybatis generator自动生成代码的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[