使用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 –这个看了一下,但是没有进行试验,不知道好坏与否

使用mybatis完成通用dao和通用service的更多相关文章

  1. spring+mybatis通用dao层、service层的实现

    个人理解: 1.mybatis-spring.jar 提供了SqlSessionTemplate类该类可以对数据库进行CRUD操作(底层其实还是SqlSession) 2.我们可以集成SqlSessi ...

  2. Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...

  3. Mybatis 使用PageHelper封装通用Dao分页方法

    参考: PageHelper官网:https://pagehelper.github.io/docs/howtouse/#3-%E5%A6%82%E4%BD%95%E5%9C%A8%E4%BB%A3% ...

  4. spring基于通用Dao的多数据源配置详解【ds1】

    spring基于通用Dao的多数据源配置详解 有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种 ...

  5. spring基于通用Dao的多数据源配置

    有时候在一个项目中会连接多个数据库,须要在spring中配置多个数据源,近期就遇到了这个问题,因为我的项目之前是基于通用Dao的,配置的时候问题不断.这样的方式和资源文件冲突:扫描映射文件的话,Sql ...

  6. Java学习笔记之使用反射+泛型构建通用DAO

    PS:最近简单的学了学后台Servlet+JSP.也就只能学到这里了.没那么多精力去学SSH了,毕竟Android还有很多东西都没学完.. 学习内容: 1.如何使用反射+泛型构建通用DAO. 1.使用 ...

  7. Java反射结合JDBC写的一个通用DAO

    以前写反射只是用在了与设计模式的结合上,并没有考虑到反射可以与DAO结合.也是一个偶然的机会,被正在上培训的老师点到这个问题,才考虑到这个可能性,于是上网参考各种代码,然后自己动手开发了一个通用DAO ...

  8. Spring Boot-------JPA——EntityManager构建通用DAO

    EntityManager EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满 ...

  9. 十六、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法

    @NoRepositoryBean:Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口 1.通用接口: import org.springframework.da ...

随机推荐

  1. JSP-07-使用JavaBean封装数据

    7.1 常命包名 Dao 包中的接口(NewsDao)以及类(NewsDaoImpl)注意负责和数据操作相关的事情. Service 包中的接口和类对dao的方法进行封装和调用,注意负责和业务逻辑相关 ...

  2. 使用 Delphi Xe 的 TDictionary

    原本一直使用 TList, 将定义的一个个 Record 保存在TList 里面, 为了能把某些对象管理起来, 例如一个类的 n 多实例,可以进行索引.查找.释放等 今天刚看到原来已经有了一个叫 TD ...

  3. TI BLE CC2541的通讯协议.

    包类型: 01命令/02数据/03应答消息 开始标志FF/本数据包长度(注意是16进制)/校验码/包ID/包类型01: 表示是命令/01表示下面要开始传输/03字符串编号/字符串长度/结束位FEFF  ...

  4. linux内核参数优化

    net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route ...

  5. javaWeb 使用jsp开发 html过滤标签

    1.jsp调用代码 <t:htmlFilter> <a href="${pageContext.request.contextPath }/index.jsp"& ...

  6. 理解逐次逼近寄存器型ADC:与其它类型ADC的架构对比【转】

    转自:http://bbs.dzsc.com/space/viewspacepost.aspx?postid=86760 摘要:逐次逼近寄存器型(SAR)模数转换器(ADC)占据着大部分的中等至高分辨 ...

  7. ServiceStack.Redis 之 IRedisTypedClient 04_转

    IRedisTypedClient IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似. 它支持在Redis中使用Linq查 ...

  8. 关于JDK,tomcat,MyEclipse的配置

    1.下载安装JDK 在自定义安装路径时,jdk和之后的jre文件夹是属于平行结构,我的安装路径为:D:\jdk\jdk1.6.0_43和D:\jdk\jre6 然后是对环境变量的配置, 计算机→属性→ ...

  9. Pro ASP.NET MVC 5 Framework.学习笔记.6.4.MVC的必备工具

    2.5.创建链式依赖 当你请求Ninject创建一个类型,它检查该类型的依赖是否声明.它也会检查该依赖是否依赖其他类型.如果这里有附加依赖,Ninject自动解决他们,并创建请求的所有类的实例.正是由 ...

  10. 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.2.更换主题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...