1.什么是三层架构:
三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:界面层(User Interface layer)、业务逻辑层(Business Logic Layer)、数据访问层(Data access layer)。区分层次的目的即为了“高内聚低耦合”的思想。在软件体系架构设计中,分层式结构是最常见,也是最重要的一种结构。微软推荐的分层式结构一般分为三层,从下至上分别为:数据访问层、业务逻辑层(又或称为领域层)、表示层。
2.三层架构的优点:
web层
1>接收数据:本案例不需要接收数据
2>用List接受查询结果(常用于收集查询结果集)
定义一个List集合productList泛型为product用于接收service.findAllProduct();的返回值。
3>传递数据
List<product> productList =service.findAllProduct();
service层
1>传递数据
return dao.findAllCategory();
dao层
1>链接数据库QueryRunner()的有参链接
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List接受查询到的数据
List<product> productList = runner.query(sql, new BeanListHandler<product>(product.class));
数据的回写
return productList;
web层
将查询到被回写的数据放入request域中并转发到jsp中进行显示
request.setAttribute("productList", productList);
request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);
3.关于数据的封装和回写
1>List<String>,List<User> Map<String, String> Map<String, User>的赋值和遍历
//1)遍历strList<String>
List<String> strList = new ArrayList<String>();
strList.add("一");
strList.add("二");
strList.add("三");
strList.add("四");
request.setAttribute("strList", strList);
//2)遍历List<User>的值
List<User> userList = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setName("lisi");
user1.setPassword("1234");
userList.add(user1);
User user2 = new User();
user2.setId(2);
user2.setName("wangwu");
user2.setPassword("123456");
userList.add(user2);
request.setAttribute("userList", userList);
//3)遍历Map<String,String>的值
Map<String, String> strMap = new HashMap<String, String>();
strMap.put("key1", "一");
strMap.put("key2", "二");
strMap.put("key3", "三");
strMap.put("key4", "四");
request.setAttribute("strMap", strMap);
//4)遍历Map<String,User>的值
Map<String, User> userMap = new HashMap<String, User>();
userMap.put("key1", user1);
userMap.put("key2",user2);
request.setAttribute("userMap", userMap);
遍历(jstl)
<h1>取出strList数据</h1>
<c:forEach items="${strList}" var="str">
${str}<br>
</c:forEach>
<h1>取出userList数据</h1>
<c:forEach items="${userList}" var="user">
${user.id}<br>
${user.name}<br>
${user.password}<br>
</c:forEach>
<h1>取出strMap数据</h1>
<c:forEach items="${strMap}" var="strMap">
${strMap.key}=${strMap.value}<br>
</c:forEach>
<h1>取出userMap数据</h1>
<c:forEach items="${userMap}" var="userMap">
${userMap.key }=${userMap.value.name}=${userMap.value.password}<br>
</c:forEach>
2>Map<String,String>封装数据
Map<String, String[]> properties = request.getParameterMap();
product product = new product();
try {
//将properties中的数据封装到product对象中
BeanUtils.populate(product, properties);
} catch (IllegalAccessException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3>手动的封装product中自己想要的数据
product.setPimage("products/1/c_0033.jpg");
domain:product
package com.hdh.domain;
public class product {
// `pid` varchar(32) NOT NULL,
// `pname` varchar(50) DEFAULT NULL,
// `market_price` double DEFAULT NULL,
// `shop_price` double DEFAULT NULL,
// `pimage` varchar(200) DEFAULT NULL,
// `pdate` date DEFAULT NULL,
// `is_hot` int(11) DEFAULT NULL,
// `pdesc` varchar(255) DEFAULT NULL,
// `pflag` int(11) DEFAULT NULL,
// `cid` varchar(32) DEFAULT NULL,
private String pid;
private String pname;
private double market_price;
private double shop_price;
private String pimage;
private String pdate;
private int is_hot;
private String pdesc;
private int pflag;
private String cid;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getMarket_price() {
return market_price;
}
public void setMarket_price(double market_price) {
this.market_price = market_price;
}
public double getShop_price() {
return shop_price;
}
public void setShop_price(double shop_price) {
this.shop_price = shop_price;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public String getPdate() {
return pdate;
}
public void setPdate(String pdate) {
this.pdate = pdate;
}
public int getIs_hot() {
return is_hot;
}
public void setIs_hot(int is_hot) {
this.is_hot = is_hot;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
public int getPflag() {
return pflag;
}
public void setPflag(int pflag) {
this.pflag = pflag;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
}