mybatis逆向生成實體類,maven方式
1.pom檔案
雖然pom檔案中的<dependency>依賴中已經有了mysql驅動包,但在 <plugin>外掛中,必須再引入mysql-connector-java,再加上mybatis-generator-core和mybatis包.。由於原先的pom檔案中有spring-boot-maven-plugin外掛(springboot專案),所以用<pluginManagement>將外掛包起來
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>generator.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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
2.generator.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>
<context id="MyBatis3Simple" targetRuntime="MyBatis3Simple"
defaultModelType="flat">
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/focus?useUnicode=true&characterEncoding=utf8&useSSL=false"
userId="root" password="root" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.melo.focus.domain.basic"
targetProject="MAVEN" />
<sqlMapGenerator targetPackage="mapper/basic"
targetProject="MAVEN" />
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.melo.focus.mapper.basic"
targetProject="MAVEN" />
<table domainObjectName="User" tableName="user"></table>
<table domainObjectName="Role" tableName="role"></table>
<table domainObjectName="Resource" tableName="resource"></table>
<table domainObjectName="Authority" tableName="authority"></table>
<table domainObjectName="ResourceAuthority" tableName="resouce_authority"></table>
<table domainObjectName="RoleResourcer" tableName="role_resource_r"></table>
<table domainObjectName="RoleBusiness" tableName="role_business"></table>
<table domainObjectName="UserRoleR" tableName="user_role_r"></table>
</context>
</generatorConfiguration>
3.mvn mybatis-generator:generate
注意
最近發現個問題,springboot專案中 用<pluginManagement>標籤,會打包失敗,打出的包不能執行。將此標籤註釋包就可以運行了,目前不是很明白問題的原因