springboot+mybatis 利用插件生成代码
首先在pom.xml 找到插件栏 plugins
然后我们按照下面引入插件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.bihu.study</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>HGYstudySpringBoot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 不需要版本号 因为是用的是SpringBoot自带的插件 parent中可以看到版本号!--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- 添加mySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- 添加myBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 引入mybatis generator 插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <!--配置文件位置[待会要新建]--> <configurationFile>src/main/resources/generator-config.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
自己找里面的 plugins 中的 第二个 plugin 按照里面代码配:
然后创建对应的配置文件 文件里面写:

<?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> <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat"> <!-- 自动检查关键字,为关键字增加反引号 --> <property name="autoDelimitKeywords" value="true"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!--覆盖生成XML文件--> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <!-- 生成的实体类添加toString()方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 不生成注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!---数据库连接信息--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sb?useUnicode=true&characterEncoding=utf-8" userId="root" password="root"> </jdbcConnection> <!--自动 domain[Bean]类的位置 --> <javaModelGenerator targetProject="src\main\java" targetPackage="com.bihu.study.Bean"/> <!--自动 mapper xml的位置 --> <sqlMapGenerator targetProject="src\main\resources" targetPackage="mapping"/> <!--自动 mapper类[接口]的位置 --> <javaClientGenerator targetProject="src\main\java" targetPackage="com.bihu.study.Mapper" type="XMLMAPPER"/> <!-- 这个我踩坑了[1天] 这里说明生成数据库里面的那个表 表名和一些配置 --> <!-- tableName 是表名 domainObjectName是Bean名 --> <!-- 也可以直接用tableName一个 --> <table tableName="test" domainObjectName="User"/> </context> </generatorConfiguration>
你要改 就跟着注释改即可。
然后你配置运行命令,添加maven 的 然后输入:
然后你运行 一下命令 就会生成对应代码 最后你创建 接口 、Service 、 contorller 测试即可。
这里有个坑 当时没跨过去 就是那个表名的。。。
下面的话你可以自己试试她自动生成的代码方法什么的
后面如果你需要查询全部数据:
推荐mybatis3以上 自己改改,因为可以实现无参构造等属性。:
#####3.3.5 <javaModelGenerator>
元素
该元素必须配置一个,并且最多一个。
该元素用来控制生成的实体类,根据 <context>
中配置的 defaultModelType
,一个表可能会对应生成多个不同的实体类。一个表对应多个类实际上并不方便,所以前面也推荐使用 flat
,这种情况下一个表对应一个实体类。
该元素只有两个属性,都是必选的。
targetPackage
: 生成实体类存放的包名,一般就是放在该包下。实际还会受到其他配置的影响 (<table>
中会提到)。targetProject
: 指定目标项目路径,使用的是文件系统的绝对路径。
该元素支持以下几个 <property>
子元素属性:
-
constructorBased
: 该属性只对MyBatis3
有效,如果true
就会使用构造方法入参,如果false
就会使用setter
方式。默认为false
。 -
enableSubPackages
: 如果true
,MBG 会根据catalog
和schema
来生成子包。如果false
就会直接用targetPackage
属性。默认为false
。 -
immutable
: 该属性用来配置实体类属性是否可变,如果设置为true
,那么constructorBased
不管设置成什么,都会使用构造方法入参,并且不会生成setter
方法。如果为false
,实体类属性就可以改变。默认为false
。 -
rootClass
: 设置所有实体类的基类。如果设置,需要使用类的全限定名称。并且如果 MBG 能够加载rootClass
,那么 MBG 不会覆盖和父类中完全匹配的属性。匹配规则:- 属性名完全相同
- 属性类型相同
- 属性有
getter
方法 - 属性有
setter
方法
-
trimStrings
: 是否对数据库查询结果进行trim
操作,如果设置为true
就会生成类似这样public void setUsername(String username) {this.username = username == null ? null : username.trim();}
的setter
方法。默认值为false
。
配置示例如下:
<javaModelGenerator targetPackage="test.model" targetProject="E:\MyProject\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
2023年5月28日 13:20:20

<?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> <!-- 配置文件,放在resource目录下即可 --> <!--数据库驱动个人配置--> <classPathEntry location="/Users/didi/.m2/repository/mysql/mysql-connector-java/8.0.18/mysql-connector-java-8.0.18.jar"/> <context id="MysqlTables" targetRuntime="MyBatis3"> <property name="autoDelimitKeywords" value="true"/> <!--可以使用``包括字段名,避免字段名与sql保留字冲突报错--> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- optional,旨在创建class时,对注释进行控制 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/imooc_mall?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" userId="root" password="12345678"> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="com.imooc.mall.model.pojo" targetProject="src/main/java"> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="true"/> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true"/> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!--生成mapper映射文件存放位置--> <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.mall.model.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成对应表及类名--> <table schema="root" tableName="imooc_mall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="imooc_mall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="imooc_mall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="imooc_mall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="imooc_mall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="imooc_mall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
如果在配置文件加了依赖,上面那个
<classPathEntry location="/Users/didi/.m2/repository/mysql/mysql-connector-java/8.0.18/mysql-connector-java-8.0.18.jar"/>
要删除哈!
本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/15364906.html