ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mybatis自动化,非常实用的技巧

2020-10-27 03:31:46  阅读:147  来源: 互联网

标签:xml core generator 1.3 实用 自动化 mybatis org


Mybatis自动化   作用:反向生成实体类,接口,mapper.xml 步骤:   1.mybatis.xml全局配置文件中,添加依赖包:
<dependency> 
    <groupId>org.mybatis.generator</groupId> 
    <artifactId>mybatis-generator-core</artifactId>         
    <version>1.3.5</version> 
</dependency>
     2.mybatis.xml文件中,加载插件:
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration> <!--配置文件的路径-->
                    <configurationFile>
                        src/main/resources/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

  在resources文件夹下,创建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>
    <!--数据库驱动jar -->
    <classPathEntry location="D:\core-code\maven\maven_repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar" />
    <context id="MyBatis" targetRuntime="MyBatis3">
    <!--去除注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/e?useSSL=false"
                        userId="root"
                        password="">
        </jdbcConnection>
        <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
        <javaModelGenerator targetPackage="com.java.bean"
                            targetProject="D:\code\idea_workspaces\maven\mybatis3\src\main\java">
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--生成SQLmapper文件 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="D:\code\idea_workspaces\maven\mybatis3\src\main\resources">
        </sqlMapGenerator>
        <!--生成Dao文件,生成接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.java.dao"
                             targetProject="D:\code\idea_workspaces\maven\mybatis3\src\main\java">
        </javaClientGenerator>
        <table tableName="user" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
        </table>
        <table tableName="courier" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
        </table>
        <table tableName="express" enableCountByExample="false"
                        enableUpdateByExample="false" enableDeleteByExample="false"
                        enableSelectByExample="false" selectByExampleQueryId="false">
        </table>
        <table tableName="eadmin" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

 

  

运行:maven Project选项卡->plugins->找到mybatis-generator-core,双击运行就会自动生成

 

 

  此时项目文件源码中啥都没

 

 运行命令后

 

 轻松生成对应的实体类和mapper/dao接口,真的省了很多编码步骤!

 

 

标签:xml,core,generator,1.3,实用,自动化,mybatis,org
来源: https://www.cnblogs.com/misaki-workshop/p/13882375.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有