可乐分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 546|回复: 11

[开发教程] Eclipse中安装使用MyBatis generator

[复制链接]

416

主题

2

回帖

1416

积分

管理员

Rank: 12Rank: 12Rank: 12

积分
1416
发表于 2024-9-4 23:10:45 | 显示全部楼层 |阅读模式

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件,它能够帮我们自动生成相应的实体类、Dao接口以及映射文件。

1.Eclipse中安装mybatis generator插件

mybatis generator下载地址:https://github.com/mybatis/generator/releases

第一步:下载Mybatis Generator插件将features和plugins这2个文件夹放到eclipse的安装目录下面,重启后eclipse即可。

1.png


第二步:验证是否配置成功,在线配置也是这么验证的,菜单File=>new=>others,输入Mybatis出现如下图标就代表配置成功了。
2.png

2.配置使用 mybatis generator

选中项目=>右键=》New=》other=》选择如上图所示=》Next=>Finish

3.png


然后会生成一个generatorConfig.xml文件,配置该文件,如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3. <generatorConfiguration>
  4.     <!--数据库驱动-->
  5.     <classPathEntry    location="E:\eclipse-workspace\FrameStudy\SSM_MyBatisGenerator\WebContent\WEB-INF\lib\mysql-connector-java-5.1.6-bin.jar"/>
  6.     <context id="mysql">
  7.          <!--设置去除自动生成的注释-->
  8.         <commentGenerator>
  9.             <property name="suppressDate" value="true"/>
  10.             <property name="suppressAllComments" value="true"/>
  11.         </commentGenerator>
  12.         <!--数据库链接地址账号密码-->
  13.         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/emps" userId="root" password="">
  14.         </jdbcConnection>
  15.         <javaTypeResolver>
  16.             <property name="forceBigDecimals" value="false"/>
  17.         </javaTypeResolver>
  18.         <!--生成Model类存放位置-->
  19.         <javaModelGenerator targetPackage="com.ssmdemo.model" targetProject="SSM_MyBatisGenerator\src">
  20.             <property name="enableSubPackages" value="true"/>
  21.             <property name="trimStrings" value="true"/>
  22.         </javaModelGenerator>
  23.         <!--生成映射文件存放位置-->
  24.         <sqlMapGenerator targetPackage="ssmdemo.mapping" targetProject="SSM_MyBatisGenerator\src">
  25.             <property name="enableSubPackages" value="true"/>
  26.         </sqlMapGenerator>
  27.         <!--生成mapper类存放位置-->
  28.         <javaClientGenerator type="XMLMAPPER" targetPackage="com.ssmdemo.mapper" targetProject="SSM_MyBatisGenerator\src">
  29.             <property name="enableSubPackages" value="true"/>
  30.         </javaClientGenerator>
  31.         <!--生成对应表及类名-->

  32.         <table tableName="employee" domainObjectName="Employee" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

  33.     </context>
  34. </generatorConfiguration>
复制代码

选择generatorConfig.xml文件=>右键=》Run As =>Run mybatis Generator,然后它就会自动帮我们生成相应的实体类、Dao接口、实体类映射文件。


