使用泛型类简化ibatis系统架构
jdk1.5的推出为我们带来了枚举、泛型、foreach循环、同步工具包等等好东西。其中,泛型的使用为我们的代码开发提供了很大的简便,简化了我们的代码。
1、设计思路
1)GenericDao泛型类提供所有的增删改查功能;
2)所有的dao在继承GenericDao泛型类拥有自身的增删改查功能,不需再写其他代码。
2、实现GenericDao,基类
public abstract class GenericDao<T, PK extends Serializable> {
protected final Logger logger = LoggerFactory.getLogger(getClass());
public static final String POSTFIX_SELECTBYID = ".selectById";
public static final String POSTFIX_SELECTBYIDS = ".selectByIds";
public static final String POSTFIX_SELECTBYMAP = ".selectByMap";
public static final String POSTFIX_SELECTIDSLIKEBYMAP = ".selectIdsLikeByMap";
public static final String POSTFIX_PKSELECTMAP = ".pkSelectByMap";
public static final String POSTFIX_COUNT = ".count";
public static final String POSTFIX_COUNTLIKEBYMAP = ".countLikeByMap";
public static final String POSTFIX_INSERT = ".insert";
public static final String POSTFIX_DELETEBYID = ".deleteById";
public static final String POSTFIX_DELETEBYIDS = ".deleteByIds";
public static final String POSTFIX_DELETEBYIDSMAP = ".deleteByIdsMap";
public static final String POSTFIX_DELETEBYMAP = ".deleteByMap";
public static final String POSTFIX_UPDATE = ".update";
public static final String POSTFIX_UPDATEBYMAP = ".updateByMap";
public static final String POSTFIX_UPDATEBYIDSMAP = ".updateByIdsMap";
protected Class<T> clazz;
protected String clazzName;
protected T t;
// 定义主从数据库服务器,主数据库负责(Write op),从数据库负责(read op)
@Autowired
protected SqlMapClientTemplate masterSqlMapClientTemplate;
@Autowired
protected SqlMapClientTemplate slaveSqlMapClientTemplate;
public GenericDao() {
// 通过范型反射,取得在子类中定义的class.
clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
clazzName = clazz.getSimpleName();
}
public Integer count(String propertyName, Object propertyValue) {
return count(new String[]{propertyName},new Object[]{propertyValue});
}
public Integer count(String[] propertyNames, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < propertyNames.length; i++) {
map.put(propertyNames[i], propertyValues[i]);
}
return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_COUNT, map);
}
@Override
public Integer countLikeByMap(String[] propertyNames, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < propertyNames.length; i++) {
map.put(propertyNames[i], propertyValues[i]);
}
return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_COUNTLIKEBYMAP, map);
}
/** 根据自定义SqlMap中的条件语句查询出记录数量 */
public Integer countByStatementPostfix(String statementPostfix,String[] properties, Object[] propertyValues){
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
return (Integer) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + statementPostfix, map);
}
/**
* 通过id得到实体对象
*/
public T findById(PK id) {
return (T) slaveSqlMapClientTemplate.queryForObject(clazz.getName() + POSTFIX_SELECTBYID, id);
}
/**
* 根据ids获取实体列表
*
* @param ids
* @return
*/
public List<T> findByIds(List<PK> ids) {
return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYIDS, ids);
}
/**
* 直接从数据库查询出ids列表(包括符合条件的所有id)
*
* @param properties
* 查询条件字段名
* @param propertyValues
* 字段取值
* @return
*/
public List<PK> findIdsBy(String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);
}
/**
* 根据条件查询结果
*
* @param properties
* 查询条件名
* @param propertyValues
* 查询条件值
* @return
*/
public List<T> findByMap(String[] properties, Object[] propertyValues, String orderBy, String order) {
return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYMAP, map);
}
/**
* 根据条件查询结果的id列表
*
* @param properties
* 查询条件名
* @param propertyValues
* 查询条件值
* @return
*/
public List<PK> findIdsByMap(String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);
}
/**
* 分页查询(未处理缓存)
*
* @param properties
* 查询条件字段名
* @param propertyValues
* 字段取值
* @return
*/
public List<T> pageQueryBy(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
return (List<T>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTBYMAP, map);
}
/**
* 从数据库分页查询出ids列表的前200个id
*
* @param properties
* 查询条件字段名
* @param propertyValues
* 字段取值
* @return
*/
protected List<PK> pageQueryIdsBy(String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", 0);// 操作
map.put("end", Config.getInstance().getPageCacheSize());
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);
}
/**
* 分页查询出id列表(处理缓存)
*
* @param properties
* 查询条件字段名
* @param propertyValues
* 字段取值
* @return
*/
public List<PK> pageQueryIdsByMap(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
return (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_PKSELECTMAP, map);
}
/** like分页查询(不走列表缓存) */
public List<T> pageLikeBy(String[] properties, Object[] propertyValues, String orderBy, String order, int pageSize, int pageNo){
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
List<PK> ids = (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + POSTFIX_SELECTIDSLIKEBYMAP, map);
return findByIds(ids);
}
/**
* 新增对象
*/
public Serializable insert(T o) throws Exception {
if (t instanceof QueryCachable) {
// 清除受影响的缓存
clearListCache(o);
}
return (Serializable) masterSqlMapClientTemplate.insert(clazz.getName() + POSTFIX_INSERT, o);
}
/**
* 更新对象
*/
public T update(T o) throws Exception {
masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATE, o);
return o;
}
/**
* 更新对象的部分属性
*/
public int update(PK id, String[] properties, Object[] propertyValues) throws Exception {
// 更新数据库
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
map.put("id", id);
return masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATEBYMAP, map);
}
/**
* 根据ID列表更新对象的部分属性
*/
public int updateByIdsMap(List<PK> ids,String[] properties, Object[] propertyValues) throws Exception{
// 更新数据库
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
map.put("ids", ids);
return masterSqlMapClientTemplate.update(clazz.getName() + POSTFIX_UPDATEBYIDSMAP, map);
}
/**
* 根据ID删除对象
*/
public void deleteById(PK id) throws Exception {
masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYID, id);
}
/**
* 根据ID删除对象
*/
public void deleteByIds(List<PK> ids) throws Exception {
masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYIDS, ids);
}
/** 根据ID及条件删除对象 */
public void deleteByIdsMap(List<PK> ids, String[] properties, Object[] propertyValues) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
map.put("ids", ids);
masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYIDSMAP, map);
}
/**
* 根据条件删除对象
*/
public int deleteByMap(String[] properties, Object[] propertyValues) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
return masterSqlMapClientTemplate.delete(clazz.getName() + POSTFIX_DELETEBYMAP, map);
}
/**
* 根据自定义SqlMap中的条件语句查询出列表(注意:不处理缓存)
*/
public List<T> findByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues, String orderBy, String order) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
return slaveSqlMapClientTemplate.queryForList(clazz.getName() + statementPostfix, map);
}
/**
* 根据自定义SqlMap中的条件语句查询出列表(注意:不处理缓存)
*/
public List<T> pageQueryByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues, String orderBy, String order,int pageSize,int pageNo) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
if (orderBy != null) {
map.put("orderBy", orderBy);
map.put("order", order);
}
map.put("limit", true);
map.put("start", (pageNo - 1) * pageSize );// limit 操作
map.put("end", pageSize);
List<PK> ids = (List<PK>) slaveSqlMapClientTemplate.queryForList(clazz.getName() + statementPostfix, map);
return findByIds(ids);
}
/**
* 根据自定义SqlMap中的条件语句更新数据(注意:不处理缓存)
*/
public void updateByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
masterSqlMapClientTemplate.update(clazz.getName() + statementPostfix, map);
}
/**
* 根据自定义SqlMap中的条件语句删除数据(注意:不处理缓存)
*/
public void deleteByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
masterSqlMapClientTemplate.delete(clazz.getName() + statementPostfix, map);
}
/**
* 根据自定义SqlMap中的条件语句插入数据(注意:不处理缓存)
*/
public void insertByStatementPostfix(String statementPostfix, String[] properties, Object[] propertyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < properties.length; i++) {
map.put(properties[i], propertyValues[i]);
}
masterSqlMapClientTemplate.insert(clazz.getName() + statementPostfix, map);
}
}
3、所有其他Dao只需继承GenericDao,传个具体的类型,不需再写增删改查的方法
package com.teacherclub.business.album.dao;
import org.springframework.stereotype.Repository;
import com.teacherclub.business.album.entity.Album;
import com.teacherclub.core.mem.MemcacheUtil;
import com.teacherclub.core.orm.EntityCachable;
import com.teacherclub.core.orm.dao.impl.GenericDao;
@Repository
public class AlbumDao extends GenericDao<Album,Integer> { //传参数Album类
}
此泛型的实现,关键在于:clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
这在项目中经常使用。
使用泛型类简化ibatis系统架构的更多相关文章
- Windows Internals学习笔记(二)系统架构
参考资料: 1. <Windows Internals> 2. http://bestcbooks.com 3. Windows Drive Kit 4. Microsoft Window ...
- 【转】深入分析 iBATIS 框架之系统架构与映射原理
深入分析 iBATIS 框架之系统架构与映射原理 iBATIS 通过 SQL Map 将 Java 对象映射成 SQL 语句和将结果集再转化成 Java 对象,与其他 ORM 框架相比,既解决了 Ja ...
- 论 业务系统 架构 的 简化 (一) 不需要 MQ
MQ , 就是 消息队列(Message Queue), 不知从什么时候起, MQ 被用来 搭建 分布式 业务系统 架构, 一个重要作用 就是用来 “削峰” . 我们 这里 就来 讨论 如何 设 ...
- 异构(兼容dubbo)SOA系统架构(.net)优化升级
前面一片文章已经提到我司的异构(兼容dubbo)SOA系统架构,解决了不少技术痛点,也还算比较完善,也顺利推广开来. 但作为项目的开发者,自己产品的问题心里是清楚的,离自己满意还是有不小的距离. 在推 ...
- Android系统架构说明介绍
Android系统架构说明介绍 Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高层到低层分别是应用程序层.应用程序框 ...
- 系统架构师JD
#################################################################################################### ...
- Android的系统架构
转自Android的系统架构 从上图中可以看出,Android系统架构为四层结构,从上层到下层分别是应用程序层.应用程序框架层.系统运行库层以及Linux内核层,分别介绍如下: 1)应用程序层 ...
- Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs)
Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs) Cola:一个分布式爬虫框架 发布时间:2013-06-17 14:58:27, 关注:+2034, 赞美: ...
- 大数据高并发系统架构实战方案(LVS负载均衡、Nginx、共享存储、海量数据、队列缓存)
课程简介: 随着互联网的发展,高并发.大数据量的网站要求越来越高.而这些高要求都是基础的技术和细节组合而成的.本课程就从实际案例出发给大家原景重现高并发架构常用技术点及详细演练. 通过该课程的学习,普 ...
随机推荐
- Lync二次开发关于Lync启动退出问题
以前使用C++开发的version.dll文件,由于各个用户环境的不同,造成某些用户加载不了我们开发的插件,并且写version.dll的同事还没找到好的解决办法,所以得换一种思路去解决这个问题,就是 ...
- Android GreenDAO 3.0 不修改版本号的情况下增加、删除表、添加字段
最近项目中使用了GreenDAO的3.0以上的版本,出现需要增加删除表的需求,刚开始用,发现官方对增加和删除的方法是每次去修改数据库版本号,版本一旦升级,那么原来数据库中的表会全部删除再重建.太麻烦, ...
- 【Android学习入门】Android studio基本设置
1.背景设置 依次选择File->Settings-->Appearance & Behaviour->Apprearance,然后勾选 show line number. ...
- R.java文件无法自动生成的问题
如果出现R.java文件无法自动生成的问题,同时Console窗口提示下列信息: Android requires compiler compliance level 5.0 or 6.0. Foun ...
- Quartz .Net(定时框架):
Quartz .Net(定时框架): 基本说明: 说明:Quartz .Net 是一个从 Java 版的 Quartz 移植过来定时任务框架,可以实现异常灵活的定 时任务 用法: 安装 Quartz ...
- 如何提升SharePoint 2010的性能
文章来自: http://www.chinaemail.com.cn/server/xtfl/Exchange/201109/66466.html SharePoint是微软历史上销售量增长最快的产品 ...
- Linux学习_按时间顺序解压多个文件,搜索文件中的内容
ls的结果按时间数据先排序,再取末尾5个文件,再调用tar命令 ls -1 | sort -u | tail -5 |xargs -n1 tar xzvf 利用grep命令从文件中搜索. grep - ...
- Linux系统如何设置开机程序自启动
在Linux系统如何让程序开机时自动启动 核心提示:系统的服务在开机时一般都可以自动启动,那在linux系统下如果想要程序在开机时自动启动怎么办?我们知道在 windows系统“开始”--& ...
- 表面积最小(POJ3536)
题目链接:http://poj.org/problem?id=3536 在体积固定的情况下,表面积最小时的长,宽,高. 这里枚举长,宽,根据体积计算高. #include <iostream&g ...
- 学习MyBatis之简单入门HelloWorld
转:https://blog.csdn.net/gaomb_1990/article/details/78299784 一.准备 Eclipse:Luna Service Release 1 (4.4 ...