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> {
- }
此泛型的实现,关键在于:clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
相关推荐
基于springMVC 和 MyBatis 泛型的代码生成模板,可以根据自己的需求修改,这里泛型的代码我就不上传了
mybatis的学习 dao mybatis的入门案例 第一步:读取配置文件 ... mybatis在使用代理dao的方式实现增删改查时做什么事? 只有两件事: 第一:创建代理对象 第二:在代理对象中调用selectList
mybatisDAO层自动生成插件,使用前需根据自己的操作系统,修改配置文件中代码生成的目录即可。
此项目主要实现了struts2.3+spring3.0+mybatist3.6+mysql的基本集成
MyBatis通用Dao,包含分页方法
MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, spring2.5参考手册MyBatis_3参考手册, ...
mybatis 通用dao的简单实现 简单的增 删 改 查 复杂功能未实现
二、在封装上针对Mybatis进行Dao、service层封装,里面提供两种实现机制:一种采用注解泛型动态实现cud操作,目前查询还是有缺陷;另一种机制采用xml文件形式实现dao、service、controller层封装,具体业务类只需...
NULL 博文链接:https://zhaoshijie.iteye.com/blog/2003209
idea 14 ssm 全注解框架+log4j+事物控制+mybatis基础Dao配置
使用Mybatis+Spring的源码,博客地址https://blog.csdn.net/weixin_41515594/article/details/109449066
mybatis开发dao方法(mapper方式)
mybatis开发Dao方法(原始方法teacher)
springboot+mybatis封装
现在越来越多的开发者使用MyBatis作为ORM的框架,它开发迅速,上手容易,开源代码多。但是在进行数据库操作的时候的独自生成的DaoMaper业务太多,在相互调用的业务之间,容易产生这样那样的事务问题。因此本人一直想...
mybatis 自动生成实体类dao
MyBatis_Dao,Mybatis的原始Dao的开发方式。
Mapper代理的开发方式,程序员只需要编写mapper接口(相当于dao接口)即可。Mybatis会自动的为mapper接口生成动态代理实现类。 代码有注释,很实用的例子
使用MyBatis Generator生成DAO
springboot+mybatis(mybatis dao与xml文件自动生成)