您的位置:首页 > 编程语言

idea中使用Mybatis generator 自动生成代码

2018-01-22 13:01 411 查看
1.自己创建一个表

2.在idea中创建Mavne项目,选好之后next。




3.填写groupId和ArtifactId:

(这两个参数值都是自己定义的),下面这段文字,是网上抄来的,以便大家更好地了解这两个参数。
groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。
一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。




4.点击Next,配置Maven信息。
这里的  第一行  配置。是你Maven安装的位置。
第二行是安装maven的时候你自己配置好的sttings.xml文件。settings.xml是Maven的全局配置文件。
之后点击next




5.项目名称和存放位置。




6.创建完成,看一下 目录结构。




7.把resource文件夹设置为Resources Root        右键项目名




8.自动生成的代码都保存在了target夹下面,所以我们要创建这个文件夹,没有的话会报错。










9.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>10.在resources源文件夹下面创建generatorConfig.xml这上面有一行会爆红,别管它。
这里要使用你自己的mysql驱动包。 下面 <classPathEntry location>中

配置自己的数据库地址密码等。
在下面有你数据库的表的名字。
<?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/scott" userId="root" password="11111111">
</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>

<!-- 要生成哪些表 user是我的数据库表名 -->
<table tableName="user" domainObjectName="user"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>11.执行生成代码



下一步:
点击加号--->Name:generator--->Command line: 输入-->  mybatis-generator:generate -e
这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题。
点击Apply  -->ok






12.运行:run-->run‘generator’













eclipse中:https://www.cnblogs.com/JsonShare/p/5521901.html

结束!
不懂的留言,看到就及时回复!zyb




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: