玩了几天mybatis终于有了个大概的模型,尽管还不是最满意,但也是可以实现CRUD的全部功能。
感谢以下帖子提供的思路。MyBatis3 通用CRUD的研究[一]MyBatis3 通用CRUD的研究[二]还是有很多学习和借鉴,不过缺点就是不能通用查询功能。很是遗憾。最重要的部分没有,可惜楼主也没有给什么解释。
究其原因。是因为能用的BaseMapper接口并没有实现类,它只负责收集一些配置信息(可以理解为把配置从xml转到了java注解里),最终的实现在第三方类里面。第三方不能识别到范型具体的类(也许就是所谓有类型丢失问题),结果返回的结果集的时候会因为找不到具体的类就会出现转型问题。所以必须指明返回值的实体类型。
根据这个解决方案就是,在子接口中指定查询,然后在service层中也做一个通用的父类。就可以实现通用的crud功能。
public interface BaseMapper<T> {
public abstract T get(T obj);
public abstract List<T> findBy(T obj, List<SearchFilter> searchFilterList, int pageNumber, int limit);
@SelectProvider(type = CRUDTemplate.class,method = "count")
public int count(Object obj, List<SearchFilter> searchFilterList);
/**
* Insert语句从CUDTemplate类中生成
* @param obj
*/
@InsertProvider(type = CRUDTemplate.class, method = "insert")
@Options(keyColumn="id", keyProperty="id", useGeneratedKeys=false)
// @SelectKey(before=true, resultType=Long.class, keyProperty="id", statement={" select acct_seq.nextval from dual "})
public void insert(Object obj);
/**
* Update语句从CUDTemplate类中生成
* @param obj
*/
@UpdateProvider(type = CRUDTemplate.class, method = "update")
public void update(Object obj);
/**
* Delete语句从CUDTemplate类中生成
* @param obj
*/
@DeleteProvider(type = CRUDTemplate.class, method = "delete")
public void delete(Object obj);
public Long getKey();
}
/**
* 只需要继承BaseMapper,就可以直接实现根据主键的CRUD操作
* 自定义的sql就直接写到UserMapper.xml中
* @author amu
*
*/
public interface UserMapper extends BaseMapper<User> {
@SelectProvider(type = CRUDTemplate.class, method = "get")
public Department getDepartmentDetail(Department d);
@SelectProvider(type = CRUDTemplate.class, method = "get")
@Override
public User get(User obj);
@SelectProvider(type = CRUDTemplate.class, method = "findBy")
@Override
public List<User> findBy(User obj, List<SearchFilter> searchFilterList, int pageNumber, int limit);
}
public abstract class BaseManager<T extends IdEntity> {
private static final int USE_DEFAULT_PAGE_SIZE = -1;
@Transactional(readOnly = false)
public void delete(Long id) {
T t = this.getEntity();
t.setId(id);
getBaseMapper().delete(t);
}
@Transactional(readOnly = false)
public void save(T t) {
getBaseMapper().insert(t);
}
@Transactional(readOnly = false)
public void update(T t) {
getBaseMapper().update(t);
}
@Transactional(readOnly = true)
public List<T> findBy(List<SearchFilter> searchFilterList, int pageNumber, int limit) {
return getBaseMapper().findBy(getEntity(), searchFilterList, pageNumber, limit);
}
@Transactional(readOnly = true)
public List<T> findBy(List<SearchFilter> searchFilterList) {
return getBaseMapper().findBy(getEntity(), searchFilterList, 0, 0);
}
@Transactional(readOnly = true)
public T get(Long id) {
T t = getEntity();
t.setId(id);
return getBaseMapper().get(t);
}
public Page<T> findPage(List<SearchFilter> searchFilterList, int pageNumber, int limit) {
int totalCount = this.getBaseMapper().count(getEntity(), searchFilterList);
Page<T> page = new Page<T>();
if (USE_DEFAULT_PAGE_SIZE == limit) {
limit = page.getPageSize();
}
List<T> result = findBy(searchFilterList, pageNumber, limit);
page.setPageNo(pageNumber);
page.setTotalCount(totalCount);
page.setResult(result);
return page;
}
public Page<T> findPage(List<SearchFilter> searchFilterList, int pageNumber){
return findPage(searchFilterList, pageNumber, USE_DEFAULT_PAGE_SIZE);
}
public abstract T getEntity();
public abstract BaseMapper<T> getBaseMapper();
}
ps:自已也不是很满意,DAO层子接口的实现还是有点麻烦。
因为比较懒,基于mysql数据库。