详解Mybatis Generator的具体使用教程

作者:zorro的菜鸟笔记 时间:2022-01-16 22:39:13 

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

1、相关文件

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

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

以下是相关文件截图:

详解Mybatis Generator的具体使用教程

和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键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。

看下效果图:

详解Mybatis Generator的具体使用教程

详解Mybatis Generator的具体使用教程

生成相关代码:

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);
}

来源:https://www.cnblogs.com/zorro-y/p/5602471.html

标签:Mybatis,Generator,使用
0
投稿

猜你喜欢

  • SpringCloud:feign对象传参和普通传参及遇到的坑解决

    2023-02-17 11:18:58
  • Java 遍历取出Map集合key-value数据的4种方法

    2022-02-03 02:48:59
  • java实现excel和txt文件互转

    2023-10-07 23:04:05
  • Android Handle原理(Looper,Handler和Message)三者关系案例详解

    2023-08-25 22:51:47
  • java.lang.NoClassDefFoundError错误解决办法

    2021-12-29 03:52:27
  • Java实现FTP上传到服务器

    2022-10-07 10:28:58
  • Android 动画之TranslateAnimation应用详解

    2023-06-27 06:17:59
  • C#实现的简单验证码识别实例

    2022-09-16 03:44:11
  • Spring Security OAuth过期的解决方法

    2023-05-26 22:30:01
  • 浅析C++字节对齐容易被忽略的两个问题

    2022-06-02 20:29:59
  • TC 集群Seata1.6高可用架构源码解析

    2022-04-18 05:02:34
  • Java基础-Java常量和常量值

    2023-08-03 03:49:03
  • Java中获取文件大小的详解及实例代码

    2023-02-27 08:12:27
  • Java 添加、替换、删除PDF中的图片的示例代码

    2023-08-28 09:06:26
  • Unity3D实现渐变颜色效果

    2022-09-03 08:14:32
  • adb通过wifi连接android设备流程解析

    2021-12-27 08:39:37
  • 解析Android开发优化之:对界面UI的优化详解(一)

    2023-05-23 17:45:10
  • Java Swing组件编程之JTable表格用法实例详解

    2022-12-23 01:49:26
  • JavaEE中用response向客户端输出中文数据乱码问题分析

    2022-07-14 00:39:27
  • JavaWeb开发之JSTL标签库的使用、 自定义EL函数、自定义标签(带属性的、带标签体的)

    2021-08-22 10:25:00
  • asp之家 软件编程 m.aspxhome.com