paoding-rose 框架BaseDAO

版本:

<dependency>
    <groupId>net.paoding</groupId>
    <artifactId>paoding-rose-jade</artifactId>
    <version>2.0.u01</version>
</dependency>
<dependency>
    <groupId>net.paoding</groupId>
    <artifactId>paoding-rose-web</artifactId>
    <version>2.0.u01</version>
</dependency>


用的paoding-rose 久了,感觉DAO层书写还是挺方便的.

但是在其他非rose项目中各种baseDAO 用起来挺舒服的。

每每想到rose中update语句,因为修改字段或条件不同就要创建一个方法,就感觉疲惫...

下面是我写的通用DAO,希望对用rose的人提供方便,大家如果有好的建议也可以分享下..


其中:

$table:为子类中的表名

$field:为子类中的查询字段


在baseDAO 中没有save 需要自己在子类中自己实现


baseDAO<T>:

import java.util.List;
import java.util.Map;

import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;

/**
 * 公共dao
 * @author Administrator
 *
 *	实现一些共有方法
 */
public interface CommonDAO<T> {

	@SQL("select $field from $table where id=:id")
	T byId(@SQLParam("id")long id);
	
	/**
	 * 修改 
	 * 为了形成sql 方便 sql语句的实现都加了  update table set id=id...  ,"id=id"表明每个表都要有id字段。
	 * @param modField  需要修改的字段 key为字段名,value为要改变的值 字段不能为null
	 * @param condField 条件字段 key为字段名,value为要改变的值,该字段不能为null
	 * @param cIsEmpty  默认写:false  为了防止condField为空 造成修改全表。在condField为true时可以修改全表
	 * @return
	 */
	@SQL("update $table set id=id "
			+ "#for(key in :m.keySet()){ ,##(:key) = #(:m[:key]) }"
			+ "#if(:c==null || :c.keySet().size()==0){"
			+ " #if(:cIsEmpty){}#else{ where 1=2}"
			+ "}#else{"
			+ "	where 1=1  #for(key in :c.keySet()){ and ##(:key) = #(:c[:key]) }"
			+ "}"
			)
	int update(@SQLParam("m")Map<String, Object> modField,
	@SQLParam("c")Map<String, Object> condField,@SQLParam("cIsEmpty")boolean cIsEmpty);
	
	
	
	
	/**
	 * 分页
	 * 
	 * @param condField 条件字段
	 * @param start  开始数
	 * @param size   每页个数
	 * @param orderField  排序的字段 
	 * @param direction  正序(asc),倒序(desc)
	 * @return
	 */
	@SQL("select $field from $table "
			+ "#if(:c!=null || :c.keySet().size()>0){"
			+ "	where 1=1  #for(key in :c.keySet()){ and ##(:key) = #(:c[:key]) }"
			+ "}"
			+ " order by ##(:orderField) ##(:direction)"
			+ " limit :start,:size ")
	List<T> FindPage(@SQLParam("c")Map<String, Object> condField,@SQLParam("start")long start
			,@SQLParam("size")int size,@SQLParam("orderField")String orderField,
			@SQLParam("direction")String direction);
	
	/**
	 * 条件查询 总数量
	 * @return
	 */
	@SQL("select count(*) from $table "
			+ "#if(:c!=null || :c.keySet().size()>0){"
			+ "	where 1=1  #for(key in :c.keySet()){ and ##(:key) = #(:c[:key]) }"
			+ "}")
	int FindCount(@SQLParam("c")Map<String, Object> condField);
	
	
	/**
	 * 根据条件查找一个
	 * @param condField
	 * @return    条件为空 返回空
	 */
	@SQL("select $field from $table "
			+ "#if(:c!=null || :c.keySet().size()>0){"
			+ "	where 1=1  #for(key in :c.keySet()){ and ##(:key) = #(:c[:key]) }"
			+ "}#else{ where 1=2}")
	T findOne(@SQLParam("c")Map<String, Object> condField);
	
	/**
	 * 根据条件查找
	 * @param condField
	 * @return   条件为空 返回空
	 */
	@SQL("select $field from $table "
			+ "#if(:c!=null || :c.keySet().size()>0){"
			+ "	where 1=1  #for(key in :c.keySet()){ and ##(:key) = #(:c[:key]) }"
			+ "}#else{ where 1=2}")
	List<T> find(@SQLParam("c")Map<String, Object> condField);
	
}


子类:


@DAO
public interface ProductDAO extends CommonDAO<Product> {

	
	String table="t_product";
	
	String field="id,name";
	
	@SQL("insert into $table(name)"
			+ " values(:p.name)")
	int save(@SQLParam("p")Product p);
	
	
}


调用:

@Service
public class ProductService {

	@Autowired
	ProductDAO productDAO;
	
	
	public Product detail(int id){
		return productDAO.byId(id);
	}
	
	
}


猜你喜欢

转载自blog.csdn.net/zjh1n795/article/details/51249239