import java.io.*;
public class User implements Serializable {
private Integer userId;
private String userAlias;
private String password;
private String realName;
private String email;
private String userTypeId;
public User() {
}
public User(Integer userId) {
this.userId=userId;
}
public User(String userAlias) {
this.userAlias=userAlias;
}
public User(String userAlias,String password) {
this.userAlias=userAlias;
this.password=password;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public void setUserAlias(String userAlias) {
this.userAlias = userAlias;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserTypeId(String userTypeId) {
this.userTypeId = userTypeId;
}
public void setRealName(String realName) {
this.realName = realName;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getUserId() {
return userId;
}
public String getUserAlias() {
return userAlias;
}
public String getPassword() {
return password;
}
public String getUserTypeId() {
return userTypeId;
}
public String getRealName() {
return realName;
}
public String getEmail() {
return email;
}
}
import java.util.*;
import java.io.Serializable;
public class UserInfo implements Serializable {
private Integer userId;
private Date regDate;
private Date lastLogDate;
private Integer logTimes;
public UserInfo() {
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public void setLastLogDate(Date lastLogDate) {
this.lastLogDate = lastLogDate;
}
public void setLogTimes(Integer logTimes) {
this.logTimes = logTimes;
}
public Integer getUserId() {
return userId;
}
public Date getRegDate() {
return regDate;
}
public Date getLastLogDate() {
return lastLogDate;
}
public Integer getLogTimes() {
return logTimes;
}
}
public interface BaseDao {
}
public interface SequenceDao extends BaseDao {
Integer getSequenceUserId();
}
import com.wysm.netstar.domain.User;
import java.util.List;
public interface UserDao extends BaseDao {
public abstract List getUser(String userAlias);
public abstract List getUserByEmail(String email);
public abstract List getUser(String userAlias,String password);
public abstract void insertUser(User user);
public abstract void updateUser(User user);
public abstract boolean existUserId(Integer userid);
}
import com.wysm.netstar.domain.UserInfo;
public interface UserInfoDao extends BaseDao {
public abstract UserInfo getUserInfo(Integer userId);
public abstract void insertUserInfo(UserInfo userInfo);
public abstract void updateUserInfo(UserInfo userInfo);
}
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
public class BaseSqlMapDao extends SqlMapDaoTemplate {
protected static final int PAGE_SIZE = 4;
public BaseSqlMapDao(DaoManager daoManager) {
super(daoManager);
}
}
import com.ibatis.dao.client.DaoManager;
import com.wysm.netstar.persistence.iface.SequenceDao;
public class SequenceSqlMapDao extends BaseSqlMapDao implements SequenceDao {
public SequenceSqlMapDao(DaoManager daoManager) {
super(daoManager);
}
public Integer getSequenceUserId(){
return (Integer)queryForObject("getSequenceUserId", new Object());
}
}
import com.ibatis.dao.client.DaoManager;
import com.wysm.netstar.domain.User;
import com.wysm.netstar.persistence.iface.UserDao;
import java.util.List;
import org.apache.log4j.Logger;
public class UserSqlMapDao extends BaseSqlMapDao implements UserDao {
static Logger logger=Logger.getLogger(UserSqlMapDao.class);
public UserSqlMapDao(DaoManager daoManager) {
super(daoManager);
logger.debug("In UserSqlMapDao daoManager null="+(daoManager==null));
}
public List getUser(String userAlias){
return queryForList("getUserByUserAlias", userAlias);
}
public List getUser(String userAlias, String password){
User user=new User(userAlias, password);
return queryForList("getUserByUserAliasAndPassword", user);
}
public void insertUser(User user){
update("insertUser", user);
}
public void updateUser(User user){
update("updateUser", user);
}
public List getUserByEmail(String email){
return queryForList("getUserByEmail", email);
}
public boolean existUserId(Integer userid){
Integer cnt=(Integer)this.queryForObject("existUserId", userid);
return cnt.intValue()>0;
}
}
import com.ibatis.dao.client.DaoManager;
import com.wysm.netstar.persistence.iface.UserInfoDao;
import com.wysm.netstar.domain.UserInfo;
public class UserInfoSqlMapDao extends BaseSqlMapDao implements UserInfoDao {
public UserInfoSqlMapDao(DaoManager daoManager) {
super(daoManager);
}
public UserInfo getUserInfo(Integer userId){
return (UserInfo)queryForObject("getUserInfoByUserId", userId);
}
public void insertUserInfo(UserInfo userInfo){
update("insertUserInfo", userInfo);
}
public void updateUserInfo(UserInfo userInfo){
update("insertUserInfo", userInfo);
}
}
import com.wysm.netstar.domain.User;
public interface UserService {
public void addNewUser(User user); //增加新用户
public boolean existUserAlias(String userAlias); //检查用户别名是否已经存在
public User existUser(String userAlias,String password); //用户密码验证
public boolean existEmail(String email); //检查EMAIL是否已经存在
public boolean existUserId(Integer userId); //用户流水号验证
}
package com.wysm.netstar.service.ibatis;
import java.util.*;
import org.apache.log4j.*;
import com.ibatis.dao.client.*;
import com.wysm.netstar.domain.*;
import com.wysm.netstar.persistence.iface.*;
import com.wysm.netstar.service.*;
public class UserServiceImpl implements UserService {
private UserDao userDao;
private SequenceDao sequenceDao;
private UserInfoDao userInfoDao;
private DaoManager daoMgr;
static Logger logger=Logger.getLogger(UserServiceImpl.class);
public UserServiceImpl() {
// daoMgr = DaoConfig.getDaoManager();
// this.userDao=(UserDao)daoMgr.getDao(UserDao.class);
}
private void initDaos(){
if (userDao==null){
this.userDao=(UserDao)daoMgr.getDao(UserDao.class);
this.sequenceDao=(SequenceDao)daoMgr.getDao(SequenceDao.class);
this.userInfoDao=(UserInfoDao)daoMgr.getDao(UserInfoDao.class);
}
}
public void addNewUser(User user){
Integer id=sequenceDao.getSequenceUserId();
user.setUserId(id);
UserInfo userInfo=new UserInfo();
userInfo.setUserId(id);
userInfo.setLastLogDate(null);
userInfo.setLogTimes(new Integer(0));
userInfo.setRegDate(new Date());
try{
daoMgr.startTransaction(); /* 事务处理 */
userDao.insertUser(user);
userInfoDao.insertUserInfo(userInfo);
daoMgr.commitTransaction();
}catch(Exception ex){
ex.printStackTrace();
}finally{
daoMgr.endTransaction();
}
}
public boolean existUserAlias(String userAlias){
List users=userDao.getUser(userAlias);
if (users!=null&&users.size()>0)
return true;
return false;
}
public User existUser(String userAlias,String password){
List users=userDao.getUser(userAlias,password);
if (users!=null&&users.size()>0)
return (User)users.get(0);
return null;
}
public boolean existEmail(String email){
List users=userDao.getUserByEmail(email);
if (users!=null&&users.size()>0)
return true;
return false;
}
public void setDaoMgr(DaoManager daoMgr) {
this.daoMgr = daoMgr;
}
/* 四个注入方法 */
public void setSequenceDao(SequenceDao sequenceDao) {
this.sequenceDao = sequenceDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setUserInfoDao(UserInfoDao userInfoDao) {
this.userInfoDao = userInfoDao;
}
public boolean existUserId(Integer userId){
if (userId.intValue()==0)
return false;
return userDao.existUserId(userId);
}
}
<!DOCTYPE daoConfig
PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN"
"http://ibatis.apache.org/dtd/dao-2.dtd">
<daoConfig>
<context>
<transactionManager type="SQLMAP">
<property name="SqlMapConfigResource"
value="com/wysm/netstar/persistence/sqlmapdao/sql/sql-map-config.xml"/>
</transactionManager>
<!-- 接口注入-->
implementation="com.wysm.netstar.persistence.sqlmapdao.UserSqlMapDao"/>
<dao inter
implementation="com.wysm.netstar.persistence.sqlmapdao.SequenceSqlMapDao"/>
<dao inter
implementation="com.wysm.netstar.persistence.sqlmapdao.UserInfoSqlMapDao"/>
</context>
</daoConfig>
设定DAO文件的注入,不是很好。可以改在
applicationContext.xml文件中注入,写一个通用的构造方法见注一。
DaoConfig.java改写见文后
|
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import java.io.Reader;
import java.util.Properties;
import org.apache.log4j.Logger;
public class DaoConfig {
private static final String resource = "com/wysm/netstar/persistence/dao.xml";
private static final DaoManager daoManager;
static Logger logger=Logger.getLogger(DaoConfig.class);
static {
try {
daoManager = newDaoManager(null);
} catch (Exception e) {
throw new RuntimeException("Description. Cause: " + e, e);
}
}
public static DaoManager getDaoManager() {
return daoManager;
}
public static DaoManager newDaoManager(Properties props) {
try {
Reader reader = Resources.getResourceAsReader(resource);
return DaoManagerBuilder.buildDaoManager(reader, props);
} catch (Exception e) {
throw new RuntimeException("Could not initialize DaoConfig. Cause: " + e, e);
}
}
}