IDEA利用mybatis-generator自动生成dao和mapper
pom.xml配置
1 <properties> 2 <java.version>1.8</java.version> 3 <mybatis-generator-core.version>1.3.7</mybatis-generator-core.version> 4 <mysql-connector-java.version>5.1.46</mysql-connector-java.version> 5 <druid-spring-boot-starter.version>1.1.9</druid-spring-boot-starter.version> 6 <mybatis-spring-boot-starter.version>1.3.2</mybatis-spring-boot-starter.version> 7 </properties> 8 9 <dependencies> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-test</artifactId> 18 <scope>test</scope> 19 <exclusions> 20 <exclusion> 21 <groupId>org.junit.vintage</groupId> 22 <artifactId>junit-vintage-engine</artifactId> 23 </exclusion> 24 </exclusions> 25 </dependency> 26 <!--自动生成dao层代码时用到--> 27 <dependency> 28 <groupId>org.mybatis.generator</groupId> 29 <artifactId>mybatis-generator-core</artifactId> 30 <version>${mybatis-generator-core.version}</version> 31 </dependency> 32 33 <dependency> 34 <groupId>mysql</groupId> 35 <artifactId>mysql-connector-java</artifactId> 36 <version>${mysql-connector-java.version}</version> 37 </dependency> 38 <dependency> 39 <groupId>org.mybatis.spring.boot</groupId> 40 <artifactId>mybatis-spring-boot-starter</artifactId> 41 <version>${mybatis-spring-boot-starter.version}</version> 42 </dependency> 43 <dependency> 44 <groupId>com.alibaba</groupId> 45 <artifactId>druid-spring-boot-starter</artifactId> 46 <version>${druid-spring-boot-starter.version}</version> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-starter-aop</artifactId> 52 </dependency> 53 54 </dependencies> 55 56 <build> 57 <plugins> 58 <plugin> 59 <groupId>org.springframework.boot</groupId> 60 <artifactId>spring-boot-maven-plugin</artifactId> 61 </plugin> 62 <plugin> 63 <groupId>org.mybatis.generator</groupId> 64 <artifactId>mybatis-generator-maven-plugin</artifactId> 65 <version>1.3.7</version> 66 <configuration> 67 <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 68 <overwrite>true</overwrite> 69 </configuration> 70 71 <dependencies> 72 <dependency> 73 <groupId>mysql</groupId> 74 <artifactId>mysql-connector-java</artifactId> 75 <version>${mysql-connector-java.version}</version> 76 </dependency> 77 </dependencies> 78 </plugin> 79 </plugins> 80 </build>
generatorConfig.xml
<!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="MySql" defaultModelType="flat"> <plugin type="org.mybatis.generator.plugins.MapperAnnotationPlugin"></plugin> <plugin type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin"></plugin> <!-- <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example"/> <property name="replaceString" value="Condition"/> </plugin>--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://{IP}:{端口}/{数据库名称}" userId="{用户名}" password="{密码}"/> <javaModelGenerator targetPackage="com.***.dao.entity" targetProject="src/main/java"> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"></sqlMapGenerator> <javaClientGenerator targetPackage="com.***.dao" targetProject="src/main/java" type="XMLMAPPER"></javaClientGenerator> <table tableName="{表名称}"> <generatedKey column="id" sqlStatement="JDBC" identity="true"/> </table> </context> </generatorConfiguration>
点击MavenProjects找到项目下的Plugins,展开找到mybatis-generator,展开找到mybatis-generator:generate,双击运行