使用idea生成mybatis的配置文件
在pom.xml中添加插件的依赖
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!-- 指定插件运行的generator.xml文件位置-->
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<!-- 允许移动生成的文件-->
<verbose>true</verbose>
<!-- 允许覆盖生成的文件-->
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
将generatorConfig.xml放置到指定的目录下面
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>
<!--数据库驱动jar -->
<classPathEntry location="D:\JavaEE常用工具和类库\01.MySql驱动包\mysql-connector-java-5.1.37-bin.jar" />
<context id="Tables" targetRuntime="MyBatis3">
<!-- optional,旨在创建class时,对注释进行控制 -->
<!-- <commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>-->
<!--数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/pinyougou" userId="root"
password="root">
</jdbcConnection>
<!--默认false Java type resolver will always use java.math.BigDecimal if
the database column is of type DECIMAL or NUMERIC. -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
<javaModelGenerator targetPackage="com.itcast.common_mapper.model"
targetProject="E:\pinyougouexce 20181216\pinyougou_parent\common_mapper\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--生成mapper.xml文件路径 -->
<sqlMapGenerator targetPackage="com.itcast.common_mapper.mapper"
targetProject="E:\pinyougouexce 20181216\pinyougou_parent\common_mapper\src\main\java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--生成mapper接口文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->
<!--targetProject指定项目的路径,可以是相对路径,到java目录位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.itcast.common_mapper.mapper"
targetProject="E:\pinyougouexce 20181216\pinyougou_parent\common_mapper\src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->
<table tableName="persistent_logins" domainObjectName="persistentLogins" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tb_address" domainObjectName="tbAddress" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
<table tableName="tb_areas" domainObjectName="tbAreas" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
配置插件
输入如上的命令行:
mybatis-generator:generate -e
所有的配置好了以后,直接运行(需要特别注意数据库连接正常,并且配置的表名称在数据库中都已经创建好了)
就可以自动生成代码了