mybatis-generator自动生成mapper
1、pom.xml里加入依赖及插件配置
1 <!-- mysql --> 2 <dependency> 3 <groupId>mysql</groupId> 4 <artifactId>mysql-connector-java</artifactId> 5 <version>5.1.13</version> 6 </dependency> 7 <!-- mybatis --> 8 <dependency> 9 <groupId>org.mybatis</groupId> 10 <artifactId>mybatis</artifactId> 11 <version>3.4.2</version> 12 </dependency> 13 <dependency> 14 <groupId>org.mybatis</groupId> 15 <artifactId>mybatis-spring</artifactId> 16 <version>1.3.1</version> 17 </dependency>
1 <build> 2 <!--解决Intellij构建项目时,target/classes目录下不存在mapper.xml文件--> 3 <resources> 4 <resource> 5 <directory>${basedir}/src/main/java</directory> 6 </resource> 7 </resources> 8 <plugins> 9 <!--配置文件生成插件--> 10 <plugin> 11 <groupId>org.mybatis.generator</groupId> 12 <artifactId>mybatis-generator-maven-plugin</artifactId> 13 <version>1.3.2</version> 14 <configuration> 15 <!--配置文件的位置--> 16 <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 17 <verbose>true</verbose> 18 <overwrite>true</overwrite> 19 </configuration> 20 <executions> 21 <execution> 22 <id>Generate MyBatis Artifacts</id> 23 <goals> 24 <goal>generate</goal> 25 </goals> 26 </execution> 27 </executions> 28 <dependencies> 29 <dependency> 30 <groupId>org.mybatis.generator</groupId> 31 <artifactId>mybatis-generator-core</artifactId> 32 <version>1.3.2</version> 33 </dependency> 34 </dependencies> 35 </plugin> 36 </plugins> 37 </build>
2、generatorConfig.xml文件(放在src/main/resources目录下)
1<?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <!-- 配置mysql 驱动jar包路径.用了绝对路径 --> 8 <classPathEntry location="/Users/apple/.m2/repository/mysql/mysql-connector-java/5.1.13/mysql-connector-java-5.1.13.jar" /> 9 10 <context id="mysql_tables" targetRuntime="MyBatis3"> 11 <!-- 防止生成的代码中有很多注释,加入下面的配置控制 --> 12 <commentGenerator> 13 <property name="suppressAllComments" value="true" /> 14 <property name="suppressDate" value="true" /> 15 </commentGenerator> 16 17 <!-- 数据库连接 --> 18 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 19 connectionURL="" 20 userId="" 21 password=""> 22 </jdbcConnection> 23 <!-- 指定生成的类型为java类型,避免数据库中number等类型字段 --> 24 <javaTypeResolver > 25 <property name="forceBigDecimals" value="false" /> 26 </javaTypeResolver> 27 28 <!-- 数据表对应的model层 --> 29 <javaModelGenerator targetPackage="com.xxx.po" targetProject="src/main/java"> 30 <property name="enableSubPackages" value="true" /> 31 <property name="trimStrings" value="true" /> 32 </javaModelGenerator> 33 34 <!--对应的xml mapper文件 --> 35 <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> 36 <property name="enableSubPackages" value="true" /> 37 </sqlMapGenerator> 38 39 <!-- 对应的dao接口 --> 40 <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.dao" targetProject="src/main/java"> 41 <property name="enableSubPackages" value="true" /> 42 </javaClientGenerator> 43 44 <!-- 数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO --> 45 <table tableName="xxx" domainObjectName="xxx"/> 46 </context> 47 </generatorConfiguration>
向上吧,少年