idea maven mybatis generator 自动生成代码的实例讲解
安装过程步骤可以看上面的博文,里面介绍得很详细。
二、建数据表
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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;
|
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中加入以下配置:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?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 自动生成代码(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/jamespan23/p/8064176.html
相关文章
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 83
-
2025-05-25 30
-
2025-05-27 24
-
2025-05-25 80
-
2025-05-25 58










