简单管理系统的开发流程
1、定义好数据层
选择一个存储方式(XML)
创建users.xml
<?xml version="1.0" encoding="UTF-8"?> <users> </users>
2、创建DAO完成对用户对象的CRUD
Data Access Object -->DAO
DAO所做的事情是完成对用户对象的增删改查,常规的说,每一个对象都有一个DAO。
public class UserDao { public static final String USER_XML = "users"; private Document ud; public UserDao() { ud = XMLUtil.getUserDocument(); } private void write() { XMLUtil.write2XML(ud,USER_XML); } private Element loadElementByUsername(String username) { String path = "/users/user[username='"+username+"']"; Element e = (Element)ud.selectSingleNode(path); return e; } private User element2User(Element e) { User u = new User(); u.setNickname(e.elementText("nickname")); u.setPassword(e.elementText("password")); u.setUseranme(e.elementText("username")); return u; } public void add(User user) { if(user.getUseranme()==null||"".equals(user.getUseranme())) throw new ExpException("用户名不能为空"); User u = this.load(user.getUseranme()); if(u!=null) throw new ExpException("用户已经存在!不能继续添加"); Element e = ud.getRootElement().addElement("user"); e.addElement("username").addText(user.getUseranme()); e.addElement("password").addText(user.getPassword()); e.addElement("nickname").addText(user.getNickname()); write(); } public void delete(String username) { //1、获取根节点 Element root = ud.getRootElement(); //2、获取username的节点 Element e = loadElementByUsername(username); root.remove(e); write(); } public void update(User user) { //1、根据username获取节点 Element e = loadElementByUsername(user.getUseranme()); if(e==null) throw new ExpException("要更新的用户不存在"); //2、为这个节点修改数据 e.element("password").setText(user.getPassword()); e.element("nickname").setText(user.getNickname()); write(); } public User load(String username) { Element e = loadElementByUsername(username); if(e==null) return null; User u = element2User(e); return u; } @SuppressWarnings("unchecked") public List<User> list() { String path = "/users/user"; List<Element> eles = ud.selectNodes(path); List<User> users = new ArrayList<User>(); for(Element e:eles) { users.add(element2User(e)); } return users; }
3、编写一个XML的工具类来完成对XML的操作
public class XMLUtil { private static Document userDocument; public static Document getUserDocument() { //如果document存在直接返回 if(userDocument!=null) return userDocument; //如果不存在就创建对象 try { SAXReader reader = new SAXReader(); //System.out.println(XMLUtil.class.getResource("")); //System.out.println(XMLUtil.class.getClassLoader().getResource("")); userDocument = reader.read(XMLUtil.class.getClassLoader().getResourceAsStream("xml/users.xml")); return userDocument; } catch (DocumentException e) { e.printStackTrace(); } return null; } public static void write2XML(Document d,String name) { XMLWriter out = null; try { String path = XMLUtil.class.getClassLoader().getResource("xml/"+name+".xml").getPath(); path = path.replace("bin","src"); out = new XMLWriter(new FileWriter(path),OutputFormat.createPrettyPrint()); out.write(d); } catch (IOException e) { e.printStackTrace(); } finally { try { if(out!=null) out.close(); } catch (IOException e) { e.printStackTrace(); } } }
4、编写XML的单例模式
单例模式指的是所有的对象只有一个,这样可以提高访问的效率。
private static Document userDocument; public static Document getUserDocument() { //如果document存在直接返回 if(userDocument!=null) return userDocument; //如果不存在就创建对象 try { SAXReader reader = new SAXReader(); //System.out.println(XMLUtil.class.getResource("")); //System.out.println(XMLUtil.class.getClassLoader().getResource("")); userDocument = reader.read(XMLUtil.class.getClassLoader().getResourceAsStream("xml/users.xml")); return userDocument; } catch (DocumentException e) { e.printStackTrace(); } return null; }