whjqwer的gravatar头像
whjqwer 2018-05-24 14:48:09
idea中使用MyBatis_Generator生成Dao和Mapper.xml文件

在idea中使用MyBatis_Generator插件来自动生成dao和mapper文件,可以大大的减轻工作量。

下面的代码是MyBatis_Generator的配置文件:

<?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="context" targetRuntime="MyBatis3">

        <!-- 是否去除自动生成的代码中的注释 true:是 false:否-->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 数据库连接信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/数据库名"
                        userId="数据库用户名" password="数据库密码">
        </jdbcConnection>

        <!-- 默认 false,把 JDBC DECIMAL  NUMERIC 类型解析为 Integer
              true 时解析为 java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- targetProject:生成 POJO 类的位置 -->
        <javaModelGenerator targetPackage="cn.wmyskxz.pojo" targetProject="src/main/java">
            <!-- enableSubPackages:是否让 schema 作为包的后缀-->
            <property name="enableSubPackages" value="false"/>
            <!-- trimStrings:从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- targetProject:生成xml映射文件存放位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <!-- enableSubPackages:是否让 schema 作为包的后缀-->
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- targetProject:生成mapper类存放位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.wmyskxz.mapper" targetProject="src/main/java">
            <!-- enableSubPackages:是否让 schema 作为包的后缀-->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--生成对应表及类名
            tableName:要生成的表名
            domainObjectName:生成后的实例名
            enableCountByExample:Count语句中加入where条件查询,默认为true开启
            enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
            enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
            enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
            selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
        -->
        <table tableName="表名" domainObjectName="实例名" enableCountByExample="false" enableUpdateByExample="false" 
            enableDeleteByExample="false" enableSelectByExample="true" selectByExampleQueryId="false">
            <!--使用数据库中实际的字段名作为生成的实体类的属性-->
            <property name="useActualColumnNames" value="true"/>
            <!--使用自增长键-->
            <property name="my.isgen.usekeys" value="true"/>
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

要想使用还得再pom文件中加入MyBatis_Generator插件,配置如下:

<plugin>
   <groupId>org.mybatis.generator</groupId>
   <artifactId>mybatis-generator-maven-plugin</artifactId>
   <version>1.3.2</version>
</plugin>

上述配置完成后点击idea右侧边栏的Maven Projects,如下图:

idea中使用MyBatis_Generator生成Dao和Mapper.xml文件

就会出现MyBatis_Generator插件了,点击运行就会自动生成dao和mapper.xml文件

 


打赏
最近浏览
qishang777  LV3 2022年4月10日
zyk_zuidaima  LV1 2021年10月15日
陈石养  LV2 2021年9月23日
jjjmmm  LV1 2020年12月30日
Ans226 2020年6月22日
暂无贡献等级
pyyLearn 2020年5月18日
暂无贡献等级
我是特种兵庄焱  LV13 2020年5月12日
0011100  LV12 2020年4月9日
海羅叔叔__  LV6 2020年4月8日
xiaoqianga  LV1 2020年3月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友