使用MyBatis生成器

在编写项目时,我们常常需要与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&amp;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 generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

一. 生成器模板路径可以引用相关变量 如 ${basepackage}/${className}.java,根据该变量生成输出文件 二. 自动搜索某个目录所有模板文件,无需配置 三.代码生成器模板可以引用的相关变量 1. g.generateByTable("table_name") 方法可以引用的变量 table : cn.org.rapid_framework.generator.provider.db.table.model.Table 2. g.generateByClass(UserInfo.class) 方法可以引用的变量 clazz : cn.org.rapid_framework.generator.provider.java.model.JavaClass 3. g.generateBySql("select * from user_info where pwd=:pwd") 方法可以引用的变量 sql : cn.org.rapid_framework.generator.provider.db.sql.model.Sql 4.公共变量 env : 系统环境变量 System.getProperties() : 直接引用,没有前缀 generator.properties 文件中的所有属性,直接引用,没有前缀 gg : 模板控制变量, cn.org.rapid_framework.generator.GeneratorControl 四.每个模板有gg变量可以控制自身的自定义配置 (每一个模板都会创建新的gg实例) 如是否生成,是否覆盖目标文件,甚至是生成其它文件 ${gg.setIgnoreOutput(true)}: 如果为true则不生成输出文件 ${gg.generateFile(outputFile,content)} 在模板中生成其它文件 ${gg.getProperty(key,defaultValue)}: 得到proproty,如果没有找到,则返回默认值 ${gg.getInputProperty(key)}: 会弹出一个输入框,提示用户输入值 具体参考: http://code.google.com/p/rapid-framework/wiki/rapid_generator_gg 五.支持生成(gen)及删除操作(del),即生成的代码也可以很方便的删除 六. 自动删除模板扩展名: .ftl,.vm 举例: 如你有一个模板 SqlMap.xml.ftl 将变为 SqlMap.xml 所以你要生成ftl扩展名的文件,应该将文件名从 list.ftl => list.ftl.ftl 七. 模板自动include所有父目录的:macro.include文件,可以存放公共的macro 示例: 如你的模板为 com/project/UserDao.java, 将自动include: com/project/macro.include, com/macro.include, macro.include 八. generator.xml (或者generator.properties)配置文件 1.类似ant可以变量引用,引用环境变量使用${env.JAVA_HOME}, 引用System.getProperties()直接引用 2.自动替换generator.properties中的句号(.)为反斜杠,设置key为key+"_dir"后缀 示例: pkg=com.company => pkg_dir=com/company 九.自动拷贝二进制文件至输出目录 如模板目录下的 zip,rar,doc文件将会自动拷贝至输出目录,不会破坏文件格式 (通过扩展名自动识别) 十. 数据库表配置,用于自定义生成器模板引用的table变量,配置文件必须存放在classpath: generator_config/table/table_name.xml (该文件生成器可以生成,自己再自定义修改)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值