Mybatis_Generator自动生成Pojo.Dao.Mapping

本文介绍如何使用MyBatis Generator自动生成DAO类、Mapper接口及XML文件。包括创建数据库表、下载并配置MyBatis Generator核心工具,以及通过命令行运行生成脚本。

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

一、首先创建表

根据自己情况创建,我这里用的是sqlserver2012


二、下载mybatis-generator-core



     下载地址:点击下载此文件


三、配置文件

打开下载的mybatis-generator-core,文件中有geteratorConfig.xml,



四、运行

通过cmd命令方式来运行,启动cmd进入到”G:\WYT\mybatis\mybatis-generator-core-1.3.2\lib”这个目录下,Win+R,然后输入 cd /d”G:\WYT\mybatis\mybatis-generator-core-1.3.2\lib”,进入目录,然后输入运行的脚本java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite”,如下图


五、查看生成的文件


Dao类:

package wyt.domain;

public class User {
    private Integer id;

    private String username;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}


Mapper类:
package wyt.IDao;

import wyt.domain.User;

public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}
Mapper类:

<?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="wyt.IDao.UserMapper">
  <resultMap id="BaseResultMap" type="wyt.domain.User">
    <id column="Id" jdbcType="INTEGER" property="id" />
    <result column="Username" jdbcType="NVARCHAR" property="username" />
    <result column="Password" jdbcType="NVARCHAR" property="password" />
    <result column="Age" jdbcType="INTEGER" property="age" />
  </resultMap>
  <sql id="Base_Column_List">
    Id, Username, Password, Age
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from User
    where Id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from User
    where Id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="wyt.domain.User">
    insert into User (Id, Username, Password, 
      Age)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=NVARCHAR}, #{password,jdbcType=NVARCHAR}, 
      #{age,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="wyt.domain.User">
    insert into User
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        Id,
      </if>
      <if test="username != null">
        Username,
      </if>
      <if test="password != null">
        Password,
      </if>
      <if test="age != null">
        Age,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=NVARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=NVARCHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="wyt.domain.User">
    update User
    <set>
      <if test="username != null">
        Username = #{username,jdbcType=NVARCHAR},
      </if>
      <if test="password != null">
        Password = #{password,jdbcType=NVARCHAR},
      </if>
      <if test="age != null">
        Age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where Id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="wyt.domain.User">
    update User
    set Username = #{username,jdbcType=NVARCHAR},
      Password = #{password,jdbcType=NVARCHAR},
      Age = #{age,jdbcType=INTEGER}
    where Id = #{id,jdbcType=INTEGER}
  </update>
  <resultMap id="BaseResultMap" type="wyt.domain.User">
    <id column="Id" jdbcType="INTEGER" property="id" />
    <result column="Username" jdbcType="NVARCHAR" property="username" />
    <result column="Password" jdbcType="NVARCHAR" property="password" />
    <result column="Age" jdbcType="INTEGER" property="age" />
  </resultMap>
  <sql id="Base_Column_List">
    Id, Username, Password, Age
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from User
    where Id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from User
    where Id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="wyt.domain.User">
    insert into User (Id, Username, Password, 
      Age)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=NVARCHAR}, #{password,jdbcType=NVARCHAR}, 
      #{age,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="wyt.domain.User">
    insert into User
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        Id,
      </if>
      <if test="username != null">
        Username,
      </if>
      <if test="password != null">
        Password,
      </if>
      <if test="age != null">
        Age,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=NVARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=NVARCHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="wyt.domain.User">
    update User
    <set>
      <if test="username != null">
        Username = #{username,jdbcType=NVARCHAR},
      </if>
      <if test="password != null">
        Password = #{password,jdbcType=NVARCHAR},
      </if>
      <if test="age != null">
        Age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where Id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="wyt.domain.User">
    update User
    set Username = #{username,jdbcType=NVARCHAR},
      Password = #{password,jdbcType=NVARCHAR},
      Age = #{age,jdbcType=INTEGER}
    where Id = #{id,jdbcType=INTEGER}
  </update>
</mapper>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值