新建maven工程
在resources目录下新建配置文件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"> <generatorConfiguration> <!--配置要链接的数据库的数据源--> <classPathEntry location="D:/maven_repository/mysql/mysql-connector-java/8.0.26/mysql-connector-java-8.0.26.jar"/> <!---Mybatis上下文--> <context id="MySqlContext" targetRuntime="MyBatis3"> <!--配置数据库的链接信息--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" userId="root" password="root"/> <!--数据库BigDecimals字段在java中定义--> <javaTypeResolver> <property name="forceBigDecimals" value="true"/> </javaTypeResolver> <!--实体类生成信息--> <javaModelGenerator targetPackage="com.simon.app.entity" targetProject="./src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--mybatis 的xml文件地址--> <sqlMapGenerator targetPackage="com.simon.app.repository" targetProject="./src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--mybatis的mapper接口--> <javaClientGenerator targetPackage="com.simon.app.repository" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--要生成的表结构--> <table tableName="t_account" domainObjectName="Account" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
pom.xml新增如下内容
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.simon</groupId> <artifactId>app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <simon.version>1.0.0</simon.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <mybatis.version>3.5.7</mybatis.version> <mybatis_generator.version>1.4.0</mybatis_generator.version> <mysql-jdbc.verison>8.0.26</mysql-jdbc.verison> </properties> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-jdbc.verison}</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis_generator.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
执行mvn命令:mvn mybatis-generator:generate
注意:在执行前应将maven加入环境变量