是对String类型插入的两个测试。test方法中,使用了模版类提交回调(RedisCallBack)的方法来使用jedis connection操作数据。这一部分,有没有似曾相识呢?
HibernateTemplate的HibernateCallback,以及Hibernate Session类中的doWork以及doReturningWork方法,都是使用了这样的机制,方便对于连接或者session的统一管理。
public int excuteHqlUpdate(final String hql,final Object ...params){
? ? ? ? return getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? @SuppressWarnings("unchecked")
? ? ? ? ? ? public Integer doInHibernate(Session session) throws HibernateException {
? ? ? ? ? ? ? ? Query queryObject = session.createQuery(hql);
? ? ? ? ? ? ? ? if (params != null) {
? ? ? ? ? ? ? ? ? ? for (int i = 0; i < params.length; i++) {
? ? ? ? ? ? ? ? ? ? ? ? queryObject.setParameter(i, params[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return queryObject.executeUpdate();
? ? ? ? ? ? }
? ? ? ? });
? ? }
4. 总结
我们这节,讲了spring redis的配置使用,基本特性,以及引入使用RedisTemplate。通过之前HibernateTemplate的对比,也应该对RedisTemplate的基本设计有了一定的了解。下节,我们将进行深入学习RedisTempalte。