mybatis-generator自动生成代码

2022-06-30 14:52:03 浏览数 (1)

mybatis-generator有三种用法:命令行、eclipse插件、maven插件,maven插件方式最方便。

一、在pom.xml中添加plugin

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <mybatis-generator.version>1.3.6</mybatis-generator.version> <mysql.version>5.1.13</mysql.version> <mybatis.version>3.2.4</mybatis.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis-generator.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> </dependencies> <build> <finalName>mybatis-generator</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${mybatis-generator.version}</version> <!-- 数据库驱动 --> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> <!-- 自动生成 --> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> <configuration> <!-- 指定文件位置好像不起作用,始终默认读取src/main/resources/generatorConfig.xml文件 --> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <!--允许移动生成的文件--> <verbose>true</verbose> <!--允许覆盖生成的文件--> <overwrite>true</overwrite> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </pluginManagement> </build>

其中generatorConfig.xml的位置,大家根据实际情况自行调整

二、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"> <!-- 该配置文件说明插件需要如何生成,以及生成对应的包名,路径等信息。 还有重要的就是我们要生成的实体类所对应的的表或者试图 -->

{jdbc_password}"> </jdbcConnection> <!-- 指定生成的类型为java类型,避免数据库中number等类型字段 --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成model模型,对应的包,存放位置可以指定具体的路径,如/ProjectName/src,也可以使用MAVEN来自动生成 --> <javaModelGenerator targetPackage="{sqlMapperPackage}" targetProject="{daoMapperPackage}" targetProject="

</generatorConfiguration>

三、mybatisGenerator.properties配置文件

#数据库配置 #

jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8jdbc_url=jdbc:mysql://localhost:3306/test jdbc_driver=com.mysql.jdbc.Driver

jdbc_username=root jdbc_password=root

#执行:右键 Run As ---->Maven build ---->Goals:mybatis-generator:generate

#输出目录

targetProject=src/main/java

#modelPackage,sqlMapperPackage通常一致

modelPackage=com.generator.entitys

sqlMapperPackage=com.generator.dao

daoMapperPackage=com.generator.entitys

四、执行

mvn mybatis-generator:generate

0 人点赞