iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解MybatisGenerator的具体使用教程
  • 245
分享到

详解MybatisGenerator的具体使用教程

2024-04-02 19:04:59 245人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录1、相关文件2、使用方法mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Gen

mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。

1、相关文件

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

由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar

以下是相关文件截图:

和Hibernate逆向生成一样,这里也需要一个配置文件:

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>
    <!--数据库驱动-->
    <classPathEntry    location="mysql-connector-java-5.0.8-bin.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" passWord="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="lcw.model" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。

上面配置文件中的:

<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。

生成语句文件:

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

2、使用方法

在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。

看下效果图:

生成相关代码:

Message.java

package lcw.model;
public class Messgae {
    private Integer id;
    private String title;
    private String describe;
    private String content;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title == null ? null : title.trim();
    }
    public String getDescribe() {
        return describe;
    }
    public void setDescribe(String describe) {
        this.describe = describe == null ? null : describe.trim();
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content == null ? null : content.trim();
    }
}

MessgaeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="lcw.dao.MessgaeMapper" >
  <resultMap id="BaseResultMap" type="lcw.model.Messgae" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="title" property="title" jdbcType="VARCHAR" />
    <result column="describe" property="describe" jdbcType="VARCHAR" />
    <result column="content" property="content" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, title, describe, content
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from message
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from message
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="lcw.model.Messgae" >
    insert into message (id, title, describe,
      content)
    values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{describe,jdbcType=VARCHAR},
      #{content,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="lcw.model.Messgae" >
    insert into message
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="title != null" >
        title,
      </if>
      <if test="describe != null" >
        describe,
      </if>
      <if test="content != null" >
        content,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="title != null" >
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="describe != null" >
        #{describe,jdbcType=VARCHAR},
      </if>
      <if test="content != null" >
        #{content,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="lcw.model.Messgae" >
    update message
    <set >
      <if test="title != null" >
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="describe != null" >
        describe = #{describe,jdbcType=VARCHAR},
      </if>
      <if test="content != null" >
        content = #{content,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="lcw.model.Messgae" >
    update message
    set title = #{title,jdbcType=VARCHAR},
      describe = #{describe,jdbcType=VARCHAR},
      content = #{content,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

MessgaeMapper.java

package lcw.dao;
import lcw.model.Messgae;
public interface MessgaeMapper {
    int deleteByPrimaryKey(Integer id);
    int insert(Messgae record);
    int insertSelective(Messgae record);
    Messgae selectByPrimaryKey(Integer id);
    int updateByPrimaryKeySelective(Messgae record);
    int updateByPrimaryKey(Messgae record);
}

到此这篇关于详解Mybatis Generator的具体使用教程的文章就介绍到这了,更多相关Mybatis Generator使用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解MybatisGenerator的具体使用教程

