Mybatis自动生成Example类优化(抽象)

Mybatis自动生成Example类优化(抽象) 

背景:

example,domain xml庞大,难以增量维护

查询条件map传递,上层调用难以透明

多表查询,查询条件复杂,domain  xml庞大

 

设计思路:

mybatis是半自动化的orm框架,过度封装不免会丧失性能、易用性和自由度;所以框架定位并不是做一个含有各种and \or \distinct、group by..等通用的查询解决方案。只是将sql抽象为各个片断,在基类接口里提供比较常用的增、删、改、统计方法。对于复杂查询,在各个业务接口(Mapper)类方法形参中定义各种片段;至于查询结果映射保持原有mybatis方式。

 

 

自动或手工生成相关domain 如:

public class App {

.....

public static class MetaPath extends EntityPathSupport<App> {
       public FieldPath<Integer, App> id = createFieldPath(Integer.class,"id",JdbcType.INTEGER);;

       public FieldPath<String, App> appName = createFieldPath(String.class,"app_name",JdbcType.VARCHAR);;

       public FieldPath<Byte, App> platformType = createFieldPath(Byte.class,"platform_type",JdbcType.TINYINT);;

       public FieldPath<Byte, App> status = createFieldPath(Byte.class,"status",JdbcType.TINYINT);;

       public FieldPath<String, App> description = createFieldPath(String.class,"description",JdbcType.VARCHAR);;

       public FieldPath<Date, App> createOn = createFieldPath(Date.class,"create_on",JdbcType.TIMESTAMP);;

       public FieldPath<Integer, App> createBy = createFieldPath(Integer.class,"create_by",JdbcType.INTEGER);;

       public FieldPath<Date, App> opOn = createFieldPath(Date.class,"op_on",JdbcType.TIMESTAMP);;

       public FieldPath<Integer, App> opBy = createFieldPath(Integer.class,"op_by",JdbcType.INTEGER);;

       public MetaPath() {
           super("t_app");
       }
   }

}

 

使用方法:

1)查询

App.MetaPath mp = new App.MetaPath();

SelectStatement<App> st = SelectStatement.newInstance(mp);

st.getRestrictions().add(mp.appName.like(“%2%”)).add(mp.id.isNotNull());

st.setPagination(0, 1);

List<App> list = appMapper.selectByStatement(st);

System.out.println(list.size());

 

2)修改:

App app = new App();

app.setAppName(“sdfsdfdsfds”);

App.MetaPath ap = new App.MetaPath();

UpdateStatement<App> st = UpdateStatement.newInstance(ap); st.setFieldMetadatas(ap.metaDatas(app));

st.getRestrictions().add(ap.id.eq(app.getId()));

appMapper.updateByStatement(st);

3)删除:

...

 

此帖只是抛砖引玉(由于jar包中涉及公司信息,暂就到这吧)

 

 

基类如下 :

public interface VpmMapper<T,PK> {
	
	/**
	 * 新增
	 * @param entity
	 * @return
	 */
	int insert(T entity);
	
	/**
	 * 修改
	 * @param entity
	 * @return
	 */
	int update(T entity);
	/**
	 * 查询
	 * @param pk
	 * @return
	 */
	T selectByPK(PK pk);
	/**
	 * 删除
	 * @param entity
	 * @return
	 */
	int deleteByPK(PK pk);
	/**
	 * 
	 * @param statement
	 * @return
	 */
	int insertByStatement(@Param("st")InsertStatement<T> statement);
	
	int updateByStatement(@Param("st")UpdateStatement<T> statement);
	
	int deleteByRestriction(@Param("restrictions")Restrictions<T> restrictions);
	
	int countByRestriction(@Param("restrictions")Restrictions<T> restrictions);
	
	
}

 

猜你喜欢

转载自ahzzhen.iteye.com/blog/2119752