Spring Boot(四)通用CRUD案例、异常统一处理

本文介绍了Spring Boot项目中如何实现通用的CRUD操作,包括BaseEntity、BaseDao、BaseService、BaseController的定义和实现。同时,文章涵盖了异常统一处理,如ResultDataDto用于统一Json数据交互格式,以及自定义异常类如RuntimeFunctionException、RuntimeServiceException等。此外,还提供了项目GitHub链接以及BaseCrudController实现的模糊、分页查询功能。
摘要由CSDN通过智能技术生成



项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot (三)集成spring security,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.4



思路为:

BaseEntity

BaseDao,实现类分支:MyBatis、Redis

BaseService,实现类分支:MyBatis、Redis

BaseController,实现类分支:统一异常处理、MyBatis(增删改查,模糊分页)、MyBatis(读)、Redis(增删改查)

ResultDataDto,统一Json数据交互格式。

自定义异常类:业务逻辑异常;功能不完善;网络异常;其它异常。(权限异常由Spring Security处理,本章略)


------------------------------------------------------------------------------------------


BaseEntity,统一父类,实现反射,泛型动态实现统一增删改查。

src/main/java/com/training/core/entity/BaseEntity.java

package com.training.core.entity;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonProperty;
import tk.mybatis.mapper.annotation.NameStyle;
import tk.mybatis.mapper.code.Style;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@NameStyle(value = Style.camelhumpAndLowercase)
public class BaseEntity implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("id")
    private Integer id;

	/**
	 * 创建时间
	 */
	@Column
	private Date createTime;
	
	/**
	 * 最后修改时间
	 */
	@Column
	private Date lastModifyTime;
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}
}


定义BaseDao接口,统一增删改查规范:

src/main/java/com/training/core/dao/BaseDao.java

package com.training.core.dao;

import java.util.List;

import tk.mybatis.mapper.entity.Example;

import com.training.core.dto.FlexiPageDto;
import com.training.core.entity.BaseEntity;

public interface BaseDao<T extends BaseEntity> {

	/**
	 * 根据Id查询实体
	 */
	public T getEntityById(final Class<T> cls, final Integer id);
	
	/**
	 * 新增实体
	 */
	public void addEntity(final T entity);
	
	/**
	 * 更新实体
	 */
	public void updateEntity(final T entity);
	
	/**
	 * 根据Id删除实体
	 */
	public void deleteEntityById(final Class<T> cls, final Integer id);

	/**
	 * 查询全部
     */
	public List<T> selectAll(Class<T> cls);
	
	/**
	 * 单表模糊查询
	 */
	public List<T> findByLike(Example example);
	
	/**
	 * 根据模糊分页查询
	 */
	public List<T> findByPage(Example example, FlexiPageDto flexiPageDto);
	
	/**
	 * 单表模糊查询总记录数
	 */
	public int findRowCount(Example example);
	
	
}


MyBatis BaseDao实现:

src/main/java/com/training/core/dao/impl/MyBatisBaseDaoImpl.java

package com.training.core.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import com.training.core.annotation.MapperClass;
import com.training.core.dao.BaseDao;
import com.training.core.dto.FlexiPageDto;
import com.training.core.entity.BaseEntity;

@Repository("myBatisBaseDao")
@SuppressWarnings("unchecked")
public class MyBatisBaseDaoImpl<T extends BaseEntity> implements BaseDao<T> {
	
	@Resource
    @Qualifier("sessionFactory")
    private SqlSessionFactory sqlSessionFactory;
	
	private SqlSession sqlSession;
	
	@SuppressWarnings("rawtypes")
	public <M extends Mapper<T>> M getMapper(Class cls){
		MapperClass mapperClass = (MapperClass) cls.getAnnotation(MapperClass.class);
		if(null == mapperClass){
			throw new RuntimeException("没有注解MapperClass");
		}
		 	return (M) getSqlSession().getMapper(mapperClass.value());
	}

	@Override
	public T getEntityById(Class<T> cls, Integer id) {
		return this.getMapper(cls).selectByPrimaryKey(id);
	}

	@Override
	public void addEntity(T entity) {
		this.getMapper(entity.getClass()).insert(entity);
	}

	@Override
	public void updateEntity(T entity) {
		this.getMapper(entity.getClass()).updateByPrimaryKey(entity);
	}

	@Override
	public void deleteEntityById(Class<T> cls, Integer id) {
		this.getMapper(cls).deleteByPrimaryKey(id);
	}

	@Override
	public List<T> selectAll(Class<T> cls) {
		return this.getMapper(cls).selectAll();
	}

	@Override
	public List<T> findByLike(Example example) {
		return this.getMapper(example.getEntityClass()).selectByExample(example);
	}

	@Override
	public List<T> findByPage(Example example, FlexiPageDto flexiPageDto) {
		
		RowBounds rowBounds = new RowBounds(flexiPageDto.getOffset(), flexiPageDto.getRp());
		return this.getMapper(example.getEntityClass()).selectByExampleAndRowBounds(example, rowBounds);
	}

	@Override
	public int findRowCount(Example example) {
		return this.getMapper(example.getEntityClass()).selectCountByExample(example);
	}
	
	public SqlSession getSqlSession(){
        if (null == sqlSession){
        	synchronized (MyBatisBaseDaoImpl.class) {
        		this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
			}
        }
        return this.sqlSession;
    }
	
}


Redis统一增删改查,再下一章讲解,本章略。



定义BaseService接口,统一增删改查规范:

src/main/java/com/training/core/service/BaseService.java

<
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值