MyBatis 代码生成器
1.添加依赖
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
<!--添加generator的插件-->
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<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.49</version>
</dependency>
</dependencies>
<configuration>
<!--配置文件的路径-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<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>
<!--如果不需要生成example的话将targetRuntime设置为MyBatis3Simple即可-->
<context id="test" targetRuntime="MyBatis3Simple">
<commentGenerator>
<!-- 生成的注释是否包含生成的时间,一般设置为true,不生成时间 -->
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mos" userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<!-- 是否将数据库中的decimal类型转换为java的BigDecimal类型 -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成的实体类pojo的包名和位置 -->
<!-- targetProject 这个是项目的跟路径,从xxC盘后面开始写 -->
<javaModelGenerator targetPackage="com.tong.pojo"
targetProject="\Idea\Idea-project\mybatis-generator\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成mapper.xml映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.tong.dao"
targetProject="\Idea\Idea-project\mybatis-generator\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="" value=""/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.tong.dao"
targetProject="\Idea\Idea-project\mybatis-generator\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要生成哪些表 -->
<table tableName="mos_user" domainObjectName="User"></table>
<table tableName="mos_pro" domainObjectName="Product"></table>
<table tableName="mos_role" domainObjectName="Role"></table>
<table tableName="mos_cart" domainObjectName="Cart"></table>
</context>
</generatorConfiguration>
3. 添加完毕之后,在idea的maven中执行下面命令即可
mybatis-generator:generate
如果成功了,在你项目的目录中可以看到dao、bean、mapper全部生成了。
注意:
这里面只会根据单表来生成文件,并不会生成表之间的关系
倘若Mapper文件已经存在时,如果进行重新生成mapper文件时,内容不被覆盖而是进行内容追加,结果导致mybatis解析失败。
解决方法:删除原来已经生成的mapper文件之后再进行生成。
Mybatis自动生成的bean及dao文件不是内容而是直接覆盖没有此问题。