网站建设推广关键词手机百度最新正版下载
在编写项目时,我们常常需要与MySQL进行连接,在此基础上,如果我们需要使用数据库中的数据,就需要先创建对应的实体类,然后再编写对应的SQL语句,但是这一部分操作是有便捷操作的
导入依赖
我们需要现在 pom 文件中导入对应的MyBatis生成器依赖
<!-- 将这一部分导入到 properties 中 -->
<!-- MyBatis生成器 -->
<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>
<!-- 将这一部分导入到 plugins 中 -->
<!-- mybatis 生成器插件 -->
<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>${mybatis-generator-plugin-version}</version><executions><execution><id>Generate MyBatis Artifacts</id><phase>deploy</phase><goals><goal>generate</goal></goals></execution></executions><!-- 相关配置 --><configuration><!-- 打开日志 --><verbose>true</verbose><!-- 允许覆盖 --><overwrite>true</overwrite><!-- 配置文件路径 --><configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile></configuration>
</plugin>
添加完之后,让 maven 重新加载,让依赖成功导入
根据设置的配置文件路径添加配置文件
在 pom 文件中加入的依赖中,我们加入了一个配置文件路径,我们需要根据这个文件路径进行文件创建.后续需要通过这个文件来使用生成器
依赖中的地址: src/main/resources/mybatis/generatorConfig.xml (这个地址可自定义,但是自定义一定要记得修改依赖中设置的地址)
<generatorConfiguration><!-- 驱动包路径,location中路径替换成自己本地路径 --><classPathEntry location="本地驱动包地址"/><context id="DB2Tables" targetRuntime="MyBatis3"><!-- 禁用自动生成的注释 --><commentGenerator><property name="suppressAllComments" value="true"/><property name="suppressDate" value="true"/></commentGenerator><!-- 连接配置(自己的数据库连接配置) --><!-- driverClass5.0配置: com.mysql.jdbc.Driver --><jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/table_name?characterEncoding=utf8&useSSL=false"userId="root" password="password"> </jdbcConnection><javaTypeResolver><!-- 小数统一转为BigDecimal --><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- 实体类生成位置 --><javaModelGenerator targetPackage="com.example.forum.model" targetProject="src/main/java"><property name="enableSubPackages" value="true"/><property name="trimStrings" value="true"/></javaModelGenerator><!-- mapper.xml生成位置 --><sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"><property name="enableSubPackages" value="true"/></sqlMapGenerator><!-- DAO类生成位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="com.example.forum.dao" targetProject="src/main/java"><property name="enableSubPackages" value="true"/></javaClientGenerator><!-- 配置生成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即可 --><table tableName="t_article" domainObjectName="Article" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false"><!-- 类的属性用数据库中的真实字段名做为属性名, 不指定这个属性会自动转换 _ 为驼峰命名规则 --><property name="useActualColumnNames" value="true"/></table><table tableName="t_article_reply" domainObjectName="ArticleReply" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table><table tableName="t_board" domainObjectName="Board" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table><table tableName="t_message" domainObjectName="Message" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table><table tableName="t_user" domainObjectName="User" enableSelectByExample="false" enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false"><property name="useActualColumnNames" value="true"/></table></context>
</generatorConfiguration>
那么我们要怎么找到本地的驱动包呢?
1.打开 此电脑 ,找到 user 文件夹
2.在文件夹中找到 .m2 文件夹
3.找到 repository
4.双击打开之后,在文件夹里面搜索 mysql 文件夹
5.找到自己使用的版本,复制对应的 jar 包的文件路径
运行
将所有文件配置好之后,我们打开项目,找到 maven 键,打开 Plugins ,找到 mybatis-generator ,并双击 mybatis-generator:generate
控制台出现以下信息,并且开始创建 实例类 和 对应的 mapper.xml 文件那就是配置成功了
问题处理
根元素匹配
如果在运行生成器的时候控制台出现了图片中的问题,那么我们需要在配置文件中加入以下代码
<!-- 以下代码加在 generatorConfiguration 之前 -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">