使用MyBatis Generator生成实体类,映射文件和DAO接口

使用MyBatis Generator生成实体类,映射文件和DAO接口

简介

MyBatis属于一种半自动的ORM框架,主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件,这样可以省去很多的功夫。单独创建一个工程用来生成所需文件,再将生成的代码copy到项目工程中即可。

用于生成文件的工程目录结构如下:
项目结构
在generator.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="ojdbc7-12.1.0.2.jar"/>
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true"/>
			<!-- 是否去除自动生成的注释 -->
			<property name="suppressAllComments" value="true"/>
		</commentGenerator>
		
		<!-- 数据库链接URL,用户名,密码 -->
		<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@your db ip:port:SID" userId="username" password="pwd">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false"/>
		</javaTypeResolver>
		<!-- 生成模型和包名的位置 -->
		<javaModelGenerator targetPackage="com.xxx.model" targetProject="src">
			<property name="enableSubPackages" value="true"/>
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>
		<!-- 生成映射文件的包名和位置 -->
		<sqlMapGenerator targetPackage="com.xxx.mapper" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator targetPackage="com.xxx.dao" type="XMLMAPPER" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>
		<!-- 要生成的表tableName是数据库中的表或者视图名,domainObjectName是实体类名 -->
		<table tableName="Student" domainObjectName="Student"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
		selectByExampleQueryId="false">
		</table>
	</context>
</generatorConfiguration>

打开CMD窗口,执行命令:

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

cmd
执行成功后,可以看到项目中自动生成的文件:
生成成功后的项目结构
StudentMapper.java

package com.xxx.dao;

import com.xxx.model.Student;

public interface StudentMapper {
    int deleteByPrimaryKey(String stid);

    int insert(Student record);

    int insertSelective(Student record);

    Student selectByPrimaryKey(String stid);

    int updateByPrimaryKeySelective(Student record);

    int updateByPrimaryKey(Student record);
}

StudentMapper.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="com.xxx.dao.StudentMapper" >
  <resultMap id="BaseResultMap" type="com.xxx.model.Student" >
    <id column="STID" property="stid" jdbcType="VARCHAR" />
    <result column="SNAME" property="sname" jdbcType="VARCHAR" />
    <result column="SAGE" property="sage" jdbcType="DECIMAL" />
    <result column="SSEX" property="ssex" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    STID, SNAME, SAGE, SSEX
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from STUDENT
    where STID = #{stid,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from STUDENT
    where STID = #{stid,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.xxx.model.Student" >
    insert into STUDENT (STID, SNAME, SAGE, 
      SSEX)
    values (#{stid,jdbcType=VARCHAR}, #{sname,jdbcType=VARCHAR}, #{sage,jdbcType=DECIMAL}, 
      #{ssex,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.xxx.model.Student" >
    insert into STUDENT
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="stid != null" >
        STID,
      </if>
      <if test="sname != null" >
        SNAME,
      </if>
      <if test="sage != null" >
        SAGE,
      </if>
      <if test="ssex != null" >
        SSEX,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="stid != null" >
        #{stid,jdbcType=VARCHAR},
      </if>
      <if test="sname != null" >
        #{sname,jdbcType=VARCHAR},
      </if>
      <if test="sage != null" >
        #{sage,jdbcType=DECIMAL},
      </if>
      <if test="ssex != null" >
        #{ssex,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.xxx.model.Student" >
    update STUDENT
    <set >
      <if test="sname != null" >
        SNAME = #{sname,jdbcType=VARCHAR},
      </if>
      <if test="sage != null" >
        SAGE = #{sage,jdbcType=DECIMAL},
      </if>
      <if test="ssex != null" >
        SSEX = #{ssex,jdbcType=VARCHAR},
      </if>
    </set>
    where STID = #{stid,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.xxx.model.Student" >
    update STUDENT
    set SNAME = #{sname,jdbcType=VARCHAR},
      SAGE = #{sage,jdbcType=DECIMAL},
      SSEX = #{ssex,jdbcType=VARCHAR}
    where STID = #{stid,jdbcType=VARCHAR}
  </update>
</mapper>

Student.java

package com.xxx.model;

public class Student {
    private String stid;

    private String sname;

    private Short sage;

    private String ssex;

    public String getStid() {
        return stid;
    }

    public void setStid(String stid) {
        this.stid = stid == null ? null : stid.trim();
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname == null ? null : sname.trim();
    }

    public Short getSage() {
        return sage;
    }

    public void setSage(Short sage) {
        this.sage = sage;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex == null ? null : ssex.trim();
    }
}

自此,mybatis generator自动生成文件已全部完成,接下来将对应的文件拷贝到项目中即可。^ . ^

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值