Exemplo n.º 1
0
  public void delete(String id) {
    RedisCachePool pool = null;
    Jedis jedis = null;
    RedisDao rd = null;
    try {
      pool = redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString());
      jedis = pool.getResource();
      Object note = RedisDao.getBean("Note:" + id, Note.class, jedis);

      if (null != note) {
        // 查询之后开启事物
        Transaction transation = jedis.multi();

        rd = new RedisDao(transation);
        rd.delSingleDataFromRedis(note, rd.getBeanField(note));

        /* 处理之后的数据库sql日志处理 */
        String logs = "delete from tcnote where note_id=" + id;
        rd.pubishLog(logs);
        rd.log(logs);

        transation.exec();
      }
    } catch (Exception e) {
      log.error(" delete(String id) 删除失败!" + e.getLocalizedMessage());
    } finally {
      log.info("回收jedis连接");
      pool.returnResource(jedis);
    }
  }
Exemplo n.º 2
0
  public void insert(Note note) {
    RedisCachePool pool = null;
    Jedis jedis = null;
    RedisDao rd = null;
    try {
      pool = redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString());
      jedis = pool.getResource();

      // 获取redis里面最大的主键值
      Set<String> sortKey = RedisDao.getRevrangeSortSet("Note:sort:noteId", 0, 0, jedis);
      for (String id : sortKey) {
        // 新增的主键赋值
        note.setNoteId(Integer.parseInt(id) + 1);
        break;
      }

      Transaction transation = jedis.multi();
      rd = new RedisDao(transation);
      BeanField beanField = rd.getBeanField(note);
      // 插入新增的
      rd.insertSingleDataToredis(note, beanField);

      /* 处理之后的数据库sql日志处理 */
      String logs = insertSql(note);
      rd.pubishLog(logs);
      rd.log(logs);
      transation.exec();

    } catch (Exception e) {
      log.error(" insert(Note note) 失败!" + e.getLocalizedMessage());
    } finally {
      log.info("回收insert==>jedis连接");
      pool.returnResource(jedis);
    }
  }
Exemplo n.º 3
0
  public Note queryById(String i) {
    Note note = new Note();
    RedisCachePool pool =
        redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString());
    Jedis jedis = pool.getResource();
    // 查询不用开启事物
    RedisDao rd = new RedisDao(jedis);
    note = (Note) rd.getBean("Note:" + i, note.getClass(), jedis);
    pool.returnResource(jedis);

    // dubbo 调用的时候防止java.sql.Blob cannot be assigned from null ,也就是blob字段不能为空
    note.setBlobContent(null);
    return note;
  }
Exemplo n.º 4
0
  public List<Note> findAll() {
    List<Note> noteList = new ArrayList<Note>();
    RedisCachePool pool = null;
    Jedis jedis = null;
    try {
      pool = redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString());
      jedis = pool.getResource();
      // 查询不用开启事物
      RedisDao rd = new RedisDao(jedis);
      Set<String> sortKey = rd.smembers("Note:index:noteId");
      noteList = (List<Note>) rd.getListBean(sortKey, Note.class, jedis);

      // dubbo 调用的时候防止java.sql.Blob cannot be assigned from null ,也就是blob字段不能为空
      delalBlob(noteList);
    } catch (Exception e) {
      log.error(" List<Note> findAll()查询失败!" + e.getLocalizedMessage());
    } finally {
      log.info("回收jedis连接");
      pool.returnResource(jedis);
    }
    return noteList;
  }
Exemplo n.º 5
0
  /** 更新直接调用删除,然后再插入 */
  public void update(Note newNote) {
    RedisCachePool pool = null;
    Jedis jedis = null;
    RedisDao rd = null;
    try {
      pool = redisCacheManager.getRedisPoolMap().get(RedisDataBaseType.defaultType.toString());
      jedis = pool.getResource();

      // 获取原来redis里面存储的note
      Object orldNote = RedisDao.getBean("Note:" + newNote.getNoteId(), Note.class, jedis);
      if (null != orldNote) {
        // 查询之后开启事物
        Transaction transation = jedis.multi();
        rd = new RedisDao(transation);
        BeanField beanField = rd.getBeanField(orldNote);

        // // 先删除原来的
        // rd.delSingleDataFromRedis(orldNote, beanField);
        // // 再插入新修改的note
        // rd.insertSingleDataToredis(newNote, beanField);

        rd.updateSingleFromToredis(orldNote, newNote, beanField);

        /* 处理之后的数据库sql日志处理 */
        String logs = genSql(newNote);
        rd.pubishLog(logs);
        rd.log(logs);
        transation.exec();
      }
    } catch (Exception e) {
      log.error(" update(Note note) 失败!" + e.getLocalizedMessage());
    } finally {
      // log.info("回收jedis连接");
      pool.returnResource(jedis);
    }
  }