简单介绍下流程:浏览器发出请求,交给servlet
,servlet
收到请求后调用service
组件进行处理,处理产生数据,数据从哪儿来,这里就调用dao
,dao
专门与数据库进行交互,数据一定要封装,在javabean
组件中, servlet
不适合作输出,转交给jsp
,会把javabean
存在域(request
)里,jsp从request域里取出javabean
的数据进行显示,将结果打给浏览器。
为了将来层与层之间进行维护,会在层与层之间定义接口,dao
接口和service
接口,service
层对dao
接口进行调用,dao
层对dao
接口进行实现,将来dao
层换了,不影响service
层。servlet
接口也是一样的原理。
一、规划
>搭建开发环境
1.1 导入开发包
dom4j开发包
jstl开发包
beanUtils开发包
log4j开发包
1.2 创建组织程序的包
com.bird.domain
com.bird.dao
com.bird.dao.impl
com.bird.service
com.bird.service.impl
com.bird.web.controller
com.bird.web.ui
com.bird.utils
junit.test
WEB-INF/jsp保存所有jsp
1.3 创建代表数据库的XML文件
在类目录下创建一个代表数据库的XML文件
二、充当数据库的user.xml
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user id="234343434" username="aaa" password="123" email="aa@sina.com" birthday="1900-09-18" nickname="强子" />
</users>
三、工具类的Dom4j用来解析XML文档
package cn.itcast.utils;
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
//3
public class XmlUtils {
private static String filePath;
static {
filePath = XmlUtils.class.getClassLoader().getResource("users.xml").getPath();
filePath = filePath.replace("%20", " ");
}
public static Document getDocument() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(filePath));
return document;
}
public static void write2Xml(Document document) throws Exception {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format);
writer.write(document);
writer.close();
}
}
四、封装数据的JavaBean
package cn.itcast.domain;
import java.util.Date;
//1
public class User {
private String id;
private String username;
private String password;
private String email;
private Date birthday;
private String nickname;
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
五、Dao层实现的开发
package cn.itcast.dao.impl;
import java.text.SimpleDateFormat;
import org.dom4j.Document;
import org.dom4j.Element;
import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import cn.itcast.utils.XmlUtils;
//2
public class UserDaoImpl implements UserDao {
public void add(User user) {
try {
Document document = XmlUtils.getDocument();
Element root = document.getRootElement();
Element user_tag = root.addElement("user");
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("nickname", user.getNickname());
user_tag.setAttributeValue("birthday", user.getBirthday() == null ? "" : user.getBirthday().toLocaleString());
XmlUtils.write2Xml(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
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();
user.setUsername(e.attributeValue("username"));
user.setNickname(e.attributeValue("nickname"));
user.setPassword(e.attributeValue("password"));
user.setEmail(e.attributeValue("email"));
user.setId(e.attributeValue("id"));
String date = e.attributeValue("birthday");
if (date == null || date.equals("")) {
user.setBirthday(null);
} else {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(df.parse(date));
}
return user;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//查找注册的用户是否在数据库中存在
public boolean find(String username) {
try {
Document document = XmlUtils.getDocument();
Element e = (Element) document.selectSingleNode("//user[@username='" + username + "']");
if (e == null)
return false;
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
六、测试类
package junit.test;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.domain.User;
import org.junit.Test;
import java.util.Date;
public class UserDaoTest {
@Test
public void testAdd() {
User user = new User();
user.setBirthday(new Date());
user.setEmail("chenx@163.com");
user.setId("333333333");
user.setNickName("小静");
user.setPasswWord("cxjcxj");
user.setUserName("陈小镜");
UserDaoImpl userDao = new UserDaoImpl();
userDao.addUser(user);
}
@Test
public void testFind() {
User user = new User();
UserDaoImpl userDao = new UserDaoImpl();
userDao.findUser("陈小镜", "cxjcxj");
}
@Test
public void testFindName() {
UserDaoImpl dao = new UserDaoImpl();
dao.findUser("陈小镜");
}
}
biubiu
连下篇:http://www.jianshu.com/p/2121014cbaef
后面将会持续更新,敬请期待吧!!!
网友评论