使用代码生成器能快速的生成mapper、实体类、以及mapper映射文件,大大提高了开发效率,具体步骤如下:
1、新建一个格式为XML的配置文件 名称generatorConfig
我把配置文件放在了resources目录下的generator下
<?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>
<!-- 1、生成文件的命令:java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
2、在mybatis-generator-core-1.3.6.jar所在目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成文件的命令即可。 -->
<!-- 数据库驱动包位置 -->
<classPathEntry location="D:\apache-maven-3.6.1\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:8080/mp?characterEncoding=utf-8"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- pojo -->
<javaModelGenerator targetPackage="com.caochenlei.mpdemo.pojo"
targetProject="E:\Test\mp-demo\src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- xml -->
<sqlMapGenerator targetPackage="com.caochenlei.mpdemo.mapper"
targetProject="E:\Test\mp-demo\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- DAo -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.caochenlei.mpdemo.mapper"
targetProject="E:\Test\mp-demo\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="Test_case" domainObjectName="TestCaseDO" enableDeleteByExample="false" >
</table>
</context>
</generatorConfiguration>
需要修改的有:
1、数据库驱动包位置
2、数据库地址以及账号密码
3、实体类的包名以及路径
4、xml映射文件的包名及路径
5、DAO的路径
6、tableName表名称 domainObjectName 需要生成的类名(会根据配置的库名中的表名生成实体类、mapper、mapper映射文件)
二、下载 mybatis-generator-core-1.3.6.jar
链接:https://pan.baidu.com/s/1URuElNFllYMNCIBWBYfrbw
提取码:obqj
和XML文件放在同一个目录下
三、进入Terminal命令界面 cd到jar包放置的目录
resources目录下的generator下
cd src\main\resources\generator
输入如下命令java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
回车即可
四、看看效果
实体类:
package com.caochenlei.mpdemo.pojo;
import java.sql.Date;
public class SysUserDO {
private Long uid;
private String userName;
private String passWord;
private String nickName;
private String mobile;
private String email;
private String createTime;
private String updateTime;
public Long getUid() {
return uid;
}
public void setUid(Long uid) {
this.uid = uid;
}
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 String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName == null ? null : nickName.trim();
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile == null ? null : mobile.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
}
mapper:
package com.caochenlei.mpdemo.mapper;
import com.caochenlei.mpdemo.pojo.SysUserDO;
import com.caochenlei.mpdemo.pojo.SysUserDOExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
public interface SysUserDOMapper {
long countByExample(SysUserDOExample example);
int deleteByPrimaryKey(Long uid);
int insert(SysUserDO record);
int insertSelective(SysUserDO record);
List<SysUserDO> selectByExampleWithRowbounds(SysUserDOExample example, RowBounds rowBounds);
List<SysUserDO> selectByExample(SysUserDOExample example);
SysUserDO selectByPrimaryKey(Long uid);
int updateByExampleSelective(@Param("record") SysUserDO record, @Param("example") SysUserDOExample example);
int updateByExample(@Param("record") SysUserDO record, @Param("example") SysUserDOExample example);
int updateByPrimaryKeySelective(SysUserDO record);
int updateByPrimaryKey(SysUserDO record);
List<SysUserDO> queryByPage(@Param("pageNo")Integer pageNo, @Param("pageSize")Integer pageSize);
}
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.caochenlei.mpdemo.mapper.SysUserDOMapper">
<resultMap id="BaseResultMap" type="com.caochenlei.mpdemo.pojo.SysUserDO">
<id column="uid" jdbcType="BIGINT" property="uid" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="pass_word" jdbcType="VARCHAR" property="passWord" />
<result column="nick_name" jdbcType="VARCHAR" property="nickName" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="create_time" jdbcType="DATE" property="createTime" />
<result column="update_time" jdbcType="DATE" property="updateTime" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
uid, user_name, pass_word, nick_name, mobile, email, create_time, update_time
</sql>
<select id="selectByExample" parameterType="com.caochenlei.mpdemo.pojo.SysUserDOExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from user_base
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user_base
where uid = #{uid,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from user_base
where uid = #{uid,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
insert into user_base (uid, user_name, pass_word,
nick_name, mobile, email,
create_time, update_time)
values (#{uid,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{passWord,jdbcType=VARCHAR},
#{nickName,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR},
#{createTime,jdbcType=DATE}, #{updateTime,jdbcType=DATE})
</insert>
<insert id="insertSelective" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
insert into user_base
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">
uid,
</if>
<if test="userName != null">
user_name,
</if>
<if test="passWord != null">
pass_word,
</if>
<if test="nickName != null">
nick_name,
</if>
<if test="mobile != null">
mobile,
</if>
<if test="email != null">
email,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="uid != null">
#{uid,jdbcType=BIGINT},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="passWord != null">
#{passWord,jdbcType=VARCHAR},
</if>
<if test="nickName != null">
#{nickName,jdbcType=VARCHAR},
</if>
<if test="mobile != null">
#{mobile,jdbcType=VARCHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=DATE},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=DATE},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.caochenlei.mpdemo.pojo.SysUserDOExample" resultType="java.lang.Long">
select count(*) from user_base
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update user_base
<set>
<if test="record.uid != null">
uid = #{record.uid,jdbcType=BIGINT},
</if>
<if test="record.userName != null">
user_name = #{record.userName,jdbcType=VARCHAR},
</if>
<if test="record.passWord != null">
pass_word = #{record.passWord,jdbcType=VARCHAR},
</if>
<if test="record.nickName != null">
nick_name = #{record.nickName,jdbcType=VARCHAR},
</if>
<if test="record.mobile != null">
mobile = #{record.mobile,jdbcType=VARCHAR},
</if>
<if test="record.email != null">
email = #{record.email,jdbcType=VARCHAR},
</if>
<if test="record.createTime != null">
create_time = #{record.createTime,jdbcType=DATE},
</if>
<if test="record.updateTime != null">
update_time = #{record.updateTime,jdbcType=DATE},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update user_base
set uid = #{record.uid,jdbcType=BIGINT},
user_name = #{record.userName,jdbcType=VARCHAR},
pass_word = #{record.passWord,jdbcType=VARCHAR},
nick_name = #{record.nickName,jdbcType=VARCHAR},
mobile = #{record.mobile,jdbcType=VARCHAR},
email = #{record.email,jdbcType=VARCHAR},
create_time = #{record.createTime,jdbcType=DATE},
update_time = #{record.updateTime,jdbcType=DATE}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
update user_base
<set>
<if test="userName != null">
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="passWord != null">
pass_word = #{passWord,jdbcType=VARCHAR},
</if>
<if test="nickName != null">
nick_name = #{nickName,jdbcType=VARCHAR},
</if>
<if test="mobile != null">
mobile = #{mobile,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=DATE},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=DATE},
</if>
</set>
where uid = #{uid,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.caochenlei.mpdemo.pojo.SysUserDO">
update user_base
set user_name = #{userName,jdbcType=VARCHAR},
pass_word = #{passWord,jdbcType=VARCHAR},
nick_name = #{nickName,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=DATE},
update_time = #{updateTime,jdbcType=DATE}
where uid = #{uid,jdbcType=BIGINT}
</update>
<select id="selectByExampleWithRowbounds" parameterType="com.caochenlei.mpdemo.pojo.SysUserDOExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from user_base
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="queryByPage" resultMap="BaseResultMap">
select * from user_base limit #{pageNo},#{pageSize}
</select>
</mapper>