利用maven插件生成mybatis的 mapper dao entity 大大减少了工作量。
我的环境
Eclipse Java EE IDE for Web Developers.
Version: Mars.1 Release (4.5.1)
在你的pom.xml下添加如下代码
<build>
<plugins>
<!-- mybatis自动生成mapper插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<!-- <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
<jdbcURL>jdbc:mysql://120.25.162.108:3306/lzgstudy</jdbcURL>
<jdbcUserId>root</jdbcUserId>
<jdbcPassword></jdbcPassword> -->
</configuration>
<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>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<!-- copy-dependency plugin -->
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
<!-- mybatis-generator-plugin -->
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<versionRange>[1.3.2,)</versionRange>
<goals>
<goal>generate</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
添加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">
<!-- 项目 右键 》run as 》 maven bulid 》弹出对话框 在goals中输入mybatis-generator:generate 》 点击 Run -->
<generatorConfiguration>
<!-- 数据库驱动,用maven 插件不需要外部的引入-->
<!-- <classPathEntry location="E:\project\vmanagerStore\src\main\resources\tools\mysql-connector-java-5.1.29.jar"/> -->
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://120.25.162.108:3306/lzgstudy"
userId="root"
password="">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true"/>
</javaTypeResolver>
<!-- 生成实体的包名和位置-->
<javaModelGenerator targetPackage="com.lzg.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成Mapper文件的包名和位置-->
<sqlMapGenerator targetPackage="configs.mapper" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lzg.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 需要生成的表,tableName数据库表名,domainObjectName对应的实体的类名,要生成多张表就复制多行-->
<table tableName="testUser" domainObjectName="TestUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
上面xml文件 基本都有注释了,要修改的地方可能就是 数据库配置 和 代码生成的路径。完成后
项目 右键 》run as 》 maven bulid 》弹出对话框 在goals中输入mybatis-generator:generate 》 点击 Run
如图,能看到代码生成了。
问题:DAO的名字可能和我们习惯的IXxx不同,自己手动改下吧。
小技巧:数据库字段命名为user_name 生成的实体中就是 userName(驼峰命名),数据库直接userName 实体中是username。
其他如果大家还有好的技巧欢迎留言哈。互相学习下。