关于对javaUtils封装和三层架构的笔记
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;
}
}