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

15 篇文章 0 订阅

概述:

使用通用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;
  }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个使用Mybatis-Plus进行通用DAO层封装的示例代码: 1. 首先,我们需要引入Mybatis-Plus依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> ``` 2. 接着,我们需要定义一个基础的Mapper接口,用于封装通用的增删改查操作: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface MyBaseMapper<T> extends BaseMapper<T> { } ``` 3. 然后,我们需要定义一个基础的Service接口,用于封装通用的增删改查操作: ```java import com.baomidou.mybatisplus.extension.service.IService; public interface MyBaseService<T> extends IService<T> { } ``` 4. 最后,我们需要定义一个基础的ServiceImpl实现类,用于实现通用的增删改查操作: ```java import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; public abstract class MyBaseServiceImpl<M extends MyBaseMapper<T>, T> extends ServiceImpl<M, T> implements MyBaseService<T> { } ``` 通过以上代码实现,我们可以很方便地使用Mybatis-Plus进行通用DAO层封装。接下来,我们只需要定义具体的Mapper和Service接口,继承以上基础接口即可,如: ```java public interface UserMapper extends MyBaseMapper<User> { } public interface UserService extends MyBaseService<User> { } public class UserServiceImpl extends MyBaseServiceImpl<UserMapper, User> implements UserService { } ``` 这样,我们就可以通过UserService接口,调用Mybatis-Plus提供的通用方法,实现对User表的增删改查操作了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值