用户注册-总结2(Dao层)

package com.du.utils;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlTool {
	
	private static String filepath;
	static{
		filepath=XmlTool.class.getClassLoader().getResource("users.xml").getPath();
		//filepath = XmlUtils.class.getClassLoader().getResource("users.xml").getPath();
	}
	//读取文档
	public static Document getDcument() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(filepath);
        return document;
    }
	//写入分档
	public static void write2Xml(Document document) throws IOException{
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream(filepath), format );
        writer.write( document );
        writer.close();
	}
}

 

注意:

1.要写入和读取xml 要用到dom4j开发包。

 

Dao层:

import java.text.SimpleDateFormat;
import java.util.Date;

import org.dom4j.Document;
import org.dom4j.Element;


import com.du.dao.UserDao;
import com.du.domain.Users;
import com.du.utils.XmlTool;

public class UserDaoImpl implements UserDao {

	// 存入user 到xml
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.du.dao.impl.UserDao#add(com.du.domain.Users)
	 */
	public void add(Users user) {
		try {
			Document document = XmlTool.getDcument();
			Element root = document.getRootElement();
			Element e = root.addElement("user");
			e.setAttributeValue("id", user.getId());
			e.setAttributeValue("username", user.getUsername());
			e.setAttributeValue("password", user.getPassword());
			e.setAttributeValue("email", user.getEmail());
			e.setAttributeValue("birthday", user.getBirthday() == null ? null
					: user.getBirthday().toLocaleString());
			e.setAttributeValue("nickname", user.getNickname());

			XmlTool.write2Xml(document);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}

	}

	// 根据用户名和秘密查找出这个用户的所有信息
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.du.dao.impl.UserDao#find(java.lang.String, java.lang.String)
	 */
	public Users find(String username, String password) {
		try {
			Document document = XmlTool.getDcument();
			Element e = (Element) document
					.selectSingleNode("//user[@username='" + username
							+ "' and @password='" + password + "']");
			if (e == null) {
				return null;
			}
			Users user = new Users();
			String date = e.attributeValue("birthday");
			if (date == null || date.equals("")) {
				user.setBirthday(null);
			} else {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date d = sdf.parse(date);
				user.setBirthday(d);
			}
			user.setId(e.attributeValue("id"));
			user.setNickname(e.attributeValue("nickname"));
			user.setEmail(e.attributeValue("email"));
			user.setUsername(username);
			user.setPassword(password);
			return user;
		} catch (Exception e) {
			new RuntimeException(e);
		}
		return null;
	}

	// 查询xml中是否有同 用户名的人
	// 用于判断是否可以添加到数据库(xml)
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.du.dao.impl.UserDao#find(java.lang.String)
	 */
	public boolean find(String username) {
		try{
			Document document =XmlTool.getDcument();
			Element e = (Element) document.selectSingleNode("//user[@username='"+username+"']");
			if(e==null){
				return false;
			}
			return true;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
}
 

注意:

1.不会写的时候看dom4j的开发文档中的快速入门

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值