mybatis-generator【mybatis代码自动生成】

mybatis-generator是Mybatis的代码自动生成工具,能够节省开发者编写Mapping映射文件的时间。该工具需要配置相关jar包和XML文件,并且依赖于已有的数据库表。在Windows环境下,通过命令行运行java jar命令可以自动生成UserMapper.java、UserMapper.xml和User.java等文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、简介

mybatis属于半自动的ORM,使用其需要大量的编写Mapping的映射文件xml。因此可能会需要大量的时间,浪费开发时间。mybatis-generator是一个可以方便快速自动生成对应表的mapping映射文件xml。


二、准备

需要三个jar包和一个配置文件xml


下载



三、配置generator


注意:因为mybatis是半自动的,所以数据库必须要有表,所以还是需要手动建表。


<?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="E:\generator\mysql-connector-java-5.1.24-bin.jar" /> 
	<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 数据库链接URL、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" userId="root" password="12345678">
		<!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa"> -->
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="com.wuhn.bean" targetProject="D:\Users\Administrator\Workspaces\MyEclipse 10\test\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成的映射文件包名和位置 -->
		<sqlMapGenerator targetPackage="com.wuhn.mapping" targetProject="D:\Users\Administrator\Workspaces\MyEclipse 10\test\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.wuhn.dao" targetProject="D:\Users\Administrator\Workspaces\MyEclipse 10\test\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
		<table tableName="user_info" domainObjectName="Test" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		
	</context>
</generatorConfiguration>



四、使用

1、在window下操作

2、打开cmd,进入文件夹

3、使用命令

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


自动生成三个文件



UserMapper.java

package com.wuhn.dao;

import com.wuhn.module.User;

public interface UserMapper {
    int deleteByPrimaryKey(String userid);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(String userid);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}


UserMapper.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.wuhn.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.wuhn.module.User" >
    <id column="userid" property="userid" jdbcType="VARCHAR" />
    <result column="nickname" property="nickname" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="gender" property="gender" jdbcType="INTEGER" />
    <result column="birthday" property="birthday" jdbcType="DATE" />
    <result column="picture" property="picture" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    userid, nickname, email, password, name, gender, birthday, picture
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user
    where userid = #{userid,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from user
    where userid = #{userid,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.wuhn.module.User" >
    insert into user (userid, nickname, email, 
      password, name, gender, 
      birthday, picture)
    values (#{userid,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, 
      #{password,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{gender,jdbcType=INTEGER}, 
      #{birthday,jdbcType=DATE}, #{picture,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.wuhn.module.User" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="userid != null" >
        userid,
      </if>
      <if test="nickname != null" >
        nickname,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="gender != null" >
        gender,
      </if>
      <if test="birthday != null" >
        birthday,
      </if>
      <if test="picture != null" >
        picture,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="userid != null" >
        #{userid,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null" >
        #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="gender != null" >
        #{gender,jdbcType=INTEGER},
      </if>
      <if test="birthday != null" >
        #{birthday,jdbcType=DATE},
      </if>
      <if test="picture != null" >
        #{picture,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.wuhn.module.User" >
    update user
    <set >
      <if test="nickname != null" >
        nickname = #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="gender != null" >
        gender = #{gender,jdbcType=INTEGER},
      </if>
      <if test="birthday != null" >
        birthday = #{birthday,jdbcType=DATE},
      </if>
      <if test="picture != null" >
        picture = #{picture,jdbcType=VARCHAR},
      </if>
    </set>
    where userid = #{userid,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.wuhn.module.User" >
    update user
    set nickname = #{nickname,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      name = #{name,jdbcType=VARCHAR},
      gender = #{gender,jdbcType=INTEGER},
      birthday = #{birthday,jdbcType=DATE},
      picture = #{picture,jdbcType=VARCHAR}
    where userid = #{userid,jdbcType=VARCHAR}
  </update>
</mapper>


User.java

package com.wuhn.module;

import java.util.Date;

public class User {
    private String userid;

    private String nickname;

    private String email;

    private String password;

    private String name;

    private Integer gender;

    private Date birthday;

    private String picture;

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid == null ? null : userid.trim();
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname == null ? null : nickname.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getPassword() {
        return password;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture == null ? null : picture.trim();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值