1、数据表结构
user_access_flow
2、renren-wap-fuxi-model
UserPlayFlowResult.java
/**
* $Id$
* Copyright 2009-2010 Oak Pacific Interactive. All rights reserved.
*/
package com.xiaonei.wap.fuxi.model;
import java.io.Serializable;
/**
*
* @author <a href="mailto:[email protected]">王侨</a>
* @createTime 2011-7-20下午07:27:51
*/
public class UserAccessFlow implements Serializable {
private static final long serialVersionUID = -3660276636695964258L;
private long id;
/** 用户Id */
private int userId;
/** 访问状态0:旧应用,1:新应用 */
private int access;
/** 最后访问时间 */
private long addTime;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the userId
*/
public int getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(int userId) {
this.userId = userId;
}
/**
* @return the access
*/
public int getAccess() {
return access;
}
/**
* @param access the access to set
*/
public void setAccess(int access) {
this.access = access;
}
/**
* @return the addTime
*/
public long getAddTime() {
return addTime;
}
/**
* @param addTime the addTime to set
*/
public void setAddTime(long addTime) {
this.addTime = addTime;
}
}
3、renren-wap-fuxi-service
UserAccessFlowEntity.java
/**
* $Id$
* Copyright 2009-2010 Oak Pacific Interactive. All rights reserved.
*/
package com.xiaonei.wap.fuxi.entity;
import java.util.Date;
/**
*
* @author <a href="mailto:[email protected]">王侨</a>
* @createTime 2011-7-20下午07:43:06
*/
public class UserAccessFlowEntity {
private long id;
private int userId;
private int access;
private Date addTime;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the userId
*/
public int getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(int userId) {
this.userId = userId;
}
/**
* @return the access
*/
public int getAccess() {
return access;
}
/**
* @param access the access to set
*/
public void setAccess(int access) {
this.access = access;
}
/**
* @return the addTime
*/
public Date getAddTime() {
return addTime;
}
/**
* @param addTime the addTime to set
*/
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
}
4、IUserAccessFlowDAO.java
注释:将 {@link ReturnGeneratedKeys} 声明在insert语句的方法上,表示返回的是插入的id
/**
* $Id$
* Copyright 2009-2010 Oak Pacific Interactive. All rights reserved.
*/
package com.xiaonei.wap.fuxi.dao;
import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;
import com.xiaonei.wap.fuxi.entity.UserAccessFlowEntity;
/**
*
* @author <a href="mailto:[email protected]">王侨</a>
* @createTime 2011-7-20下午07:45:48
*/
@DAO(catalog = "user_fuxi")
public interface IUserAccessFlowDAO {
/**
* 保存记录
* @param userAccessFlow
* @return
*/
@ReturnGeneratedKeys
@SQL("INSERT INTO user_access_flow(user_id, access, add_time) VALUES(:userAccessFlow.userId, :userAccessFlow.access, :userAccessFlow.addTime)")
public long create(@SQLParam("userAccessFlow") UserAccessFlowEntity userAccessFlow);
/**
* 根据user_id 更新user_access_flow
* @param userAccessFlow
* @return
*/
@SQL("UPDATE user_access_flow SET access=:userAccessFlow.access, add_time=:userAccessFlow.addTime " +
"WHERE user_id=:userAccessFlow.userId ")
public int update(@SQLParam("userAccessFlow") UserAccessFlowEntity userAccessFlow);
/**
* 根据userId 取 访问信息
* @param userId
* @return
*/
@SQL("select a.user_id, a.access, a.add_time from user_access_flow a where a.user_id = :userId ")
public UserAccessFlowEntity find(@SQLParam("userId") int userId);
}
5、IUserAccessService
/**
* $Id$
* Copyright 2009-2010 Oak Pacific Interactive. All rights reserved.
*/
package com.xiaonei.wap.fuxi.service;
import com.xiaonei.wap.fuxi.entity.UserAccessFlowEntity;
import com.xiaonei.wap.fuxi.model.UserAccessFlow;
/**
*
* @author <a href="mailto:[email protected]">王侨</a>
* @createTime 2011-7-21下午02:29:42
*/
public interface IUserAccessService {
/**
* 获取用户访问信息
* @param userId
* @return
*/
public UserAccessFlow getUserAccessFlow(int userId);
/**
* 保存用户访问信息
* @param userAccessFlow
* @return
*/
public long creatUserAccessFlow(UserAccessFlowEntity userAccessFlow);
/**
* 更新用户访问信息
* @param userAccessFlow
* @return
*/
public int updateUserAccessFlow(UserAccessFlowEntity userAccessFlow);
}
6、实现
/**
* $Id$
* Copyright 2009-2010 Oak Pacific Interactive. All rights reserved.
*/
package com.xiaonei.wap.fuxi.service.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import com.renren.xcache.XCache;
import com.xiaonei.wap.framework.cache.XCacheManager2;
import com.xiaonei.wap.fuxi.dao.IUserAccessFlowDAO;
import com.xiaonei.wap.fuxi.entity.UserAccessFlowEntity;
import com.xiaonei.wap.fuxi.model.UserAccessFlow;
import com.xiaonei.wap.fuxi.service.IUserAccessService;
/**
*
* @author <a href="mailto:[email protected]">王侨</a>
* @createTime 2011-7-21下午02:37:12
*/
public class UserAccessServiceImpl extends BaseBusinessService implements IUserAccessService,
InitializingBean{
/**
* Logger for this class
*/
private final Log logger = LogFactory.getLog(UserAccessServiceImpl.class);
private static final String CACHE_TYPE_USER_ACCESS_FLOW = "user_access_flow";
// user access缓存时间,默认1天
private int userAccessDefaultCacheTime = XCache.EXPIRE_DAY * 1;
private IUserAccessFlowDAO userAccessFlowDAO;
/**
* 基础方法,根据 userId获取访问基本信息
*
* @param userId
* @return
*/
@Override
public UserAccessFlow getUserAccessFlow(int userId) {
if (userId <0){
return null;
}
XCacheManager2 cache = XCacheManager2.getCacheManager(CACHE_TYPE_USER_ACCESS_FLOW);
UserAccessFlow userAccessFlow = cache.get(String.valueOf(userId), UserAccessFlow.class);
if (userAccessFlow == null){
userAccessFlow = loadUserAccessFlowFromDb(userId);
}
return userAccessFlow;
}
/**
* 从db中取一条userAccessFlow,放入缓存
* @param userId
* @return
*/
private UserAccessFlow loadUserAccessFlowFromDb(int userId) {
UserAccessFlowEntity userAccessFlow = userAccessFlowDAO.find(userId);
UserAccessFlow userAccessFlowResult = null;
String key = String.valueOf(userId);
if(userAccessFlow != null) {
//组装user access model
userAccessFlowResult = buildUserAccessFlow(userAccessFlow);
if(userAccessFlowResult != null) {
XCacheManager2 cache = XCacheManager2.getCacheManager(CACHE_TYPE_USER_ACCESS_FLOW);
cache.save(key, userAccessFlowResult, UserAccessFlow.class, userAccessDefaultCacheTime);
} else {
logger.error("can't build user access for key:" + key);
}
} else {
logger.error("can't find user access from db for key:" + key);
}
return userAccessFlowResult;
}
/**
* 对userAccessFlow进行包装
*
* @param userAccessFlow
* @param userAccessFlowResult
*/
private UserAccessFlow buildUserAccessFlow(UserAccessFlowEntity userAccessFlow) {
if(userAccessFlow == null) {
return null;
}
UserAccessFlow userAccessFlowResult = new UserAccessFlow();
userAccessFlowResult.setId(userAccessFlow.getId());
userAccessFlowResult.setUserId(userAccessFlow.getUserId());
userAccessFlowResult.setAccess(userAccessFlow.getAccess());
if (userAccessFlow.getAddTime() != null) {
userAccessFlowResult.setAddTime(userAccessFlow.getAddTime().getTime());
}
return userAccessFlowResult;
}
@Override
public long creatUserAccessFlow(UserAccessFlowEntity userAccessFlow) {
long result = 0;
try {
result = userAccessFlowDAO.create(userAccessFlow);
} catch (Exception e) {
logger.error("creatUserAccessFlow error, userId:" + userAccessFlow.getUserId(), e);
result = -1;
}
return result;
}
@Override
public int updateUserAccessFlow(UserAccessFlowEntity userAccessFlow) {
int result = 0;
try {
result = userAccessFlowDAO.update(userAccessFlow);
} catch (Exception e) {
logger.error("updateUserAccessFlow error, userId:" + userAccessFlow.getUserId(), e);
result = -1;
}
return result;
}
/**
* @param userAccessFlowDAO the userAccessFlowDAO to set
*/
public void setUserAccessFlowDAO(IUserAccessFlowDAO userAccessFlowDAO) {
this.userAccessFlowDAO = userAccessFlowDAO;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(userAccessFlowDAO, "userAccessFlowDAO is required!");
}
}