mybatis自动生成mapper,dao映射文件

利用Mybatis-Generator来帮我们自动生成mapper.xml文件,dao文件,model文件。

1、所需文件

关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases

以下是相关文件截图:

 

从以上截图中可以看出来有一个generator.xml文件,主要就是修改这个文件中的配置信息。

mybaits-generator-core-1032.jar为mybaits生成器的核心包。

mysql-connector-java-5.1.34.jar为连接mysql的jar包;ojdbc6.jar为连接oracle的jar包,根据需要选择不同的jar包。

generatorConfig.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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>
    <!-- 数据库驱动包位置 -->
    <classPathEntry location="E:\web\java_mybits\generator\ojdbc6.jar" />   <!-- 本次我的数据库为oracle,所以选择此jar包 -->  <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhots:3306/my_db?characterEncoding=utf8" userId="root" password="123456">-->
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@11.10.97.14:1521:TESTOMS" userId="kfomswas" password="kfomswas">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="dh.model" targetProject="E:\web\java_mybits\generator\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="dh.mapping" targetProject="E:\web\java_mybits\generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="dh.dao" targetProject="E:\web\java_mybits\generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="DH_DESIGN_TASK" domainObjectName="DhDesignTask" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />       <!--           <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />            -->
    </context>
</generatorConfiguration>

 如上代码所示:

修改下划线部分就能够生成相应的文件。多表一次性生成文件只需在上面的红色标注部分的注释释放掉,并修改相应的表名即可。

2、生成语句

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

3、生成步骤

 

在该目录下的路径框内输入cmd,并按Enter,输入生成语句

再回车之后显示:MyBatis Generator finished successfully

在该目录下就生成了相应的文件

 相应的文件已上传至文件库。

posted @   怡安  阅读(558)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示