sqlserver:
//localhost:
1433;databaseName=db_bookMana">
4.3各个子功能的主要代码
4.3.1读者信息界面
(1)读者信息添加
核心代码:
UserAction.Java
publicStringuserAdd()
{
Stringsql="fromTUserwhereuserDel='no'anduserJiehao='"+userJiehao+"'";
ListuserList=userDAO.getHibernateTemplate().find(sql);
if(userList.size()>0)
{
this.setMessage("借书证号已存在。
请重新录入");
this.setPath("admin/user/userAdd.jsp");
return"succeed";
}//判断借书证号是否存在,存在返回重新输入,不存在返回succeed
TUseruser=newTUser();
user.setUserJiehao(userJiehao);
user.setUserRealname(userRealname);
user.setUserAddress(userAddress);
user.setUserSex(userSex);
user.setUserTel(userTel);
user.setUserAge(userAge);
user.setUserEmail(userEmail);
user.setUserDel("no");
userDAO.save(user);
this.setMessage("操作成功");
this.setPath("userMana.action");
return"succeed";
}
TUserDAO.java
publicvoidsave(TUsertransientInstance)
{//对操作进行保存
log.debug("savingTUserinstance");
//完成DAO对象的插入数据操作
try{
getHibernateTemplate().save(transientInstance);
log.debug("savesuccessful");
}catch(RuntimeExceptionre){
log.error("savefailed",re);
throwre;}
}
(2)读者信息查询和删除
核心代码:
UserAction.Java
publicStringuserDel()
{//定义删除
TUseruser=userDAO.findById(userId);
user.setUserDel("yes");
userDAO.attachDirty(user);
this.setMessage("删除成功");
this.setPath("userMana.action");
return"succeed";
}
publicStringuserMana()
{ListuserList=userDAO.findAll();///从数据库里面取出所有user
Maprequest=(Map)ServletActionContext.getContext().get("request");
//在Action里面获取request对象一种方法
request.put("userList",userList);
returnActionSupport.SUCCESS;
}
publicStringuserDetail()
{
TUseruser=userDAO.findById(userId);
Maprequest=(Map)ServletActionContext.getContext().get("request");
request.put("user",user);
returnActionSupport.SUCCESS;
}
TUserDAO.java
publicvoiddelete(TUserpersistentInstance)
{//定义删除方法
log.debug("deletingTUserinstance");
try
{
getHibernateTemplate().delete(persistentInstance);
log.debug("deletesuccessful");
}catch(RuntimeExceptionre)
{
log.error("deletefailed",re);
throwre;}
}
publicListfindAll()
{//定义查找方法
log.debug("findingallTUserinstances");
try
{
StringqueryString="fromTUserwhereuserDel='no'";
returngetHibernateTemplate().find(queryString);
}catch(RuntimeExceptionre)
{
log.error("findallfailed",re);
throwre;
}
}
4.3.2密码修改
核心代码:
adminAction.java
publicStringadminAdd()
{
TAdminadmin=newTAdmin();
admin.setUserName(userName);
admin.setUserPw(userPw);
adminDAO.save(admin);
this.setMessage("操作成功");
this.setPath("adminManage.action");
return"succeed";
}
TAdminDAO.java
publicvoidsave(TAdmintransientInstance){
log.debug("savingTAdmininstance");
try{
getHibernateTemplate().save(transientInstance);
log.debug("savesuccessful");
}catch(RuntimeExceptionre){
log.error("savefailed",re);
throwre;
}
}
//判断密码是否正确、新密码是否为空
functioncheck(){
varuserPwReal="${sessionScope.admin.userPw}";
if(document.formPw.userPw.value!
=userPwReal)
{alert("原密码不正确");return;}
if(document.formPw.userPw1.value==""){
alert("新密码不能空");return;}document.getElementById("indicator").style.display="block";
loginService.adminPwEdit(document.formPw.userPw1.value,callback);
}functioncallback(data){
document.getElementById("indicator").style.display="none";alert("修改成功");}
4.3.3管理员信息查询与删除
核心代码:
adminAction.java
publicStringadminManage()
{//用户管理
ListadminList=adminDAO.findAll();
Maprequest=(Map)ServletActionContext.getContext().get("request");
request.put("adminList",adminList);
returnActionSupport.SUCCESS;
}
publicStringadminDel()
{
adminDAO.delete(adminDAO.findById(userId));
this.setMessage("删除成功");
this.setPath("adminManage.action");
return"succeed";
}
TAdminDAO.java
publicvoiddelete(TAdminpersistentInstance){//定义删除方法
log.debug("deletingTAdmininstance");
try{
getHibernateTemplate().delete(persistentInstance);
log.debug("deletesuccessful");
}catch(RuntimeExceptionre){
log.error("deletefailed",re);
throwre;
}
}
publicTAdminfindById(java.lang.Integerid){//定义查询
log.debug("gettingTAdmininstancewithid:
"+id);
try{
TAdmininstance=(TAdmin)getHibernateTemplate().get(
"com.model.TAdmin",id);
returninstance;
}catch(RuntimeExceptionre){
log.error("getfailed",re);
throwre;
}
}
publicListfindByProperty(StringpropertyName,Objectvalue){
//根据外键值Name进行查询,显示
log.debug("findingTAdmininstancewithproperty:
"+propertyName
+",value:
"+value);
try{
StringqueryString="fromTAdminas