企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] 只能说用这个太爽了!!! 贴上官网地址:[http://mybatis.org/generator/running/runningWithMaven.html](http://mybatis.org/generator/running/runningWithMaven.html) #### **三个步骤很简单** #### 1. pom.xml文件添加依赖 ~~~ <!--tk-mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.1.5</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.4</version> </dependency> <!--生成Java和XML文件--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.0.3</version> </dependency> </dependencies> </plugin> ~~~ #### 2. 配置generator.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="/Users/cfun/.m2/repository/mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar"/> <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包--> <!--<classPathEntry location=""/>--> <context id="context" targetRuntime="MyBatis3"> <!--开始和结束分隔符,对于有关键字的情况下适用。--> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <!--指定继承类--> <!--<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>--> <property name="mappers" value="com.blog.TkMapper"/> <!--是否区分大小写--> <property name="caseSensitive" value="true"/> </plugin> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="false"/> </commentGenerator> <!--数据库连接驱动类,URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxxxx:3306/blog?characterEncoding=utf-8" userId="xxxxx" password="xxxx"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成(实体)模型的包名和位置--> <javaModelGenerator targetPackage="com.blog.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成XML映射文件的包名和位置--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成mapper接口的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.blog.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration> ~~~ #### 3.输入mvn命令 ~~~ mvn mybatis-generator:generate ~~~ 控制台会输出以下内容就成功了 ![](https://img.kancloud.cn/bb/2c/bb2c97609fde8ea330847e8986227329_2168x948.png)