EmployeeMapper.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.ssmdemo.mapper.EmployeeMapper">
  4.   <resultMap id="BaseResultMap" type="com.ssmdemo.model.Employee">
  5.     <id column="eid" jdbcType="INTEGER" property="eid" />
  6.     <result column="cid" jdbcType="INTEGER" property="cid" />
  7.     <result column="name" jdbcType="VARCHAR" property="name" />
  8.     <result column="height" jdbcType="REAL" property="height" />
  9.     <result column="weight" jdbcType="REAL" property="weight" />
  10.   </resultMap>
  11.   <sql id="Base_Column_List">
  12.     eid, cid, name, height, weight
  13.   </sql>
  14.   <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  15.     select
  16.     <include refid="Base_Column_List" />
  17.     from employee
  18.     where eid = #{eid,jdbcType=INTEGER}
  19.   </select>
  20.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  21.     delete from employee
  22.     where eid = #{eid,jdbcType=INTEGER}
  23.   </delete>
  24.   <insert id="insert" parameterType="com.ssmdemo.model.Employee">
  25.     insert into employee (eid, cid, name,
  26.       height, weight)
  27.     values (#{eid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
  28.       #{height,jdbcType=REAL}, #{weight,jdbcType=REAL})
  29.   </insert>
  30.   <insert id="insertSelective" parameterType="com.ssmdemo.model.Employee">
  31.     insert into employee
  32.     <trim prefix="(" suffix=")" suffixOverrides=",">
  33.       <if test="eid != null">
  34.         eid,
  35.       </if>
  36.       <if test="cid != null">
  37.         cid,
  38.       </if>
  39.       <if test="name != null">
  40.         name,
  41.       </if>
  42.       <if test="height != null">
  43.         height,
  44.       </if>
  45.       <if test="weight != null">
  46.         weight,
  47.       </if>
  48.     </trim>
  49.     <trim prefix="values (" suffix=")" suffixOverrides=",">
  50.       <if test="eid != null">
  51.         #{eid,jdbcType=INTEGER},
  52.       </if>
  53.       <if test="cid != null">
  54.         #{cid,jdbcType=INTEGER},
  55.       </if>
  56.       <if test="name != null">
  57.         #{name,jdbcType=VARCHAR},
  58.       </if>
  59.       <if test="height != null">
  60.         #{height,jdbcType=REAL},
  61.       </if>
  62.       <if test="weight != null">
  63.         #{weight,jdbcType=REAL},
  64.       </if>
  65.     </trim>
  66.   </insert>
  67.   <update id="updateByPrimaryKeySelective" parameterType="com.ssmdemo.model.Employee">
  68.     update employee
  69.     <set>
  70.       <if test="cid != null">
  71.         cid = #{cid,jdbcType=INTEGER},
  72.       </if>
  73.       <if test="name != null">
  74.         name = #{name,jdbcType=VARCHAR},
  75.       </if>
  76.       <if test="height != null">
  77.         height = #{height,jdbcType=REAL},
  78.       </if>
  79.       <if test="weight != null">
  80.         weight = #{weight,jdbcType=REAL},
  81.       </if>
  82.     </set>
  83.     where eid = #{eid,jdbcType=INTEGER}
  84.   </update>
  85.   <update id="updateByPrimaryKey" parameterType="com.ssmdemo.model.Employee">
  86.     update employee
  87.     set cid = #{cid,jdbcType=INTEGER},
  88.       name = #{name,jdbcType=VARCHAR},
  89.       height = #{height,jdbcType=REAL},
  90.       weight = #{weight,jdbcType=REAL}
  91.     where eid = #{eid,jdbcType=INTEGER}
  92.   </update>
  93. </mapper>

复制代码

EmployeeMapper.java



  1. package com.ssmdemo.mapper;

  2. import com.ssmdemo.model.Employee;

  3. public interface EmployeeMapper {
  4. int deleteByPrimaryKey(Integer eid);

  5. int insert(Employee record);

  6. int insertSelective(Employee record);

  7. Employee selectByPrimaryKey(Integer eid);

  8. int updateByPrimaryKeySelective(Employee record);

  9. int updateByPrimaryKey(Employee record);
  10. }
复制代码

Employee.java

  1. package com.ssmdemo.model;

  2. public class Employee {
  3.     private Integer eid;

  4.     private Integer cid;

  5.     private String name;

  6.     private Float height;

  7.     private Float weight;

  8.     public Integer getEid() {
  9.         return eid;
  10.     }

  11.     public void setEid(Integer eid) {
  12.         this.eid = eid;
  13.     }

  14.     public Integer getCid() {
  15.         return cid;
  16.     }

  17.     public void setCid(Integer cid) {
  18.         this.cid = cid;
  19.     }

  20.     public String getName() {
  21.         return name;
  22.     }

  23.     public void setName(String name) {
  24.         this.name = name == null ? null : name.trim();
  25.     }

  26.     public Float getHeight() {
  27.         return height;
  28.     }

  29.     public void setHeight(Float height) {
  30.         this.height = height;
  31.     }

  32.     public Float getWeight() {
  33.         return weight;
  34.     }

  35.     public void setWeight(Float weight) {
  36.         this.weight = weight;
  37.     }
复制代码



可乐分享 - 免责声明1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站信息来自网络,版权争议与本站无关
3、本站发布的相关帖子文章仅限用于学习和研究目的,不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负
4、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
5、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
6、可乐分享管理员和版主有权不事先通知发贴者而删除本文

回复

使用道具 举报

0

主题

544

回帖

825

积分

高级会员

Rank: 6Rank: 6

积分
825
发表于 2024-9-15 08:21:20 | 显示全部楼层
楼主节操掉了,还不快捡起来
回复

使用道具 举报

0

主题

494

回帖

750

积分

高级会员

Rank: 6Rank: 6

积分
750
发表于 2024-9-26 03:40:27 | 显示全部楼层
我的一楼总是献给度娘
回复

使用道具 举报

0

主题

502

回帖

762

积分

高级会员

Rank: 6Rank: 6

积分
762
发表于 2024-10-9 02:08:35 | 显示全部楼层
楼主节操掉了,还不快捡起来
回复

使用道具 举报

0

主题

522

回帖

792

积分

高级会员

Rank: 6Rank: 6

积分
792
发表于 2024-10-19 09:54:53 | 显示全部楼层
如果这就是爱,再转身的时候就该留下
回复

使用道具 举报

0

主题

492

回帖

747

积分

高级会员

Rank: 6Rank: 6

积分
747
发表于 2024-12-7 17:34:38 | 显示全部楼层
信春哥,得永生!keleshare.cn
回复

使用道具 举报

0

主题

518

回帖

786

积分

高级会员

Rank: 6Rank: 6

积分
786
发表于 2024-12-22 00:23:17 | 显示全部楼层
信春哥,得永生!keleshare.cn
回复

使用道具 举报

0

主题

562

回帖

852

积分

高级会员

Rank: 6Rank: 6

积分
852
发表于 2025-2-10 23:48:29 | 显示全部楼层
楼主听话,快到碗里来!
回复

使用道具 举报

0

主题

334

回帖

504

积分

高级会员

Rank: 6Rank: 6

积分
504
发表于 2025-2-18 06:52:25 | 显示全部楼层
楼主你知道的太多了。
回复

使用道具 举报

0

主题

554

回帖

840

积分

高级会员

Rank: 6Rank: 6

积分
840
发表于 2025-3-18 18:24:35 | 显示全部楼层
如果这就是爱,再转身的时候就该留下
回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【可乐分享www.keleshare.cn】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|可乐分享,站长交流 ( 苏ICP备2024109924号 )|网站地图

GMT+8, 2025-5-7 03:10 , Processed in 0.128664 second(s), 24 queries .

Powered by 可乐分享

© 2023-2024 keleshare.cn.

快速回复 返回顶部 返回列表