一、首先创建表
根据自己情况创建,我这里用的是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;
}
}
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>