An open API service indexing awesome lists of open source software.

https://github.com/lanux/hbatis

基于mybatis的crud解决方案
https://github.com/lanux/hbatis

Last synced: about 1 month ago
JSON representation

基于mybatis的crud解决方案

Awesome Lists containing this project

README

          

# hbatis
> 基于mybatis的crud解决方案

## hbatis是什么

> 建立在mybatis之上的 单表通用操作(新增、修改、删除、按字段查、按字段更新等)。减小重复性的、逻辑简单的xml配置,在不侵入mybatis的成本下,以几乎等同的代码量,
完成单表的CRUD操作,可通过代码构建Statement。大大提高代码的可维护性。

## hbatis的由来
>- mybatis generator 生成的代码及xml十分臃肿 及难以维护。
>- mybatis generator 生成的代码功能较弱 且 xml几乎不可复用
***
# 开始之旅
## POM.xml
```xml

org.mybatis.hbatis
hbatis-spring
2.0

```
## 配置文件
### 不使用hbatis
```xml



```
### 使用hbatis
```xml



```
> 如此即开启了hbatis支持,是不是 So easy ?!

## Entity
```Java
@Table(OrgInfo.EntityNode.class)
public class OrgInfo implements Serializable {

private static final long serialVersionUID = 1L;
/**
* ID
* nullable:true,length:11
*/
@Column(primaryKey = true,autoIncrement = true,comment = "ID")
private Integer id;
/**
*
* nullable:false,length:32
*/
@Column(comment = "")
@NotNull
private String code;
/**
*
* nullable:false,length:128
*/
@Column(comment = "")
@NotNull
private String secret;
/**
* 名称
* nullable:false,length:64
*/
@Column(comment = "名称")
@NotNull
private String name;
/**
* 图片
* nullable:true,length:256
*/
@Column(comment = "图片")
private String imgSrc;
/**
* 类型(1:个人,2:机构,3:机关单位,4:企业)
* nullable:false,length:11
*/
@Column(comment = "类型(1:个人,2:机构,3:机关单位,4:企业)")
@NotNull
private Integer type;
/**
* 官网
* nullable:true,length:250
*/
@Column(comment = "官网")
private String website;
/**
* 地址
* nullable:true,length:250
*/
@Column(comment = "地址")
private String address;
/**
* 电话
* nullable:true,length:32
*/
@Column(comment = "电话")
private String tel;
/**
* 邮箱
* nullable:true,length:64
*/
@Column(comment = "邮箱")
private String email;
/**
* 联系人
* nullable:true,length:32
*/
@Column(comment = "联系人")
private String contactor;
/**
* 状态
* nullable:false,length:4
*/
@Column(comment = "状态")
@NotNull
private Byte status;
/**
* 备注
* nullable:true,length:500
*/
@Column(comment = "备注")
private String remark;
/**
*
* nullable:false,length:19
*/
@Column(comment = "")
@NotNull
private Date opOn;
/**
*
* nullable:false,length:11
*/
@Column(comment = "")
@NotNull
private Integer opBy;

... get and set

public static class EntityNode extends AbstractEntityNode {
public static final EntityNode INSTANCE = new EntityNode("toi");;
/** ID */
public FieldNode id = createFieldNode("id","id",Integer.class,JdbcType.INTEGER);
/** */
public FieldNode code = createFieldNode("code","code",String.class,JdbcType.VARCHAR);
/** */
public FieldNode secret = createFieldNode("secret","secret",String.class,JdbcType.VARCHAR);
/** 名称 */
public FieldNode name = createFieldNode("name","name",String.class,JdbcType.VARCHAR);
/** 图片 */
public FieldNode imgSrc = createFieldNode("imgSrc","img_src",String.class,JdbcType.VARCHAR);
/** 类型(1:个人,2:机构,3:机关单位,4:企业) */
public FieldNode type = createFieldNode("type","type",Integer.class,JdbcType.INTEGER);
/** 官网 */
public FieldNode website = createFieldNode("website","website",String.class,JdbcType.VARCHAR);
/** 地址 */
public FieldNode address = createFieldNode("address","address",String.class,JdbcType.VARCHAR);
/** 电话 */
public FieldNode tel = createFieldNode("tel","tel",String.class,JdbcType.VARCHAR);
/** 邮箱 */
public FieldNode email = createFieldNode("email","email",String.class,JdbcType.VARCHAR);
/** 联系人 */
public FieldNode contactor = createFieldNode("contactor","contactor",String.class,JdbcType.VARCHAR);
/** 状态 */
public FieldNode status = createFieldNode("status","status",Byte.class,JdbcType.TINYINT);
/** 备注 */
public FieldNode remark = createFieldNode("remark","remark",String.class,JdbcType.VARCHAR);
/** */
public FieldNode opOn = createFieldNode("opOn","op_on",Date.class,JdbcType.TIMESTAMP);
/** */
public FieldNode opBy = createFieldNode("opBy","op_by",Integer.class,JdbcType.INTEGER);

/**
* @param alias 别名
*/
public EntityNode(String alias) {
super(OrgInfo.class,"t_org_info",alias);
}
}

// ==== 自定义属性 ====
}
```

