Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
1、相关文件
关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包
以下是相关文件截图:
和Hibernate逆向生成一样,这里也需要一个配置文件:
2、generatorConfig.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="C:/WorkSpace/longweb/WebContent/WEB-INF/lib/mysql-jdbc-5.1.25.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--时间与注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/lpt_data" userId="root" password="root">
</jdbcConnection>
<!--如果精度>0或者长度>18,就会使用java.math.BigDecimal-->
<!--如果精度=0并且10<=长度<=18,就会使用java.lang.Long-->
<!--如果精度=0并且5<=长度<=9,就会使用java.lang.Integer-->
<!--如果精度=0并且长度<5,就会使用java.lang.Short-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.chw.controller" targetProject="longweb">
<!--enableSubPackages:如果true,MBG会根据catalog和schema来生成子包。如果false就会直接用targetPackage属性。默认为false。 -->
<property name="enableSubPackages" value="true"/>
<!-- trimStrings:是否对数据库查询结果进行trim操作,如果设置为true就会生成类似这样public void setUsername(String username) {this.username = username == null ? null : username.trim();}的setter方法。默认值为false。 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="com.chw.controller" targetProject="longweb">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.chw.controller" targetProject="longweb">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="sys_user" domainObjectName="sysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。
上面配置文件中的:
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。
3、启动
生成相关代码
sysUser.java
package com.chw.controller;
import java.util.Date;
public class sysUser {
private String userid;
private String username;
private String password;
private String organizationid;
private String roleids;
private Integer status;
private String description;
private String mc;
private Date cjsj;
private Date xgsj;
private String yhlx;
private String updateVer;
private String yhemail;
private String phone;
private String mailbox;
private String ifRetailer;
private String ifWholesaler;
private String ifAgent;
private String ifManagement;
private String tgOrgid;
private Double lbs;
private String yhTx;
private Date createTime;
private Date updateTime;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid == null ? null : userid.trim();
}
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 getOrganizationid() {
return organizationid;
}
public void setOrganizationid(String organizationid) {
this.organizationid = organizationid == null ? null : organizationid.trim();
}
public String getRoleids() {
return roleids;
}
public void setRoleids(String roleids) {
this.roleids = roleids == null ? null : roleids.trim();
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
public String getMc() {
return mc;
}
public void setMc(String mc) {
this.mc = mc == null ? null : mc.trim();
}
public Date getCjsj() {
return cjsj;
}
public void setCjsj(Date cjsj) {
this.cjsj = cjsj;
}
public Date getXgsj() {
return xgsj;
}
public void setXgsj(Date xgsj) {
this.xgsj = xgsj;
}
public String getYhlx() {
return yhlx;
}
public void setYhlx(String yhlx) {
this.yhlx = yhlx == null ? null : yhlx.trim();
}
public String getUpdateVer() {
return updateVer;
}
public void setUpdateVer(String updateVer) {
this.updateVer = updateVer == null ? null : updateVer.trim();
}
public String getYhemail() {
return yhemail;
}
public void setYhemail(String yhemail) {
this.yhemail = yhemail == null ? null : yhemail.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getMailbox() {
return mailbox;
}
public void setMailbox(String mailbox) {
this.mailbox = mailbox == null ? null : mailbox.trim();
}
public String getIfRetailer() {
return ifRetailer;
}
public void setIfRetailer(String ifRetailer) {
this.ifRetailer = ifRetailer == null ? null : ifRetailer.trim();
}
public String getIfWholesaler() {
return ifWholesaler;
}
public void setIfWholesaler(String ifWholesaler) {
this.ifWholesaler = ifWholesaler == null ? null : ifWholesaler.trim();
}
public String getIfAgent() {
return ifAgent;
}
public void setIfAgent(String ifAgent) {
this.ifAgent = ifAgent == null ? null : ifAgent.trim();
}
public String getIfManagement() {
return ifManagement;
}
public void setIfManagement(String ifManagement) {
this.ifManagement = ifManagement == null ? null : ifManagement.trim();
}
public String getTgOrgid() {
return tgOrgid;
}
public void setTgOrgid(String tgOrgid) {
this.tgOrgid = tgOrgid == null ? null : tgOrgid.trim();
}
public Double getLbs() {
return lbs;
}
public void setLbs(Double lbs) {
this.lbs = lbs;
}
public String getYhTx() {
return yhTx;
}
public void setYhTx(String yhTx) {
this.yhTx = yhTx == null ? null : yhTx.trim();
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
sysUserMapper.java
package com.chw.controller;
import com.chw.controller.sysUser;
public interface sysUserMapper {
int deleteByPrimaryKey(String userid);
int insert(sysUser record);
int insertSelective(sysUser record);
sysUser selectByPrimaryKey(String userid);
int updateByPrimaryKeySelective(sysUser record);
int updateByPrimaryKey(sysUser record);
}
sysUserMapper.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.chw.controller.sysUserMapper">
<resultMap id="BaseResultMap" type="com.chw.controller.sysUser">
<id column="userid" jdbcType="VARCHAR" property="userid" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="organizationid" jdbcType="VARCHAR" property="organizationid" />
<result column="roleids" jdbcType="VARCHAR" property="roleids" />
<result column="status" jdbcType="INTEGER" property="status" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="MC" jdbcType="VARCHAR" property="mc" />
<result column="CJSJ" jdbcType="TIMESTAMP" property="cjsj" />
<result column="XGSJ" jdbcType="TIMESTAMP" property="xgsj" />
<result column="YHLX" jdbcType="VARCHAR" property="yhlx" />
<result column="UPDATE_VER" jdbcType="VARCHAR" property="updateVer" />
<result column="YHEMAIL" jdbcType="VARCHAR" property="yhemail" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="mailbox" jdbcType="VARCHAR" property="mailbox" />
<result column="if_retailer" jdbcType="VARCHAR" property="ifRetailer" />
<result column="if_wholesaler" jdbcType="VARCHAR" property="ifWholesaler" />
<result column="if_agent" jdbcType="VARCHAR" property="ifAgent" />
<result column="if_management" jdbcType="VARCHAR" property="ifManagement" />
<result column="tg_orgid" jdbcType="VARCHAR" property="tgOrgid" />
<result column="lbs" jdbcType="DOUBLE" property="lbs" />
<result column="yh_tx" jdbcType="VARCHAR" property="yhTx" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
</resultMap>
<sql id="Base_Column_List">
userid, username, password, organizationid, roleids, status, description, MC, CJSJ,
XGSJ, YHLX, UPDATE_VER, YHEMAIL, phone, mailbox, if_retailer, if_wholesaler, if_agent,
if_management, tg_orgid, lbs, yh_tx, create_time, update_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_user
where userid = #{userid,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from sys_user
where userid = #{userid,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.chw.controller.sysUser">
insert into sys_user (userid, username, password,
organizationid, roleids, status,
description, MC, CJSJ,
XGSJ, YHLX, UPDATE_VER,
YHEMAIL, phone, mailbox,
if_retailer, if_wholesaler, if_agent,
if_management, tg_orgid, lbs,
yh_tx, create_time, update_time
)
values (#{userid,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{organizationid,jdbcType=VARCHAR}, #{roleids,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
#{description,jdbcType=VARCHAR}, #{mc,jdbcType=VARCHAR}, #{cjsj,jdbcType=TIMESTAMP},
#{xgsj,jdbcType=TIMESTAMP}, #{yhlx,jdbcType=VARCHAR}, #{updateVer,jdbcType=VARCHAR},
#{yhemail,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{mailbox,jdbcType=VARCHAR},
#{ifRetailer,jdbcType=VARCHAR}, #{ifWholesaler,jdbcType=VARCHAR}, #{ifAgent,jdbcType=VARCHAR},
#{ifManagement,jdbcType=VARCHAR}, #{tgOrgid,jdbcType=VARCHAR}, #{lbs,jdbcType=DOUBLE},
#{yhTx,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
)
</insert>
<insert id="insertSelective" parameterType="com.chw.controller.sysUser">
insert into sys_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userid != null">
userid,
</if>
<if test="username != null">
username,
</if>
<if test="password != null">
password,
</if>
<if test="organizationid != null">
organizationid,
</if>
<if test="roleids != null">
roleids,
</if>
<if test="status != null">
status,
</if>
<if test="description != null">
description,
</if>
<if test="mc != null">
MC,
</if>
<if test="cjsj != null">
CJSJ,
</if>
<if test="xgsj != null">
XGSJ,
</if>
<if test="yhlx != null">
YHLX,
</if>
<if test="updateVer != null">
UPDATE_VER,
</if>
<if test="yhemail != null">
YHEMAIL,
</if>
<if test="phone != null">
phone,
</if>
<if test="mailbox != null">
mailbox,
</if>
<if test="ifRetailer != null">
if_retailer,
</if>
<if test="ifWholesaler != null">
if_wholesaler,
</if>
<if test="ifAgent != null">
if_agent,
</if>
<if test="ifManagement != null">
if_management,
</if>
<if test="tgOrgid != null">
tg_orgid,
</if>
<if test="lbs != null">
lbs,
</if>
<if test="yhTx != null">
yh_tx,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userid != null">
#{userid,jdbcType=VARCHAR},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="organizationid != null">
#{organizationid,jdbcType=VARCHAR},
</if>
<if test="roleids != null">
#{roleids,jdbcType=VARCHAR},
</if>
<if test="status != null">
#{status,jdbcType=INTEGER},
</if>
<if test="description != null">
#{description,jdbcType=VARCHAR},
</if>
<if test="mc != null">
#{mc,jdbcType=VARCHAR},
</if>
<if test="cjsj != null">
#{cjsj,jdbcType=TIMESTAMP},
</if>
<if test="xgsj != null">
#{xgsj,jdbcType=TIMESTAMP},
</if>
<if test="yhlx != null">
#{yhlx,jdbcType=VARCHAR},
</if>
<if test="updateVer != null">
#{updateVer,jdbcType=VARCHAR},
</if>
<if test="yhemail != null">
#{yhemail,jdbcType=VARCHAR},
</if>
<if test="phone != null">
#{phone,jdbcType=VARCHAR},
</if>
<if test="mailbox != null">
#{mailbox,jdbcType=VARCHAR},
</if>
<if test="ifRetailer != null">
#{ifRetailer,jdbcType=VARCHAR},
</if>
<if test="ifWholesaler != null">
#{ifWholesaler,jdbcType=VARCHAR},
</if>
<if test="ifAgent != null">
#{ifAgent,jdbcType=VARCHAR},
</if>
<if test="ifManagement != null">
#{ifManagement,jdbcType=VARCHAR},
</if>
<if test="tgOrgid != null">
#{tgOrgid,jdbcType=VARCHAR},
</if>
<if test="lbs != null">
#{lbs,jdbcType=DOUBLE},
</if>
<if test="yhTx != null">
#{yhTx,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.chw.controller.sysUser">
update sys_user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="organizationid != null">
organizationid = #{organizationid,jdbcType=VARCHAR},
</if>
<if test="roleids != null">
roleids = #{roleids,jdbcType=VARCHAR},
</if>
<if test="status != null">
status = #{status,jdbcType=INTEGER},
</if>
<if test="description != null">
description = #{description,jdbcType=VARCHAR},
</if>
<if test="mc != null">
MC = #{mc,jdbcType=VARCHAR},
</if>
<if test="cjsj != null">
CJSJ = #{cjsj,jdbcType=TIMESTAMP},
</if>
<if test="xgsj != null">
XGSJ = #{xgsj,jdbcType=TIMESTAMP},
</if>
<if test="yhlx != null">
YHLX = #{yhlx,jdbcType=VARCHAR},
</if>
<if test="updateVer != null">
UPDATE_VER = #{updateVer,jdbcType=VARCHAR},
</if>
<if test="yhemail != null">
YHEMAIL = #{yhemail,jdbcType=VARCHAR},
</if>
<if test="phone != null">
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="mailbox != null">
mailbox = #{mailbox,jdbcType=VARCHAR},
</if>
<if test="ifRetailer != null">
if_retailer = #{ifRetailer,jdbcType=VARCHAR},
</if>
<if test="ifWholesaler != null">
if_wholesaler = #{ifWholesaler,jdbcType=VARCHAR},
</if>
<if test="ifAgent != null">
if_agent = #{ifAgent,jdbcType=VARCHAR},
</if>
<if test="ifManagement != null">
if_management = #{ifManagement,jdbcType=VARCHAR},
</if>
<if test="tgOrgid != null">
tg_orgid = #{tgOrgid,jdbcType=VARCHAR},
</if>
<if test="lbs != null">
lbs = #{lbs,jdbcType=DOUBLE},
</if>
<if test="yhTx != null">
yh_tx = #{yhTx,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set>
where userid = #{userid,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.chw.controller.sysUser">
update sys_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
organizationid = #{organizationid,jdbcType=VARCHAR},
roleids = #{roleids,jdbcType=VARCHAR},
status = #{status,jdbcType=INTEGER},
description = #{description,jdbcType=VARCHAR},
MC = #{mc,jdbcType=VARCHAR},
CJSJ = #{cjsj,jdbcType=TIMESTAMP},
XGSJ = #{xgsj,jdbcType=TIMESTAMP},
YHLX = #{yhlx,jdbcType=VARCHAR},
UPDATE_VER = #{updateVer,jdbcType=VARCHAR},
YHEMAIL = #{yhemail,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
mailbox = #{mailbox,jdbcType=VARCHAR},
if_retailer = #{ifRetailer,jdbcType=VARCHAR},
if_wholesaler = #{ifWholesaler,jdbcType=VARCHAR},
if_agent = #{ifAgent,jdbcType=VARCHAR},
if_management = #{ifManagement,jdbcType=VARCHAR},
tg_orgid = #{tgOrgid,jdbcType=VARCHAR},
lbs = #{lbs,jdbcType=DOUBLE},
yh_tx = #{yhTx,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where userid = #{userid,jdbcType=VARCHAR}
</update>
</mapper>