package com.csr.core.mapper;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
@Repository
public class BaseMapperImpl<T, PK extends Serializable> extends SqlSessionDaoSupport
implements BaseMapper<T, PK>
{
public static Logger logger = Logger.getLogger(BaseMapperImpl.class);
private Class<T> entityClass = null;
public BaseMapperImpl()
{
Class c = getClass();
Type type = c.getGenericSuperclass();
if ((type instanceof ParameterizedType))
{
Type[] parameterizedType = ((ParameterizedType)type)
.getActualTypeArguments();
this.entityClass = ((Class)parameterizedType[0]);
}
}
@Resource(name="sqlSessionFactory")
public void setSuperSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
{
super.setSqlSessionFactory(sqlSessionFactory);
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public T insert(T entity)
{
try
{
Field[] fileds = entity.getClass().getDeclaredFields();
for (Field field : fileds)
{
if (!field.getName().equals("id"))
continue;
Method setIdMethod = entity.getClass().getDeclaredMethod(
"setId", new Class[] { String.class });
setIdMethod.invoke(entity, new Object[] { IdUtil.getId() });
}
}
catch (SecurityException e)
{
e.printStackTrace();
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
getSqlSession().insert(entity.getClass().getName() + "Mapper.insert",
entity);
return entity;
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public void update(T entity)
{
getSqlSession().update(entity.getClass().getName() + "Mapper.update",
entity);
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public void deleteById(PK id)
{
getSqlSession().delete(this.entityClass.getName() + "Mapper.deleteById", id);
}
@Cacheable(value={"ehcache"}, key="#id")
public T fetch(PK id)
{
return getSqlSession().selectOne(
this.entityClass.getName() + "Mapper.fetch", id);
}
public List<T> findAll()
{
return getSqlSession().selectList(
this.entityClass.getName() + "Mapper.queryList", null);
}
public int count(Map<String, Object> condition)
{
int count = ((Integer)getSqlSession().selectOne(
this.entityClass.getName() + "Mapper.count", condition)).intValue();
return count;
}
@Cacheable(value={"ehcache"}, key="'queryList-'+#condition+'-orderBy-'+#orderBy+'-sortBy-'+#sortBy")
public List<T> queryList(Map<String, Object> condition, String orderBy, String sortBy)
{
if (condition == null)
{
condition = new HashMap();
condition.put("orderBy", orderBy);
condition.put("sortBy", sortBy);
}
return getSqlSession().selectList(
this.entityClass.getName() + "Mapper.queryList", condition);
}
@Cacheable(value={"ehcache"}, key="'queryOne-'+#condition")
public T queryOne(Map<String, Object> condition)
{
return getSqlSession().selectOne(
this.entityClass.getName() + "Mapper.queryOne", condition);
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public T updateOrSave(T t, PK id)
{
if (fetch(id) != null)
{
update(t);
}
else
{
return insert(t);
}
return t;
}
@Cacheable(value={"ehcache"}, key="'findOne-'+#property+'-'+#value")
public T findOne(String property, Object value)
{
Map condition = new HashMap();
condition.put(property, value);
return getSqlSession().selectOne(
this.entityClass.getName() + "Mapper.findOne", condition);
}
@Cacheable(value={"ehcache"}, key="'findList-'+#property+'-'+#value")
public List<T> findList(String property, Object value)
{
Map condition = new HashMap();
condition.put(property, value);
return getSqlSession().selectList(
this.entityClass.getName() + "Mapper.findList", condition);
}
public Class<T> getEntityClass()
{
return this.entityClass;
}
public Integer selectMaxId()
{
return (Integer)getSqlSession().selectOne(
this.entityClass.getName() + "Mapper.selectMaxId");
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public void deleteByCondition(Map<String, Object> condition)
{
getSqlSession().delete(
this.entityClass.getName() + "Mapper.deleteByCondition", condition);
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public void deleteByProperty(String property, Object value)
{
Map condition = new HashMap();
condition.put(property, value);
deleteByCondition(condition);
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public void updateNull(T entity)
{
getSqlSession().update(this.entityClass.getName() + "Mapper.updateNull",
entity);
}
@Cacheable(value={"ehcache"}, key="'selectOne-'+#mapperId+'-'+#obj")
public T selectOne(String mapperId, Object obj)
{
return getSqlSession().selectOne(
this.entityClass.getName() + "Mapper." + mapperId, obj);
}
@Cacheable(value={"ehcache"}, key="'selectList-'+#mapperId+'-'+#obj")
public List<T> selectList(String mapperId, Object obj)
{
return getSqlSession().selectList(
this.entityClass.getName() + "Mapper." + mapperId, obj);
}
@CacheEvict(value={"ehcache"}, allEntries=true)
public List<T> insertList(List<T> entities)
{
return getSqlSession().selectList(
this.entityClass.getName() + "Mapper.insertList", entities);
}
public List<T> like(String property, Object value)
{
Map condition = new HashMap();
condition.put(property, value);
return getSqlSession().selectList(
this.entityClass.getName() + "Mapper.like", condition);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
个人花了两个星期时间整的SpringMVC(spring-aop,spring-cache)及Mybatis框架,整个框架mybatis部分完全采用注解方式,通过BaseMapper及BaseService方法实现最基础的crud操作,当然整个框架在查询封装上运行偶尔会存在些问题还需完善,如果有高手在我代码基础上完善此功能的话请给大家一起分享!
资源推荐
资源详情
资源评论









格式:zip 资源大小:80.4KB




















收起资源包目录






























































































































































共 97 条
- 1

boguagua112
- 粉丝: 11
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- aspmaker7.0
- aspmaker7.0
- matlab 解码 NMEA0183格式GGA数据
- matlab 解码 NMEA0183格式GGA数据
- matlab 解码 NMEA0183格式GGA数据
- 基于 InternLM2 的王者荣耀角色扮演项目:融合多模态技术的峡谷小狐仙妲己聊天机器人
- 为学习目的从零开始编写大语言模型(LLM)相关全部代码
- Single novel 单本小说系统,基于python爬虫+flask(新版),旧版生成html静态文件.zip
- Selenium UI 自动化测试框架(基于 python 3+selenium).zip
- SimpleChinese2 集成了包括拼音汉字转换、近义词、繁简转换等在内的许多基本的中文自然语言处理功能,使基于 Python 的中文文字处理和信息提取变得简单方便。.zip
- superman是套基于Python unitest框架开发的一套实用于API测试和WEB UI测试自动化框架.zip
- Ubuntu安装pyhton3、pip3,并且部署python web项目(基于django).zip
- Stock Backtrader Web App 是一个基于 Python 的项目,旨在简化股票回测和分析
- WeChatAI 是一款基于 Python 开发的微信群聊_个人智能助手,支持多种大语言模型,可以实现智能对话、自动回复等功能。采用现代化的界面设计,操作简单直观。.zip
- Wagtail是一套基于Python Django的内容管理系统,为很多大型机构,比如NASA、Google、MIT、Mizilla等所使用,本项目旨在将其官方文档翻译整理为中文语言。.zip
- Web接口开发与自动化测试 基于Python语言.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
前往页