## DAO
> 继承HbatisMapper
```Java
/**
* OrgInfoMapper
* @author generator
* @date 2017年07月11日
*/
public interface OrgInfoMapper extends HbatisMapper {


//-- 按实体参数查询[START]
List findByQueryParam(@Param("queryParam")OrgInfo.QueryParam queryParam);

long countByQueryParam(@Param("queryParam")OrgInfo.QueryParam queryParam);
//-- 按实体参数查询[END]

//-- 自定义业务方法,请写在下方 -->
}
```
## Service
> 在Service 中调用Mapper接口

```Java
/**
* 删除
*
* @param id
*/
public void deleteById(Integer pk) {
repo.deleteByPK(pk);
}

/**
* 通过id获取
*
* @param id
* @return
*/
public OrgInfo findById(Integer pk) {
return repo.selectByPK(pk);
}

/**
* 通过非空属性查询
*
* @param OrgInfo
* @return
*/
public List findByNotNullProps(OrgInfo entity) {

SelectStatement st = StatementBuilder.buildSelectSelective(entity);
return repo.selectByStatement(st);
}
/**
* 单表通用查询样例
*
* @param OrgInfo
* @return
*/
@Override
public OrgInfo findByCode(String code) {
OrgInfo.EntityNode n = OrgInfo.EntityNode.INSTANCE;
SelectStatement st = StatementBuilder.buildSelect(n);
st.restrictions().add(n.code.eq(code));
return CollectionUtil.uniqueResult(this.repo.selectByStatement(st));
}

```

# 代码生成器
> 角色类似 mybatis-generator

![](1.png)

![](2.png)

## 生成的mapper xml
```xml




































id,code,secret,name,img_src,type,website,address,tel,email,contactor,status,remark,op_on,op_by


toi.id toi_id,toi.code toi_code,toi.secret toi_secret,toi.name toi_name,toi.img_src toi_img_src,toi.type toi_type,toi.website toi_website,toi.address toi_address,toi.tel toi_tel,toi.email toi_email,toi.contactor toi_contactor,toi.status toi_status,toi.remark toi_remark,toi.op_on toi_op_on,toi.op_by toi_op_by










and toi.id=#{p.id}


and toi.code=#{p.code}


and toi.secret=#{p.secret}


and toi.name=#{p.name}


and toi.img_src=#{p.imgSrc}


and toi.type=#{p.type}


and toi.website=#{p.website}


and toi.address=#{p.address}


and toi.tel=#{p.tel}


and toi.email=#{p.email}


and toi.contactor=#{p.contactor}


and toi.status=#{p.status}


and toi.remark=#{p.remark}


and toi.op_on=#{p.opOn}


and toi.op_by=#{p.opBy}






select

from t_org_info toi where 1=1


limit ${queryParam.start},${queryParam.limit}



select count(1) from t_org_info toi where 1=1





```
> 如果只是单表的CRUD操作,xml内容可为空。但为了减少后期人工添加的麻烦,于是就在模块里生成...
> 关于分页:因本人一直使用mysql,未编写其它数据库的分页实现