Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
1、相关文件
关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包
以下是相关文件截图:
和Hibernate逆向生成一样,这里也需要一个配置文件:
2、generatorConfig.xml
需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。
上面配置文件中的:
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
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 delete from sys_user where userid = #{userid,jdbcType=VARCHAR} 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 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, #{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}, update sys_user where userid = #{userid,jdbcType=VARCHAR} 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}, 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}