1. 程式人生 > >關於對javaUtils封裝和三層架構的筆記

關於對javaUtils封裝和三層架構的筆記

tpi 結果集 admin str 轉發 pack pop 封裝數據 實現

1.什麽是三層架構:

三層架構(3-tier architecture) 通常意義上的三層架構就是將整個業務應用劃分為:界面層(User Interface layer)、業務邏輯層(Business Logic Layer)、數據訪問層(Data access layer)。區分層次的目的即為了“高內聚低耦合”的思想。在軟件體系架構設計中,分層式結構是最常見,也是最重要的一種結構。微軟推薦的分層式結構一般分為三層,從下至上分別為:數據訪問層、業務邏輯層(又或稱為領域層)、表示層。

2.三層架構的優點:

1、開發人員可以只關註整個結構中的其中某一層; 2、可以很容易的用新的實現來替換原有層次的實現;
3、可以降低層與層之間的依賴; 4、有利於標準化; 5、利於各層邏輯的復用。 6、結構更加的明確 7、在後期維護的時候,極大地降低了維護成本和維護時間 3.三層架構案例分析:

技術分享圖片

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;
	}

}

關於對javaUtils封裝和三層架構的筆記