java数据集合赋值与读取

在网站开发时数据是网站的核心内容。数据如何存储也是关键的处理部分,数据库肯定是数据存储的首选之地,但如果所有数据都存储到数据库,业务处理时总需要对数据库进行读写,数据库服务器压力是非常大的。有些数据属于中间数据,是不需要写进数据库的,可以采用缓存或者会话保存。如用户登录时,可以将登陆的用户名存到session中;用户购物时选择的商品信息,在确定支付结算成功之前的数据如果不是刻意用于用户行为分析,都可以看作为中间过程数据,可以临时保存在session中。session生命周期:默认30分钟没有使用,服务器会自动销毁session。实际在开发过程中还可以结合缓存redis或者memcache来实现临时数据的存储。

(1)登录状态session保存

//在session中存储用户名
session.setAttribute("username", username);
	
	//读取session中的用户名
String username=(String)session.getAttribute("username");

(2)用户业务数据集合保存

建立业务实体如book:

package com.dao;

public class book {
  private String bookname;
  private Integer bookID;
  private Double bookprice;
public String getBookname() {
	return bookname;
}
public void setBookname(String bookname) {
	this.bookname = bookname;
}
public Integer getBookID() {
	return bookID;
}
public void setBookID(Integer bookID) {
	this.bookID = bookID;
}
public Double getBookprice() {
	return bookprice;
}
public void setBookprice(Double bookprice) {
	this.bookprice = bookprice;
}
}

在首页点击相关书籍的购买操作时,购买的书籍信息就是临时中间数据,如果写进数据库,显然读写要耗很长时间,而且到支付结算成功之间用户还可以随时更改撤销。目前解决方案就是暂存在session对象中。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*" import="com.dao.book"%>
<jsp:useBean id="books" class="com.dao.bookDao" scope="page"/> 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>welcome to the bookstore</title>
</head>
<body>
<%
//JAVABEAN: 从数据库查询获得所有书籍列表信息,并以hashmap结构保存到allbook对象
HashMap allbook=(HashMap)books.getAllBooks();
%>
<table border="1" width="800px" align="center">
  <tr>
    <td>书名</td>
    <td>价格</td>
    <td>操作</td>
  </tr>
  <% 	 
    Iterator ite=allbook.keySet().iterator(); //遍历实体key
    while(ite.hasNext()){ //如果存在下一个key时
    	int bookid=(Integer)ite.next();//获得当前key值
    	book book=(book)allbook.get(bookid); //调用实体的get方法获得book实例对象
  %>
 <tr>
     <td><%=book.getBookname() %></td>  
       <td><%=book.getBookprice() %></td>    
   <td><a href="buybook.jsp?bookid=<%=book.getBookID()%>">购买</a></td>
    
  </tr>  
 <% } %>
</table>
</body>
</html>

可以看到其中书籍集合采用的HashMap集合保存。

各种集合包括:

List、Array、set、map、property

定义方式:

    private List<String> list1;
	private Set<String> set1;
	private Map<String,String> map;
	private String[] array;
    private Properties props;

赋值方式:这里使用springIOC容器给各种集合类型注入值

<!--  set方式赋值 -->
    <property name="list">
       <list>
          <value>football</value>
          <value>basketball</value>
       </list>
    </property>
	<property name="array">
	    <array>
	       <value>English</value>
	       <value>Chinese</value>
	    </array>
	</property>
	<property name="set">
	   <set>
	      <value>Tianjin</value>
	      <value>Beijing</value>
	   </set>
	</property>
	<property name="map">
	   <map>
	      <entry>
	         <key>
	            <value>11</value>
	         </key>
	         <value>python</value>
	       </entry>
	       <entry>
	         <key>
	            <value>12</value>
	         </key>
	         <value>java</value>
	       </entry>
	   </map>
	</property>
	<property name="pros">
	   <props>
	     <prop key="course1">math</prop>
	    <prop key="course2">computer</prop>
	    <prop key="course3">ai</prop>
	   </props>
	</property>

读取集合数据方式:

读取list集合的值,这里的List<String>中String是集合类型,也可以设置为对象类型

	public static void main(String[] args) {
		// TODO Auto-generated method stub
      ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
      allCollectionDemo stu=(allCollectionDemo)context.getBean("collectionDemo");
      List<String> arr=stu.getList();
      for(int i=0;i<arr.size();i++){
    	  System.out.println(arr.get(i));
      }      
      
	}

读取array集合的值,这里的String[]是集合类型,也可以设置为对象类型

 String[] arr=stu.getArray();
      for(int i=0;i<arr.length;i++){
    	  System.out.println(arr[i]);
      }

读取map集合的值,这里Map<string,string>中两个String均是数据类型,也可以设置为对象类型

  Map<String,String> arr=stu.getMap();
      Set set=arr.keySet(); 
      Iterator ite=set.iterator();
      while(ite.hasNext()){
    	  System.out.println(ite.next());
      }

读取Set集合的值

  Set<String> arr=stu.getSet();
      Iterator ite=arr.iterator();
      while(ite.hasNext()){
    	  System.out.println(ite.next());
      }

读取properties集合的值:

Properties arr=stu.getPros();     
System.out.println(arr.getProperty("course1"));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值