需求:模拟前端后台登录注册的效果(MVC模式)
准备工作:
1.搭建开发环境
1.1 导入开发包
*dom4j.jar
*jstl.jar
*beanUtils.jar
*log4j.jar
1.2 创建组织程序的包
*com.Cecilia.domain
*com.Cecilia.dao
*com.Cecilia.dao.impl
*com.Cecilia.service
*com.Cecilia.service.impl
*com.Cecilia.web.controller(处理请求的servlet)
*com.Cecilia.web.UI
*com.Cecilia.utils
*junit4.test(测试)
1.3 创建代表数据库的xml
MVC模式图
实现过程
*Dao层:
1.首先从Domain实体类开始,登录注册需要用户相关信息,因此构建User类。
User类:
/**
* 实体类
* @author 芷若初荨
*
*/
public class User {
private String id;
private String username;
private String password;
private String nickname;
private String email;
private Date birthday;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2.由于这次只是使用XML模拟数据库存储数据的效果,因此构建一个XMLUtils工具类,实现获取文件和将获得数据写入XML文件中。
XmlUtils类:
/**
* 工具类
* @author 芷若初荨
*
*/
public class XmlUtils {
private static String filepath;
//静态代码块,只执行一次
static{
filepath=XmlUtils.class.getClassLoader().getResource("users.xml").getPath();
}
//获取文件
public static Document getDocument() throws Exception{
SAXReader reader=new SAXReader();
Document document=reader.read(new File(filepath));
return document;
}
//将文件写入XML中
public static void write2Xml(Document document) throws IOException, FileNotFoundException{
//获得格式化器对象
OutputFormat format=OutputFormat.createPrettyPrint();
//设置编码
format.setEncoding("UTF-8");
//获得写入流
XMLWriter writer=new XMLWriter(new FileOutputStream(filepath),format);
//写到XML文件中
writer.write(document);
//美化代码,使之变得紧凑
format=OutputFormat.createCompactFormat();
writer=new XMLWriter(System.out,format);
writer.write(document);
}
}
3.针对User对象,接下来就是实现具体操作,在这里可以先构建构建实现类,然后再抽取实现类为接口,也可以交换顺序
UserDaoImpl类:
/**
* 实现类
* @author 芷若初荨
*
*/
public class UserDaoImpl implements UserDao {
//添加功能
@Override
public void add(User user){
try {
Document document=XmlUtils.getDocument();
//添加节点
Element root=document.getRootElement();
Element user_tag=root.addElement("root");
user_tag.setAttributeValue("id",user.getId());
user_tag.setAttributeValue("username",user.getUsername());
user_tag.setAttributeValue("password",user.getPassword());
user_tag.setAttributeValue("email",user.getEmail());
user_tag.setAttributeValue("birthday",user.getBirthday()==null?"":user.getBirthday().toString());
user_tag.setAttributeValue("nickname",user.getNickname());
//写到XML文件中
XmlUtils.write2Xml(document);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
//查询功能
@Override
public User find(String username,String password){
try {
Document document=XmlUtils.getDocument();
//搜索
Element e=(Element) document.selectSingleNode("//user[@username+']"+username+"and @password='"+password+"']");
if(e==null){
return null;
}
User user=new User();
String date=e.attributeValue("brthday");
if(date==null||date.equals("")){
user.setBirthday(null);
}else{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(sdf.parse(date));
}
user.setEmail(e.attributeValue("email"));
user.setId(e.attributeValue("id"));
user.setPassword(e.attributeValue("password"));
user.setUsername(e.attributeValue("username"));
user.setNickname(e.attributeValue("nickname"));
return user;
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
//查询指定用户在注册表中是否存在
@Override
public boolean check(String username){
Document document;
try {
document = XmlUtils.getDocument();
Element e=(Element) document.selectSingleNode("//user[@username+']"+username+"']");
if(e==null){
return false;
}
} catch (Exception e1) {
// TODO Auto-generated catch block
throw new RuntimeException(e1);
}
return true;
}
}
UserDao接口:
package com.Cecilia.dao;
/**
* 抽出方法成接口
*/
import com.Cecilia.domain.User;
public interface UserDao {
//添加功能
void add(User user);