service和serviceImpl抽取
2016-05-13 12:15
337 查看
BaseService.java
package cn.buaa.scm.service; import cn.buaa.scm.entity.Page; public interface BaseService<T> { // 插入数据 public int insert(T entity) throws Exception; // 修改单个对象 public int update(T entity) throws Exception; // 删除单个对象 public int delete(T entity) throws Exception; // 通过主键(数组)批量删除数据 public int deleteList(String[] pks) throws Exception; // 查询单个对象 public T select(T entity) throws Exception; // 通过关键字分页查询 public Page<T> selectPage(Page<T> page); // 多条件分页查询 public Page<T> selectPageUseDyc(Page<T> page); }BaseServiceimpl
package cn.buaa.scm.service.impl; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import cn.buaa.scm.dao.AccountMapper; import cn.buaa.scm.dao.BaseMapper; import cn.buaa.scm.dao.SupplierMapper; import cn.buaa.scm.entity.Page; import cn.buaa.scm.service.BaseService; public class BaseServiceImpl<T> implements BaseService<T> { protected BaseMapper<T> baseMapper; @Autowired protected SupplierMapper supplierMapper; @Autowired protected AccountMapper accountMapper; @PostConstruct // 在构造方法后,初化前执行 private void initBaseMapper() throws Exception { // 完成以下逻辑,需要对研发本身进行命名与使用规范 // this关键字指对象本身,这里指的是调用此方法的实现类(子类) System.out.println("=======this :" + this); System.out.println("=======父类基本信息:" + this.getClass().getSuperclass()); System.out.println("=======父类和泛型的信息:" + this.getClass().getGenericSuperclass()); ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); // 获取第一个参数的class Class clazz = (Class) type.getActualTypeArguments()[0]; System.out.println("=======class:" + clazz); // 转化为属性名(相关的Mapper子类的引用名)Supplier supplierMapper String localField = clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) + "Mapper"; System.out.println("=======localField:" + localField); // getDeclaredField:可以使用于包括私有、默认、受保护、公共字段,但不包括继承的字段 Field field = this.getClass().getSuperclass().getDeclaredField(localField); System.out.println("=======field:" + field); System.out.println("=======field对应的对象:" + field.get(this)); Field baseField = this.getClass().getSuperclass().getDeclaredField("baseMapper"); System.out.println("=======baseField:" + baseField); System.out.println("=======baseField对应的对象:" + baseField.get(this)); // field.get(this)获取当前this的field字段的值。例如:Supplier对象中,baseMapper所指向的对象为其子类型SupplierMapper对象,子类型对象已被spring实例化于容器中 baseField.set(this, field.get(this)); System.out.println("========baseField对应的对象:" + baseMapper); } @Override public int insert(T entity) throws Exception { // TODO Auto-generated method stub return baseMapper.insert(entity); } @Override public int update(T entity) throws Exception { // TODO Auto-generated method stub return baseMapper.update(entity); } @Override public int delete(T entity) throws Exception { // TODO Auto-generated method stub return baseMapper.delete(entity); } @Override public int deleteList(String[] pks) throws Exception { // TODO Auto-generated method stub return baseMapper.deleteList(pks); } @Override public T select(T entity) { // TODO Auto-generated method stub return baseMapper.select(entity); } @Override public Page<T> selectPage(Page<T> page) { page.setList(baseMapper.selectPageList(page)); page.setTotalRecord(baseMapper.selectPageCount(page)); return page; } @Override public Page<T> selectPageUseDyc(Page<T> page) { // TODO Auto-generated method stub page.setList(baseMapper.selectPageListUseDyc(page)); page.setTotalRecord(baseMapper.selectPageCountUseDyc(page)); return page; } }
相关文章推荐
- Android Fragment release 有时黑屏解决办法
- 大数据第二天作业
- Visual Studio 换皮肤
- 最全面的前端开发指南
- 我遇见的spring异常及其解决方案(持续更新)
- 2014年第五届蓝桥杯C/C++程序设计本科B组决赛 年龄巧合(结果填空)
- Ubuntu opensips安装流程
- jquery ui widget
- 【LeetCode051】N皇后问题,回溯法,加上一个栈维护结果
- ABAP开发常用的表
- 算法——贝叶斯
- 一个单排程序猿的成长阶段 与 各阶段的花式十八坑
- 简述Linux IO调度的算法
- 简述Linux IO调度的算法
- python numpy的部分函数
- Java中集合Set的用法
- 手机适配
- JSP的9大内置对象和4大域对象
- VirtualBox
- 获取当前日期和星期 iOS