最新教程:如何在eclipse中安装mybatis generator插件,并利用插件自动在自己指定得项目文件下生成代码文件

前言

  • 在网上看了很多教程,试了很多次才发现自己的问题所在。下面做一个记录,以方便自己回头查看和帮到网上的朋友。

使用mybatis generator插件的目的

  • 主要为了可以在项目中节省书写大量的实体类(entity,model,pojo),dao文件和映射文件(xxx.xml),为程序员节省了好多时间。

安装步骤

  • 先下载mybatis generator
  • 方法:在eclipse中自动下载。这种方法是个人认为是最稳妥的。
  • 步骤:help->eclipse marketplace->在find搜索框中输入mybatis generator 在其右边点击Go即可搜索
    正常的eclipse是很快就能搜到该最新插件的,如图下所示:

     

    image.png

点击installed即可按步骤next下载,最后是installed anyway,然后restart eclipse即可成功完成mybatis generator插件的安装

如何利用插件自动在自己指定得项目文件下生成代码文件

  • 步骤:首先在自己的项目下右键选择new ->other->Mybatis->如图下,然后next

image.png

 

填写必要生成配置文件的路径和名称,点击finish即可生成配置文件

image.png

  • 第二步:填写配置文件中的一些配置,这里主要是要填写
    -- 连接数据库的本地jar路径
    -- 连接数据库的配置:用户名,密码等
    -- 还有生成实体类,dao类,映射文件的包路径等
    -- 要生成的表名和对应的实体类名等

在这里的配置很关键,我就是在这配置的时候按了网上的说法去写,结果没报错,他也没给我生成,郁闷的半天。下面贴上我的generateConfig.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="D:\MySQL5_7\mysql-connector-java-5.1.46.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/mytest?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=UTC" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.gtyyx.model" targetProject="mySpringBootDemo">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="mySpringBootDemo/src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.gtyyx.dao" targetProject="mySpringBootDemo">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

这里最关键的是targetProject标签下的填写,没有填对的话是不能自动生成的,他还没给你报错。网上很多的例子填写的都是错的。所以很多坑需要你去跳!

例如在生成映射文件的包名和位置的时候,网上很多填的是src/main/resources,正确的是应该在src/main/resources前还要加上项目名mySpringBootDemo/src/main/resources,在这里我的项目名是mySpringBootDemo,所以是targetProject中填的是这个样子mySpringBootDemo/src/main/resources
其他地方的也应该加上项目名

第三步:就是运行generateConfig.xml了!网上很多的做法也是不对的,我也不知道为什么。可能是版本不一样了吧。这里说一下我的做法

选择generateConfig.xml文件右键,然后是选择 run,选择run mybatis generator,即可自动给你生成所要的文件,非常方面

生成的文件展示

生成的实体类

package com.gtyyx.model;

public class User {
    private Integer userId;

    private String userName;

    private String password;

    private String phone;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }
}

生成的dao类

package com.gtyyx.dao;

import com.gtyyx.model.User;

public interface UserMapper {
    int insert(User record);

    int insertSelective(User record);
}

生成的映射文件

<?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="com.gtyyx.dao.UserMapper">
  <resultMap id="BaseResultMap" type="com.gtyyx.model.User">
    <result column="user_id" jdbcType="INTEGER" property="userId" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
  </resultMap>
  <insert id="insert" parameterType="com.gtyyx.model.User">
    insert into t_user (user_id, user_name, password, 
      phone)
    values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{phone,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.gtyyx.model.User">
    insert into t_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="userId != null">
        user_id,
      </if>
      <if test="userName != null">
        user_name,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="phone != null">
        phone,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="userId != null">
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="userName != null">
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
</mapper>

上面就是我在网上踩过坑时候毫无保留的跳出坑的过程记录,如果你看了还有我的还有坑的话,留言我,看我能不能把你拉出坑来!

网上的资料是好东西同时也可能是一个坑,当你无法填坑的时候,就应该另寻他法了,多尝试,不放弃,最后你才能如愿做出你想要的事情!



作者:健倾心语
链接:https://www.jianshu.com/p/38610774bd78
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值