项目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