一、搭建springBoot项目
1、选择spring initializr项目(和上次搭建项目一样步骤)
2、将mybatis相关配置给勾选上
因为整合要用到数据库,所以必须将数据库相关配置给勾选上,如mybatis 工厂以及mybatis驱动类。
3.新项目就搭建完成
二、MBG(mybatis generator)逆向工程
1、导入相关pom依赖
<!--逆向工程依赖-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency><!--插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>
<!--这里是配置generatorConfig.xml的路径 不写默认在resources目录下找generatorConfig.xml文件-->
</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
2、将Generator.properties和generatorConfig.xml文件导入到项目中
这两个文件都放在resource里面
1. 这个文件Generator.properties
注意:其中jdbc.driverLocation中的路径为本地maven仓库中的mysql jar包路径,这也是自己将该文件导入进来后要更改的地方
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/hmf?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true jdbc.username=root jdbc.password=123456 jdbc.driverLocation=F:\Maven\inti\mysql\mysql-connector-java\8.0.20 model.package=com.hmf.code.pojo mapper.package=com.hmf.code.dao xml.mapper.package=mapper
2.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> <properties resource="generator.properties"/> <!--指定数据库jdbc驱动jar包的位置--> <classPathEntry location="${jdbc.driverLocation}"/> <context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="javaFileEncoding" value="UTF-8"/> <!--生成mapper.xml时覆盖原文件--> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/> <plugin type="org.mybatis.generator.plugins.MyBatisPlugin"> <property name="hasLombok" value="true"/> </plugin> <!--可以自定义生成model的代码注释--> <commentGenerator> <property name="suppressAllComments" value="true"/> <!-- 是否取消注释 --> <property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳 --> </commentGenerator> <!--配置数据库连接--> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> <!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题--> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--指定pojo生成位置--> <javaModelGenerator targetPackage="${model.package}" targetProject="src\main\java"/> <!--指定生成mapper.xml的路径--> <sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="src\main\resources"/> <!--指定生成mapper接口的的路径--> <javaClientGenerator type="XMLMAPPER" targetPackage="${mapper.package}" targetProject="src\main\java"/> <table tableName="student" domainObjectName="Student"> <!--<columnOverride column="create_time" javaType="java.sql.Timestamp" jdbcType="timestamp"/>--> <!--<columnOverride column="modify_time" javaType="java.sql.Timestamp" jdbcType="timestamp"/>--> <property name="enableCountByExample" value="false"/> <property name="enableDeleteByExample" value="false"/> <property name="enableDeleteByPrimaryKey" value="false"/> <property name="enableInsert" value="false"/> <property name="enableSelectByPrimaryKey" value="false"/> <property name="enableUpdateByExample" value="false"/> <property name="enableUpdateByPrimaryKey" value="false"/> <property name="selectByExampleQueryId" value="true"/> <property name="selectByPrimaryKeyQueryId" value="false"/> <!--自动生成主键,可以代替useGeneratedKeys--> <generatedKey column="stu_id" sqlStatement="Mysql" type="post" identity="true"/> </table> </context> </generatorConfiguration>
3、MyBatisPlugin.class导入org.mybatis.generator.plugins(对应的软件包)下
声明:这是一个生成规范文档注释,生成接口,注解的功能类
为什么将此文件导入到对应的软件包底下:因为到时候要将文件导入到本地仓库中的对应压缩包中去,目的是为了逆向生成代码,
当然还有一种方法就是导入插件,配置mybatis generator生成器
命名行:
mybatis-generator:generate -e
package org.mybatis.generator.plugins; import lombok.extern.slf4j.Slf4j; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.Plugin; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.*; import java.time.LocalDateTime; import java.util.List; /** * @author hmf */ @Slf4j public class MyBatisPlugin extends PluginAdapter { @Override public boolean validate(List<String> list) { return true; } @Override public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { boolean hasLombok = Boolean.parseBoolean(getProperties().getProperty("hasLombok", "false")); log.warn("hasLombok:\t" + hasLombok); if (hasLombok) { topLevelClass.addImportedType("lombok.Data"); topLevelClass.addImportedType("lombok.NoArgsConstructor"); topLevelClass.addImportedType("lombok.AllArgsConstructor"); topLevelClass.addImportedType("lombok.experimental.Accessors"); topLevelClass.addAnnotation("@Data"); topLevelClass.addAnnotation("@NoArgsConstructor"); topLevelClass.addAnnotation("@AllArgsConstructor"); topLevelClass.addAnnotation("@Accessors(chain = true)"); } topLevelClass.addJavaDocLine("/**"); String remarks = introspectedTable.getRemarks(); log.error("@table\t" + remarks); StringBuilder sb = new StringBuilder(); topLevelClass.addJavaDocLine(remarks + "\t" + introspectedTable.getFullyQualifiedTable()); topLevelClass.addJavaDocLine(" * @author hgh"); topLevelClass.addJavaDocLine(" * @date " + LocalDateTime.now()); topLevelClass.addJavaDocLine(" */"); return true; } @Override public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { field.addJavaDocLine("/**"); String remarks = introspectedColumn.getRemarks(); log.error("@column\t" + remarks); field.addJavaDocLine(" * " + remarks); field.addJavaDocLine(" */"); return true; } @Override public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper")); interfaze.addAnnotation("@Mapper"); interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Repository")); interfaze.addAnnotation("@Repository"); interfaze.addJavaDocLine("/**"); interfaze.addJavaDocLine(" * @author hgh"); interfaze.addJavaDocLine(" * @date " + LocalDateTime.now()); interfaze.addJavaDocLine(" */"); return true; } @Override public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { boolean hasLombok = Boolean.parseBoolean(getProperties().getProperty("hasLombok", "false")); return !hasLombok; } @Override public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { boolean hasLombok = Boolean.parseBoolean(getProperties().getProperty("hasLombok", "false")); return !hasLombok; } }
4、将项目进行编译
编译完成之后
5、将MyBatisPlugin.class文件赋到本地仓库的org对应版本的压缩下:
1先查看对应的jar包版本号是多少:
找到自己存放的路径 然后用解压 路径打开 都是class文件
放进去之后就可以使用逆向生成代码
逆向生成代码完成完毕