使用mybatis完成通用dao和通用service


使用mybatis完成通用dao和通用service

概述:

使用通用dao和通用service可以減少代碼的開發。可以將常用的增刪改查放到通用dao中。對不同的or框架,基本上都有自己的實現如SpringJPA的Repository就提供了常用的增刪改查方法。而MyBatis借助代碼生成工具也可以生成常用方法的映射

這里只針對Mybatis。如果使用代碼生成工具,會有一個問題:每個Mapper里面都有一些方法(增曬改查)。維護起來的話還好。只是在寫service的時候會有一個問題。比如UserMapper里面有 insert(User user) , find(Integer id) ,delete(Integer id) 等方法,則在service中也要有這些方法的實現。假設每個Mapper有5個方法。則service也需要有5個方法的實現。如果有10個實體類。mapper可以省略(由生成工具生成),但是service有50個方法。到后期肯定不好進行維護

使用通用Mapper和Service

該通用Mapper使用了Spring-mybatis。所以沒有實現類,而是直接調用xml文件中的同名方法。之所以將通用Mapper抽出來主要是為了方便些通用的service

具體代碼

  1. 通用接口,該接口方法的名稱與使用代碼生成工具的名稱完全相同
package cn.liuyiyou.yishop.mapper; import java.io.Serializable; public interface BaseMapper<T,ID extends Serializable> {  int deleteByPrimaryKey(ID id);  int insert(T record);  int insertSelective(T record);  T selectByPrimaryKey(ID id);  int updateByPrimaryKeySelective(T record);  int updateByPrimaryKeyWithBLOBs(T record);  int updateByPrimaryKey(T record); }
  1. 通用service接口。為了方便,名字和通用Mapper同名,其實更傾向於命名add。edit,find這類的命名,顯得更加面向對象
package cn.liuyiyou.yishop.service; import java.io.Serializable; public interface BaseService<T,ID extends Serializable> {  void setBaseMapper();  int deleteByPrimaryKey(ID id);  int insert(T record);  int insertSelective(T record);  T selectByPrimaryKey(ID id);  int updateByPrimaryKeySelective(T record);  int updateByPrimaryKeyWithBLOBs(T record);  int updateByPrimaryKey(T record); }
  1. 通用service實現。也很簡單。就是調用通用mapper里面的方法即可
package cn.liuyiyou.yishop.service.impl; import java.io.Serializable; import cn.liuyiyou.yishop.mapper.BaseMapper; import cn.liuyiyou.yishop.service.BaseService; public abstract class AbstractService<T, ID extends Serializable> implements BaseService<T, ID> {  private BaseMapper<T, ID> baseMapper;  public void setBaseMapper(BaseMapper<T, ID> baseMapper) {   this.baseMapper = baseMapper;  }  @Override  public int deleteByPrimaryKey(ID id) {   return baseMapper.deleteByPrimaryKey(id);  }  @Override  public int insertSelective(T record) {   return baseMapper.insertSelective(record);  }  @Override  public T selectByPrimaryKey(ID id) {   return baseMapper.selectByPrimaryKey(id);  }  @Override  public int updateByPrimaryKeySelective(T record) {   return baseMapper.updateByPrimaryKey(record);  }  @Override  public int updateByPrimaryKeyWithBLOBs(T record) {   return baseMapper.updateByPrimaryKeyWithBLOBs(record);  }  @Override  public int updateByPrimaryKey(T record) {   return baseMapper.updateByPrimaryKey(record);  }  @Override  public int insert(T record) {   return baseMapper.insert(record);  } }
  1. 具體的Mapper寫法:
package cn.liuyiyou.yishop.mapper; import java.util.List; import org.springframework.stereotype.Repository; import cn.liuyiyou.yishop.domain.ProductCategory; @Repository public interface ProductCategoryMapper extends BaseMapper<ProductCategory, Long>{  /**  * 通過父id得到類目列表  * @param praentId  * @return  */  List<ProductCategory> selectByParent(Long parent); }
  1. 具體的Servicd
package cn.liuyiyou.yishop.service.impl; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.liuyiyou.yishop.domain.Ad; import cn.liuyiyou.yishop.domain.Product; import cn.liuyiyou.yishop.mapper.AdMapper; import cn.liuyiyou.yishop.mapper.ProductMapper; import cn.liuyiyou.yishop.service.ProductService; @Service public class ProductServiceImpl extends AbstractService<Product,Long> implements ProductService {  @Autowired  private ProductMapper productMapper;  @Autowired  private AdMapper adMapper;  //這句必須要加上。不然會報空指針異常,因為在實際掉用的時候不是BaseMapper調用,而是這個productMapper  @Autowired  public void setBaseMapper(){   super.setBaseMapper(productMapper);  }  @Override  public Map<String,Object> testTwo() {   Product product = productMapper.selectByPrimaryKey(1l);   Ad ad = adMapper.selectByPrimaryKey(1l);   Map<String,Object> map = new HashMap<String,Object>();   map.put("product", product);   map.put("ad", ad);   return map;  } }

資料

http://liuyiyou.cn/2014/12/28/base-dao-service/ –這個好像有提到好壞

http://git.oschina.net/free/Mapper/blob/master/UseMapperInSpring4.md –這個看了一下,但是沒有進行試驗,不知道好壞與否


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM