参考通用mapper 文档:https://github.com/abel533/Mapper/wiki/4.1.mappergenerator
使用maven 的方法:
1,修改pom.xml
<properties> <!-- ${basedir}引用工程根目录 --> <!-- targetJavaProject:声明存放源码的目录位置 --> <targetJavaProject>${basedir}/src/main/java</targetJavaProject> <!-- targetMapperPackage:声明MBG生成XxxMapper接口后存放的package位置 --> <targetMapperPackage>xxx.xxx.xx.mappers</targetMapperPackage> <!-- targetModelPackage:声明MBG生成实体类后存放的package位置 --> <targetModelPackage>xxx.xx.xxx.entities</targetModelPackage> <!-- targetResourcesProject:声明存放资源文件和XML配置文件的目录位置 --> <targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject> <!-- targetXMLPackage:声明存放具体XxxMapper.xml文件的目录位置 --> <targetXMLPackage>mappers</targetXMLPackage> <!-- 通用Mapper的版本号 --> <mapper.version>4.0.0</mapper.version> <!-- MySQL驱动版本号 --> <mysql.version>5.1.37</mysql.version> </properties> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <!-- 配置generatorConfig.xml配置文件的路径 --> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <!-- MBG插件的依赖信息 --> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
2,创建 generatorConfig.xml,注意pom.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> <!-- 引入外部属性文件 --> <properties resource="config.properties" /> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <!-- 配置通用Mapper的MBG插件相关信息 --> <plugin type="${mapper.plugin}"> <property name="mappers" value="${mapper.Mapper}" /> <property name="caseSensitive" value="true"/> </plugin> <!-- 配置连接数据库的基本信息 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.user}" password="${jdbc.password}"> </jdbcConnection> <!-- 配置Java实体类存放位置 --> <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}" /> <!-- 配置XxxMapper.xml存放位置 --> <sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}" /> <!-- 配置XxxMapper.java存放位置 --> <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER" /> <!-- 根据数据库表生成Java文件的相关规则 --> <!-- tableName="%"表示数据库中所有表都参与逆向工程,此时使用默认规则 --> <!-- 默认规则:table_dept→TableDept --> <!-- 不符合默认规则时需要使用tableName和domainObjectName两个属性明确指定 --> <table tableName="xxxx" domainObjectName="xxx"> <!-- 配置主键生成策略 --> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> </context> </generatorConfiguration>
3,运行:pom.xml 右键 run build 命令:mybatis-generator:generate