182云笔记项目Word格式.docx
《182云笔记项目Word格式.docx》由会员分享,可在线阅读,更多相关《182云笔记项目Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
intaddUser(Useruser);
2.声明SQLUserMappeer.xml
<
insertid="
addUser"
parameterType="
cn.tedu.note.entity.User"
>
insertintocn_user(cn_user_id,cn_user_name,cn_user_password,cn_user_token,cn_user_nick)values(#{id},#{name},#{password},#{token},#{nick})
/insert>
3.测试UserDaoTest:
UserDaodao;
@BeforepublicvoidinitDao(){
dao=ctx.getBean("
userDao"
UserDao.class);
@Test
publicvoidtestAddUser(){
Stringid=UUID.randomUUID().toString();
Stringname="
Tom"
;
Stringsalt="
今天你吃了吗?
"
Stringpassword=DigestUtils.md5Hex(salt+"
123456"
);
Stringtoken="
Stringnick="
Useruser=newUser(id,name,password,token,nick);
intn=dao.addUser(user);
System.out.println(n);
2.业务层
1.声明业务层方法UserService
/***UserService中添加注册功能
*@paramname*@paramnick*@parampassword*@paramconfirm*@return注册成功的用户信息*@throwsUserNameException用户名异常
*@throwsPasswordException密码异常
*/Userregist(Stringname,Stringnick,Stringpassword,Stringconfirm)throwsUserNameException,PasswordException;
2.声明业务层异常UserNameException
publicclassUserNameExceptionextendsRuntimeException{privatestaticfinallongserialVersionUID=6435296194529486206L;
publicUserNameException(){}publicUserNameException(Stringmessage){super(message);
}publicUserNameException(Throwablecause){super(cause);
}publicUserNameException(Stringmessage,Throwablecause){super(message,cause);
}publicUserNameException(Stringmessage,Throwablecause,booleanenableSuppression,booleanwritableStackTrace){super(message,cause,enableSuppression,writableStackTrace);
}}
3.重构jdbc.properties和UserServiceImpl,将salt存储到配置文件,利用Spring注入到属性中:
#jdbc.propertiessalt=\u4ECA\u5929\u4F60\u5403\u4E86\u5417?
//UserServiceImpl@Value("
#{jdbc.salt}"
)privateStringsalt;
4.实现业务层方法:
UserServiceImpl
publicUserregist(Stringname,Stringnick,Stringpassword,Stringconfirm)throwsUserNameException,PasswordException{
//检查name,不能重复
if(name==null||name.trim().isEmpty()){thrownewUserNameException("
不能空"
Userone=userDao.findUserByName(name);
if(one!
=null){thrownewUserNameException("
已注册"
//检查密码
if(password==null||password.trim().isEmpty()){thrownewPasswordException("
if(!
password.equals(confirm)){thrownewPasswordException("
确认密码不一致"
//检查nickif(nick==null||nick.trim().isEmpty()){nick=name;
Stringid=UUID.randomUUID().toString();
Stringtoken="
password=DigestUtils.md5Hex(salt+password);
intn=userDao.addUser(user);
if(n!
=1){thrownewRuntimeException("
添加失败!
}returnuser;
5.测试:
UserServiceTest
UserServiceservice;
@Before
publicvoidinitService(){service=ctx.getBean("
userService"
UserService.class);
publicvoidtestRegist(){Useruser=service.regist("
Andy"
"
System.out.println(user);
3.控制器
1.添加控制器方法UserController
@RequestMapping("
/regist.do"
)
@ResponseBody
publicJsonResultregist(Stringname,Stringnick,Stringpassword,Stringconfirm){
Useruser=userService.regist(name,nick,password,confirm);
returnnewJsonResult(user);
2.测试
http:
//localhost:
8080/note/user/regist.do?
name=Jerry&
nick=AN&
password=12345&
confirm=12345
4.添加注册JS脚本
1.更新log_in.html取消页面检查js脚本:
window.onload=function(){
vart=setTimeout("
get('
zc'
).style.visibility='
visible'
800);
//get('
final_password'
).onblur=function(){
//varnpassword=get('
regist_password'
).value;
//varfpassword=get('
//if(npassword!
=fpassword){
//get('
warning_3'
).style.display='
block'
//}//}//get('
).value.length;
//if(npassword<
6&
&
npassword>
0){
warning_2'
).onfocus=function(){//get('
none'
//}//get('
//}}
2.添加注册对话框事件脚本login.js
#regist_button'
).click(registAction);
#regist_username'
).blur(checkRegistName);
$('
#regist_password'
).blur(checkRegistPassword);
#final_password'
).blur(checkConfirm);
3.添加注册对话框数据检验方法:
login.js
functioncheckConfirm(){varpwd2=$('
).val();
varpwd=$('
//pwd如果是空值表示false,非空则是true
if(pwd&
pwd==pwd2){
).next().hide();
returntrue;
}$('
).next().show().find('
span'
).html('
确认密码不一致'
returnfalse;
}functioncheckRegistPassword(){
).val().trim();
varrule=/^\w{4,10}$/;
if(rule.test(pwd)){$('
4~10个字符'
}functioncheckRegistName(){
varname=$('
if(rule.test(name)){$('
4~10字符'
4.添加注册对话框注册按钮事件方法login.js
functionregistAction(){console.log('
registAction'
//检验界面参数varn=checkRegistName()+checkRegistPassword()+checkConfirm();
if(n!
=3){return;
//获取界面中表单数据
varnick=$('
#nickname'
varpassword=$('
varconfirm=$('
//发起AJAX请求
varurl='
user/regist.do'
vardata={name:
name,nick:
nick,password:
password,confirm:
confirm};
//console.log(data);
//$.post是$.ajax的简化版
$.post(url,data,function(result){console.log(result);
if(result.state==0){//退回登录界面$('
#back'
).click();
varname=result.data.name;
).val(name);
).focus();
//清空表单$('
).val('
'
}elseif(result.state==4){$('
).html(result.message);
}elseif(result.state==3){$('
}else{alert(result.message);
}});
//得到响应以后,更新界面}
5.重构控制器UserController增加事件处理方法:
@ExceptionHandler(UserNameException.class)
@ResponseBodypublicJsonResulthandleUserName(UserNameExceptione){e.printStackTrace();
returnnewJsonResult(4,e);
6.测试...
调试工具:
笔记本列表功能
1.添加持久层接口NotebookDao:
publicinterfaceNotebookDao{List<
Map<
String,Object>
findNotebooksByUserId(StringuserId);
2.添加Mapper文件:
NotebookMapper.xml
mappernamespace="
cn.tedu.note.dao.NotebookDao"
selectid="
findNotebooksByUserId"
string"
resultType="
map"
selectcn_notebook_idasid,cn_notebook_nameasnamefromcn_notebookwherecn_user_id=#{userId}orderbycn_notebook_createtimedesc
/select>
/mapper>
3.测试:
publicclassNotebookDaoTestextendsBaseTest{NotebookDaodao;
@BeforepublicvoidinitDao(){dao=ctx.getBean("
notebookDao"
NotebookDao.class);
}@Test//selectcn_user_idfromcn_notebook;
publicvoidtestFindNotebooksByUserId(){StringuserId="
52f9b276-38ee-447f-a3aa-0d54e7a736e4"
List<
list=dao.findNotebooksByUserId(userId);
for(Map<
map:
list){System.out.println(map);
}}}
提示:
需要先到数据库中获得拥有笔记的用户IDuserId
selectcn_user_idfromcn_notebook;
List封装查询结果:
1.声明业务接口NotebookService
publicinterfaceNotebookService{List<
listNotebooks(StringuserId)throwsUserNotFoundException;
2.实现业务接口NotebookServiceImpl
@Service("
notebookService"
)publicclassNotebookServiceImplimplementsNotebookService{@ResourceprivateNotebookDaonotebookDao;
@ResourceprivateUserDaouserDao;
publicList<
listNotebooks(StringuserId)throwsUserNotFoundException{if(userId==null||userId.trim().isEmpty()){thrownewUserNotFoundException("
ID不能空"
}Useruser=userDao.findUserById(userId);
if(user==null){thrownewUserNotFoundException("
用户不存在"
}returnnotebookDao.findNotebooksByUserId(userId);
3.添加查询方法UserDao
UserfindUserById(StringuserId);
4.添加SQLUserMapper
findUserById"
selectcn_user_idasid,cn_user_nameasname,cn_user_passwordaspassword,cn_user_tokenastoken,cn_user_nickasnickfromcn_userwherecn_user_id=#{userId}<
publicclassNotebookServiceTestextendsBaseTest{NotebookServiceservice;
@BeforepublicvoidinitService(){service=ctx.getBean("
NotebookService.class);
}@TestpublicvoidtestListNotebooks(){StringuserId="
list=service.listNotebooks(userId);
笔记本列表
1.添加控制器父类AbstractController,封装公共的异常处理方法:
publicabstractclassAbstractController{/***在其他控制器方法执行出现异常时候,执行*异常处理方法handleException*/@ExceptionHandler(Exception.class)@ResponseBodypublicObjecthandleException(Exceptione){e.printStackTrace();
returnnewJsonRes