Mybatis官方提供了逆向生成的工程的工厂类,这个可以针对mybatis执行所需要的对应数据库中单表的pojo类,以及pojo类和SQL以及其对应的Mapper文件,都可以逆向生成,同时,这个有较多的生成方式:maven插件,基于xml配置的和基于java Configuration类 的java program。
一般情况下我们使用第4种生成方式,通过一个xml配置文件和java program类生成所需要的上述文件。首先我们可以先建立一个空的Java工程,在java工程里面添加引用对应的jar包,构建好工程之后需要配置generatorConfig.xml文件。这些步骤在前面的文章中提到过了,具体参考此链接。
这次需要分析一下生成的文件和运行的简要原理:
我们了解到,这个工程应对与单表查询逆向生成比较方便,但是对于多表查询和复杂的sql语句等支持不好,复杂的sql语句还需要开发者自己编写配置,一般的对于单表查询起会生成以下这几种文件:
1.POJO类行的java文件:
这个是对应于数据库中指定表中的字段生成的,一般情况下会根据表名字来命名文件名如下面的文件是查询newstyles数据库中的Tb_Item表格,则包含的字段都会被写入到TbItempojo类中,生成文件(文件路径在相关连接的文档中已经给出)如下:
package com.newstyles.pojo;
import java.util.Date;
public class TbItem {
private Long id;
private String title;
private String sellPoint;
private Long price;
private Integer num;
private String barcode;
private String image;
private Long cid;
private Byte status;
private Date created;
private Date updated;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}
public String getSellPoint() {
return sellPoint;
}
public void setSellPoint(String sellPoint) {
this.sellPoint = sellPoint == null ? null : sellPoint.trim();
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode == null ? null : barcode.trim();
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image == null ? null : image.trim();
}
public Long getCid() {
return cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public Byte getStatus() {
return status;
}
public void setStatus(Byte status) {
this.status = status;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
}
2. TbItemMapper.java
package com.newstyles.mapper;
import com.newstyles.pojo.TbItem;
import com.newstyles.pojo.TbItemExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface TbItemMapper {
int countByExample(TbItemExample example);
int deleteByExample(TbItemExample example);
int deleteByPrimaryKey(Long id);
int insert(TbItem record);
int insertSelective(TbItem record);
List selectByExample(TbItemExample example);
TbItem selectByPrimaryKey(Long id);
int updateByExampleSelective(@Param("record") TbItem record, @Param("example") TbItemExample example);
int updateByExample(@Param("record") TbItem record, @Param("example") TbItemExample example);
int updateByPrimaryKeySelective(TbItem record);
int updateByPrimaryKey(TbItem record);
}
你会发现其声明的类型为接口,这正符合的mybatis工程运行的规则,首先,mybatis除了Config.xml文件外,还需要对应数据库的pojo类和相对应的关于数据库操作的Mapper接口类,我们知道,这个接口不需要我们其实现,但是,需要我们给予一个相对应的****mapper.xml文件进行相关的配置,主要包括确定对应接口中方法的配置,数据类型和返回数据类型的配置,以及返回结果的操作,sql查询语句的编写。对此我们需要了解一下,这个接口中,定义的接口中的各个方法的含义:
方法 | 功能概要 |
---|---|
int countByExample(UserExample example) thorws SQLException | 按条件计数 |
int deleteByPrimaryKey(Integer id) thorws SQLException | 按主键删除 |
int deleteByExample(UserExample example) thorws SQLException | 按条件查询 |
String/Integer insert(User record) thorws SQLException | 插入数据(返回值为ID) |
User selectByPrimaryKey(Integer id) thorws SQLException | 按主键查询 |
ListselectByExample(UserExample example) thorws SQLException | 按条件查询 |
ListselectByExampleWithBLOGs(UserExample example) thorws SQLException | 按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。 |
int updateByPrimaryKey(User record) thorws SQLException | 按主键更新 |
int updateByPrimaryKeySelective(User record) thorws SQLException | 按主键更新值不为null的字段 |
int updateByExample(User record, UserExample example) thorws SQLException | 按条件更新 |
int updateByExampleSelective(User record, UserExample example) thorws SQLException | 按条件更新值不为null的字段 |
同时观察到,我们里面有很多的***Example类型的实例化对象,mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分 例如,select id,note from tb_xxx where id = 2
xxxExample example = new xxxExample();
Criteria criteria = new Example().createCriteria();//这个为具体的条件规则的类
方法 | 说明 |
---|---|
example.setOrderByClause(“字段名 ASC”); | 添加升序排列条件,DESC为降序 |
example.setDistinct(false) | 去除重复,boolean型,true为选择不重复的记录。 |
criteria.andXxxIsNull | 添加字段xxx为null的条件 |
criteria.andXxxIsNotNull | 添加字段xxx不为null的条件 |
criteria.andXxxEqualTo(value) | 添加xxx字段等于value条件 |
criteria.andXxxNotEqualTo(value) | 添加xxx字段不等于value条件 |
criteria.andXxxGreaterThan(value) | 添加xxx字段大于value条件 |
criteria.andXxxGreaterThanOrEqualTo(value) | 添加xxx字段大于等于value条件 |
criteria.andXxxLessThan(value) | 添加xxx字段小于value条件 |
criteria.andXxxLessThanOrEqualTo(value) | 添加xxx字段小于等于value条件 |
criteria.andXxxIn(List<?>) | 添加xxx字段值在List<?>条件 |
criteria.andXxxNotIn(List<?>) | 添加xxx字段值不在List<?>条件 |
criteria.andXxxLike(“%”+value+”%”) | 添加xxx字段值为value的模糊查询条件 |
criteria.andXxxNotLike(“%”+value+”%”) | 添加xxx字段值不为value的模糊查询条件 |
criteria.andXxxBetween(value1,value2) | 添加xxx字段值在value1和value2之间条件 |
criteria.andXxxNotBetween(value1,value2) | 添加xxx字段值不在value1和value2之间条件 |
3. ****Mapper.xml
这个是一个跟随***Mapper.java一起生成的文件,具体的生成代码如下:
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
id, title, sell_point, price, num, barcode, image, cid, status, created, updated
delete from tb_item
where id = #{id,jdbcType=BIGINT}
delete from tb_item
insert into tb_item (id, title, sell_point,
price, num, barcode,
image, cid, status,
created, updated)
values (#{id,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{sellPoint,jdbcType=VARCHAR},
#{price,jdbcType=BIGINT}, #{num,jdbcType=INTEGER}, #{barcode,jdbcType=VARCHAR},
#{image,jdbcType=VARCHAR}, #{cid,jdbcType=BIGINT}, #{status,jdbcType=TINYINT},
#{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
insert into tb_item
id,
title,
sell_point,
price,
num,
barcode,
image,
cid,
status,
created,
updated,
#{id,jdbcType=BIGINT},
#{title,jdbcType=VARCHAR},
#{sellPoint,jdbcType=VARCHAR},
#{price,jdbcType=BIGINT},
#{num,jdbcType=INTEGER},
#{barcode,jdbcType=VARCHAR},
#{image,jdbcType=VARCHAR},
#{cid,jdbcType=BIGINT},
#{status,jdbcType=TINYINT},
#{created,jdbcType=TIMESTAMP},
#{updated,jdbcType=TIMESTAMP},
update tb_item
id = #{record.id,jdbcType=BIGINT},
title = #{record.title,jdbcType=VARCHAR},
sell_point = #{record.sellPoint,jdbcType=VARCHAR},
price = #{record.price,jdbcType=BIGINT},
num = #{record.num,jdbcType=INTEGER},
barcode = #{record.barcode,jdbcType=VARCHAR},
image = #{record.image,jdbcType=VARCHAR},
cid = #{record.cid,jdbcType=BIGINT},
status = #{record.status,jdbcType=TINYINT},
created = #{record.created,jdbcType=TIMESTAMP},
updated = #{record.updated,jdbcType=TIMESTAMP},
update tb_item
set id = #{record.id,jdbcType=BIGINT},
title = #{record.title,jdbcType=VARCHAR},
sell_point = #{record.sellPoint,jdbcType=VARCHAR},
price = #{record.price,jdbcType=BIGINT},
num = #{record.num,jdbcType=INTEGER},
barcode = #{record.barcode,jdbcType=VARCHAR},
image = #{record.image,jdbcType=VARCHAR},
cid = #{record.cid,jdbcType=BIGINT},
status = #{record.status,jdbcType=TINYINT},
created = #{record.created,jdbcType=TIMESTAMP},
updated = #{record.updated,jdbcType=TIMESTAMP}
update tb_item
title = #{title,jdbcType=VARCHAR},
sell_point = #{sellPoint,jdbcType=VARCHAR},
price = #{price,jdbcType=BIGINT},
num = #{num,jdbcType=INTEGER},
barcode = #{barcode,jdbcType=VARCHAR},
image = #{image,jdbcType=VARCHAR},
cid = #{cid,jdbcType=BIGINT},
status = #{status,jdbcType=TINYINT},
created = #{created,jdbcType=TIMESTAMP},
updated = #{updated,jdbcType=TIMESTAMP},
where id = #{id,jdbcType=BIGINT}
update tb_item
set title = #{title,jdbcType=VARCHAR},
sell_point = #{sellPoint,jdbcType=VARCHAR},
price = #{price,jdbcType=BIGINT},
num = #{num,jdbcType=INTEGER},
barcode = #{barcode,jdbcType=VARCHAR},
image = #{image,jdbcType=VARCHAR},
cid = #{cid,jdbcType=BIGINT},
status = #{status,jdbcType=TINYINT},
created = #{created,jdbcType=TIMESTAMP},
updated = #{updated,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
对应的mybatis逆向工程的源码请参考github分享内容generatorSqlmapCustom