本文链接: https://www.lsjlt.com/news/138647.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 详解MybatisGenerator的具体使用教程
    目录1、相关文件2、使用方法Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Gen...
    99+
    2024-04-02
  • MybatisGenerator具体使用小技巧
    目录1、问题描述2、解决方案2.1pom中指定mybatisgenerator插件2.2generatorConfig.xml2.3执行2.4总结1、问题描述 mybatis gen...
    99+
    2024-04-02
  • Android入门教程之RecyclerView的具体使用详解
    目录RecyclerView 的基本用法横向滚动RecyclerView 点击事件RecyclerView 的基本用法 和我们之前学习的控件不一样,RecyclerView 属于新增...
    99+
    2024-04-02
  • Android入门教程之Fragment的具体使用详解
    目录Fragment 的简单用法动态加载 FragmentFragment 实现返回栈Fragment 和 Activity 之间的交互Fragment 生命周期Fragment 的...
    99+
    2024-04-02
  • Android入门教程之ListView的具体使用详解
    目录ListView 的简单用法定制 ListView 的界面提升 ListView 的运行效率ListView 的点击事件ListView 的简单用法 在布局中加入 ListVie...
    99+
    2024-04-02
  • flv.js的具体使用教程
    目录简介flv.js常用方法flv.js简单使用简介 Flv.js 是 HTML5 Flash 视频(FLV)播放器,纯原生 JavaScript 开发,没有用到 Flash。由 b...
    99+
    2023-05-19
    flv.js使用 flv.js
  • pandas.DataFrame.iloc的具体使用详解
    目录第一种 整数做索引第二种 列表或数组做索引第三种 利用切片做索引第四种 Boolean数组做索引第五种 带一个参数的可调用函数做索引今天学习时遇到了这个方法,为了加深理解做一下笔...
    99+
    2024-04-02
  • KotlinFragment的具体使用详解
    目录概念基本示例设置左右列布局文件配置左右布局类主布局文件注册概念 fragment 可以用作一个 activity 内部的小分块; 当我们从手机转换到 pad 上时,整体界面会发生...
    99+
    2024-04-02
  • JNDI具体用法详解
    JNDI全称(Java Naming and Directory Interface),是java命名和目录接口。它是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和...
    99+
    2024-04-02
  • Golang开发之接口的具体使用详解
    目录Golang的接口是什么什么情况下要用接口实战案例多态的例子定义通用方法的例子松耦合的例子实现插件化架构的例子Golang的接口是什么 在 Golang 中,接口是一种类型,它是...
    99+
    2023-05-14
    Golang接口使用 Golang接口 Go 接口
  • Pygame Time时间控制的具体使用详解
    pygame.time 时间控制模块,是 Pygame 中使用频率较高的模块,其主要功能是管理时间和游戏帧数率(即 FPS)。 时间在游戏开发中承担着非常重要的作用,比如释放某个技能...
    99+
    2024-04-02
  • Android View的事件体系教程详解
    目录一、什么是View?什么是ViewGroup?二、View的位置三、View的触摸事件1.MotionEvent2.TouchSlop3.VelocityTracker5.Scr...
    99+
    2024-04-02
  • Pandas中GroupBy具体用法详解
    目录简介 分割数据 多index get_group dropna groups属性 index的层级 group的遍历 聚合操作 通用聚合方法 同时使用多个聚合方法 Na...
    99+
    2024-04-02
  • 详解SQL之CASEWHEN具体用法
    简单CASE WHEN函数: CASE SCORE WHEN 'A' THEN '优' ELSE '不及格' END CASE SCORE WHEN 'B' THEN '良' ELS...
    99+
    2024-04-02
  • Java中JDBC的使用教程详解
    目录概念快速入门步骤代码实现详解各个对象DriverManager:驱动管理对象Connection:数据库连接对象Statement:执行sql的对象ResultSet:结果集对象...
    99+
    2024-04-02
  • Android Notification使用教程详解
    目录前言正文一、Android中通知的变化1. Android 4.1,API 162. Android 4.4,API 19 和 203. Android 5.0,API 214....
    99+
    2024-04-02
  • wireshark工具详解、数据包抓取分析、使用教程
    Wireshark界面 Wireshark查看数据捕获列表 数据包概要信息窗口:描述每个数据包的基本信息。如图,点击某行数据,即可在下方显示该数据包的信息。 数据包解析窗口:显示被选中的数据包的解析信息,包含每个数据包的整体信息、数据链...
    99+
    2023-08-23
    网络 tcp/ip 服务器
  • MyBatis中OGNL的使用教程详解
    前言本文主要给大家讲如何在MyBatis中使用OGNL的相关内容,分享出来供大家参考学习,感兴趣的朋友们下面来一起看看详细的介绍:如果我们搜索OGNL相关的内容,通常的结果都是和Struts有关的,你肯定搜不到和MyBatis有关的,虽然和...
    99+
    2023-05-31
    mybatis 使用 ognl
  • SpringCloud中Gateway的使用教程详解
    目录1.基础教程2.将配置放在配置文件里3.放在springcloud里面4.使用服务名而不是IP1.基础教程 pom.xml <parent> ...
    99+
    2022-11-13
    SpringCloud Gateway使用 SpringCloud Gateway
  • 重学Go语言之数组的具体使用详解
    目录什么是数组数组的创建访问数组的元素数组的长度如何遍历数组数组的比较查找数组中的元素将数组作为函数参数二维与多维数组小结什么是数组 什么是数组?数组是有固定长度的相同数据类型元素的...
    99+
    2023-02-28
    Go语言 数组使用 Go语言 数组 Go 数组
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作