for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,与JDBC批量设置相同 //flush a batch of inserts and release memory: //将本批插入的对象立即写入数据库并释放内存 session.flush(); session.clear(); } }
看看hibernate或者jdbc吧:)
批量更新建议你用jdbc。表现层--控制器--业务逻辑层(service--持久层--数据库)
一个大体的流程,structs只管前两个,后面的压根不管,自己去实现你要的业务逻辑吧。如果懒惰的话,用jstl来做吧,一个标准的taglib封装,sun大力提倡的。它提供了sql的功能,可以在页面上写taglib来实现你想要的。
-------------------------------------------
用标签库来显示啊,在ACTION里面得到你要的结果集合,再通过REQUEST或其它对象在前台VIEW页面上获得,显示出来就OK呀。。
讲的很详细,很清晰,一看就会!
专门有一节讲:批量更新,批量插入,等。
=====================================================================================
14.1. 批量插入(Batch inserts)
如果要将很多对象持久化,你必须通过经常的调用 flush() 以及稍后调用 clear() 来控制第一级缓存的大小。 Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size //20,与JDBC批量设置相同
//flush a batch of inserts and release memory:
//将本批插入的对象立即写入数据库并释放内存
session.flush();
session.clear();
}
}
tx.commit();
session.close();
14.2. 批量更新(Batch updates)
此方法同样适用于检索和更新数据。此外,在进行会返回很多行数据的查询时, 你需要使用 scroll() 方法以便充分利用服务器端游标所带来的好处。 Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
ScrollableResults customers = session.getNamedQuery("GetCustomers")
.setCacheMode(CacheMode.IGNORE)
.scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while ( customers.next() ) {
Customer customer = (Customer) customers.get(0);
customer.updateStuff(...);
if ( ++count % 20 == 0 ) {
//flush a batch of updates and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
就像已经讨论的那样,自动和透明的 对象/关系 映射(object/relational mapping)关注于管理对象的状态。 这就意味着对象的状态存在于内存,因此直接更新或者删除 (使用 SQL 语句 UPDATE 和 DELETE) 数据库中的数据将不会影响内存中的对象状态和对象数据。 不过,Hibernate提供通过Hibernate查询语言(第 15 章 HQL: Hibernate查询语言)来执行大批 量SQL风格的(UPDATE)和(DELETE) 语句的方法。 UPDATE 和 DELETE语句的语法为: ( UPDATE | DELETE ) FROM? ClassName (WHERE WHERE_CONDITIONS)?。 有几点说明: 在FROM子句(from-clause)中,FROM关键字是可选的 在FROM子句(from-clause)中只能有一个类名,并且它不能有别名 不能在大批量HQL语句中使用连接(显式或者隐式的都不行)。不过在WHERE子句中可以使用子查询。 整个WHERE子句是可选的。 举个例子,使用Query.executeUpdate()方法执行一个HQL UPDATE语句: Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction(); String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();
执行一个HQL DELETE,同样使用 Query.executeUpdate() 方法 (此方法是为 那些熟悉JDBC PreparedStatement.executeUpdate() 的人们而设定的) Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction(); String hqlDelete = "delete Customer where name = :oldName";
int deletedEntities = s.createQuery( hqlDelete )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();
由Query.executeUpdate()方法返回的整型值表明了受此操作影响的记录数量。 注意这个数值可能与数据库中被(最后一条SQL语句)影响了的“行”数有关,也可能没有。一个大批量HQL操作可能导致多条实际的SQL语句被执行, 举个例子,对joined-subclass映射方式的类进行的此类操作。这个返回值代表了实际被语句影响了的记录数量。在那个joined-subclass的例子中, 对一个子类的删除实际上可能不仅仅会删除子类映射到的表而且会影响“根”表,还有可能影响与之有继承关系的joined-subclass映射方式的子类的表
如果你用hibernate的话,可以用
Session session = sessionFactory.openSession();
Connection conn=session.getConnection();
取得数据库的连接,然后用jdbc来实现处理。否则,直接用updateBatch或者update table set a=? where b=?来处理即可。
而是页面的form和actionform的对应
因为他们是一对一的关系 现在是多对一的关系 要怎么样才能实现这个对应Struts已经支持了,在BeanUtil中实现的
页面代码:
<html:text property="subjects"/>
<html:text property="subjects"/>
.......
<html:text property="subjects"/>
ActionForm中定义属性:
String[] subjects 即可
这样子不知道行不行~~
不知道这样可不可以
我们希望ActionForm能够接收Map数据,这样我们的管理就变的容易多啦,在Struts 1.1以后版本这样的处理变得非常简单,我们在ActionForm中声明一个Map变量。
public class MapForm extends ActionForm
{
private Map map = null;
public void setMap(Map map) {
this.map = map;
}
public Map getMap() {
return this.map;
}
同时增加一个属性方法去设置和获取Map中的数据。
public void setAttribute(String attributeKey, Object attributeValue)
{
getMap().put(attributeKey, attributeValue);
}
public Object getAttribute(String attributeKey)
{
Object keyValue = getMap().get(attributeKey);
return keyValue;
}
这样我们在jsp页面中,我们就可以使用Struts的标签接触这些Map数据。
<html:text property="attribute(key)"/>
这样这些数据就可维护啦,这对查询条件较多的情况非常适用,你无需在维护这些查询信息在各个页面的过渡,Struts帮您完成了一切。下面我们就看一下如何用MapForm组织master/detail方式的数据,我们将以一个订单做为样例。1 首先建立一个Form对象,继承MapForm,同时声明主要的属性,如订单编码、定购人等。
public class OrderForm extends MapForm
{
private Integer id;
private String orderMan;2 我们拟定以Map方式保存采购项信息,同一采购项采用统一前缀,可选择行编码,如row123_ productCode,row123_ productId,row123_ amount等,这样某一采购项信息将被输入到Map中,不同的采购项的前缀不一样,前缀由row+行编码组成,同时编写可获取行编码的函数,这样可取得某一采购项的所有信息,参数rowPrefix为某一字符串,如“row”、“item”等,不包含数字编码信息,同时你可以编写Comparator,进行排序。
public Collection getRowIdList(String rowPrefix)
{
if (map.isEmpty()) return new ArrayList();
Collection allRowId = new TreeSet(new RowIdComparator(rowPrefix));
Iterator allKey = map.keySet().iterator();
while (allKey.hasNext())
{
String key = (String) allKey.next();
if (key.indexOf(rowPrefix) != -1)
{
key = key.substring(0, key.indexOf('_'));
allRowId.add(key);
}
}
return allRowId;
}3 在jsp页面中你可以通过jstl,就可以完成采购明细的显示。
<c:forEach var="rowId" items="${OrderForm.getRowIdList('row')}">
<tr align="center" id="${rowId}" onclick="clickRow()">
<td ><html:text property="attribute(${rowId}_productCode)" size="8" onkeydown="fillProductInfoWithKeyDown('${rowId}',this)" /><html:hidden property="attribute(${rowId}_productId)"/> <a href="javascript:selectproduct('${rowId}')">选择</a></td>
<td ><html:text property="attribute(${rowId}_productQty)" size="8" /> </td>
<td ><html:text property="attribute(${rowId}_productPrice)" size="8" /></td>
<td ><html:text property="attribute(${rowId}_productName)" readonly="true" size="16" /></td>
<td ><html:text property="attribute(${rowId}_productPackaging)" readonly="true" size="12"/></td>
<td ><html:text property="attribute(${rowId}_productUnit)" size="6" readonly="true" /></td>
</tr>
</c:forEach>4 这样Struts帮你完成了所有的信息处理,你需要完成你的保存就可以啦。提示:Map中的数据值默认都是String类型的,如果你想转换成你想要的类型,可以在你的Form编写一个工具方法,完成类型的转换。
public Map getTypedMap() {
Map map = this.getMap();
String keyString = (String) map.get("key");
Integer keyInteger = new Integer(keyString);
map.put("key",keyInteger);
return map;
}总结:MapForm各个功能很少被开发人员使用,如果使用得当,功能非常强大。ActionForm个人认为并非是个设计缺陷,结合BeanUtils等工具包,转换和信息处理非常方便。向楼上这个用数组的方法,也不算太好,不过也基本可行,但是是有局限性地.
CRUD操作应该在service层去做。和struts有什么关系。
批量更新的数据 这应该是搂住的意思