对比新文件 |
| | |
| | | package com.lunhan.xxx.repository; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.lunhan.xxx.common.enums.ELogger; |
| | | import com.lunhan.xxx.common.enums.EResultCode; |
| | | import com.lunhan.xxx.common.enums.EYesOrNo; |
| | | import com.lunhan.xxx.common.exceptions.BusinessException; |
| | | import com.lunhan.xxx.common.util.LoggerUtil; |
| | | import org.apache.ibatis.session.ResultHandler; |
| | | import org.slf4j.Logger; |
| | | import org.springframework.dao.EmptyResultDataAccessException; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.persistence.Table; |
| | | import java.io.Serializable; |
| | | import java.lang.reflect.ParameterizedType; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 数据访问 实现类 |
| | | * |
| | | * @param <T> 数据实体 |
| | | */ |
| | | public class BasicMapper<T> { |
| | | protected BaseMapper<T> DB; |
| | | protected Class<T> clazz; |
| | | protected String tableName; |
| | | |
| | | protected static final Logger logger = LoggerUtil.get(ELogger.SQL_LOG); |
| | | |
| | | /** |
| | | * 构造方法 |
| | | */ |
| | | public BasicMapper(BaseMapper<T> db) { |
| | | this.clazz = (Class<T>) ( |
| | | (ParameterizedType) getClass().getGenericSuperclass() |
| | | ).getActualTypeArguments()[0]; |
| | | this.DB = db; |
| | | Table annotation = clazz.getAnnotation(Table.class); |
| | | if (null == annotation) { |
| | | return; |
| | | } |
| | | this.tableName = annotation.name(); |
| | | } |
| | | |
| | | public LambdaQueryWrapper<T> query() { |
| | | return Wrappers.lambdaQuery(); |
| | | } |
| | | |
| | | public UpdateWrapper<T> update() { |
| | | return Wrappers.update(); |
| | | } |
| | | |
| | | public int insert(T entity) { |
| | | return DB.insert(entity); |
| | | } |
| | | |
| | | public int deleteById(T entity) { |
| | | return DB.deleteById(entity); |
| | | } |
| | | |
| | | public int delete(Wrapper<T> queryWrapper) { |
| | | return DB.delete(queryWrapper); |
| | | } |
| | | |
| | | public int updateById(T entity) { |
| | | return DB.updateById(entity); |
| | | } |
| | | |
| | | public int update(T entity, Wrapper<T> updateWrapper) { |
| | | return DB.update(entity, updateWrapper); |
| | | } |
| | | |
| | | public T selectById(Serializable id) { |
| | | return DB.selectById(id); |
| | | } |
| | | |
| | | public List<T> selectBatchIds(Collection<? extends Serializable> idList) { |
| | | return DB.selectBatchIds(idList); |
| | | } |
| | | |
| | | public void selectBatchIds(Collection<? extends Serializable> idList, ResultHandler<T> resultHandler) { |
| | | DB.selectBatchIds(idList, resultHandler); |
| | | } |
| | | |
| | | public Long selectCount(Wrapper<T> queryWrapper) { |
| | | return DB.selectCount(queryWrapper); |
| | | } |
| | | |
| | | public List<T> selectList(Wrapper<T> queryWrapper) { |
| | | return DB.selectList(queryWrapper); |
| | | } |
| | | |
| | | public void selectList(Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) { |
| | | DB.selectList(queryWrapper, resultHandler); |
| | | } |
| | | |
| | | public List<T> selectList(IPage<T> page, Wrapper<T> queryWrapper) { |
| | | return DB.selectList(page, queryWrapper); |
| | | } |
| | | |
| | | public void selectList(IPage<T> page, Wrapper<T> queryWrapper, ResultHandler<T> resultHandler) { |
| | | DB.selectList(page, queryWrapper, resultHandler); |
| | | } |
| | | |
| | | public List<Map<String, Object>> selectMaps(Wrapper<T> queryWrapper) { |
| | | return DB.selectMaps(queryWrapper); |
| | | } |
| | | |
| | | public void selectMaps(Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) { |
| | | DB.selectMaps(queryWrapper, resultHandler); |
| | | } |
| | | |
| | | public List<Map<String, Object>> selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper) { |
| | | return DB.selectMaps(page, queryWrapper); |
| | | } |
| | | |
| | | public void selectMaps(IPage<? extends Map<String, Object>> page, Wrapper<T> queryWrapper, ResultHandler<Map<String, Object>> resultHandler) { |
| | | DB.selectMaps(page, queryWrapper, resultHandler); |
| | | } |
| | | |
| | | public <E> List<E> selectListObject(Wrapper<T> queryWrapper) { |
| | | return DB.selectObjs(queryWrapper); |
| | | } |
| | | |
| | | public <E> void selectListObject(Wrapper<T> queryWrapper, ResultHandler<E> resultHandler) { |
| | | DB.selectObjs(queryWrapper, resultHandler); |
| | | } |
| | | |
| | | /** |
| | | * 根据条件获取数据实体列表 |
| | | * |
| | | * @param queryWrapper 查询条件 |
| | | */ |
| | | @Transactional(readOnly = true) |
| | | protected List<T> getList(Wrapper<T>... queryWrapper) { |
| | | return DB.selectList(queryWrapper.length > 0 ? queryWrapper[0] : null); |
| | | } |
| | | |
| | | /** |
| | | * 条件查询单个数据实体 |
| | | * |
| | | * @param queryWrapper 查询条件 |
| | | */ |
| | | @Transactional(readOnly = true) |
| | | protected T get(Wrapper<T> queryWrapper) { |
| | | String method = "BasicDao.Wrapper<T> queryWrapper"; |
| | | try { |
| | | return DB.selectOne(queryWrapper); |
| | | } catch (EmptyResultDataAccessException e) { |
| | | logger.error(method, e); |
| | | return null; |
| | | } catch (Exception e) { |
| | | logger.error(method + ", 发生异常", e); |
| | | throw new BusinessException(EResultCode.ERROR, "BasicDao.get(SqlBuilder builder)发生异常." + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据传入sql查询数据 |
| | | * |
| | | * @param queryWrapper 查询条件 |
| | | */ |
| | | public List<Map<String, Object>> getMap(Wrapper<T> queryWrapper) { |
| | | return DB.selectMaps(queryWrapper); |
| | | } |
| | | |
| | | /** |
| | | * 查询记录条数 |
| | | * |
| | | * @param queryWrapper 查询条件 |
| | | */ |
| | | public Long getCount(Wrapper<T> queryWrapper) { |
| | | return DB.selectCount(queryWrapper); |
| | | } |
| | | |
| | | /** |
| | | * 根据主键id删除数据 |
| | | * |
| | | * @param id 主键id |
| | | * @return 是否成功 |
| | | */ |
| | | public Boolean remove(Long id) { |
| | | int rowCount = DB.deleteById(id); |
| | | if (rowCount > 0) { |
| | | return Boolean.TRUE; |
| | | } |
| | | return Boolean.FALSE; |
| | | } |
| | | |
| | | /** |
| | | * 根据主键id删除数据 |
| | | * |
| | | * @param listId id列表 |
| | | * @return 是否成功 |
| | | */ |
| | | public Boolean remove(List<Long> listId) { |
| | | int rowCount = DB.deleteByIds(listId); |
| | | if (rowCount > 0) { |
| | | return Boolean.TRUE; |
| | | } |
| | | return Boolean.FALSE; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 根据条件删除数据 |
| | | * |
| | | * @param queryWrapper 查询条件 |
| | | */ |
| | | protected Boolean remove(Wrapper<T> queryWrapper) { |
| | | int rowCount = DB.delete(queryWrapper); |
| | | if (rowCount > 0) { |
| | | return Boolean.TRUE; |
| | | } |
| | | return Boolean.FALSE; |
| | | } |
| | | |
| | | /** |
| | | * 根据主键id逻辑删除数据(逻辑删除) |
| | | * |
| | | * @param id 主键id |
| | | * @return 是否成功 |
| | | */ |
| | | public Boolean deleteLogic(Long id) { |
| | | UpdateWrapper<T> where = Wrappers.update(); |
| | | where.set("is_delete", EYesOrNo.YES.getValue()); |
| | | where.eq("id", id); |
| | | int rowCount = DB.update(where); |
| | | if (rowCount > 0) { |
| | | return Boolean.TRUE; |
| | | } |
| | | return Boolean.FALSE; |
| | | } |
| | | |
| | | /** |
| | | * 根据逐渐删除数据(逻辑删除) |
| | | * |
| | | * @param listId id列表 |
| | | * @return 是否成功 |
| | | */ |
| | | public Boolean deleteLogic(List<Long> listId) { |
| | | UpdateWrapper<T> where = Wrappers.update(); |
| | | where.set("is_delete", EYesOrNo.YES.getValue()); |
| | | where.in("id", listId); |
| | | int rowCount = DB.update(where); |
| | | if (rowCount > 0) { |
| | | return Boolean.TRUE; |
| | | } |
| | | return Boolean.FALSE; |
| | | } |
| | | } |