谈谈对Struts多模块的熟习[Java编程]
本文“谈谈对Struts多模块的熟习[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
首先,建一个web project,然后增添对hibernate和spring的支持,我们利用的是hibernate3.1和spring2.0,然后导入dwr.jar和commons-pool-1.3.jar(不知道myeclipse怎么搞的,增添Spring功效支持的时刻就有了commons-dbcp.jar,竟然没有其依靠的commons-pool-x.jar,只好单独增添了,别的,需求将Spring2.0 AOP Liberaries里的asm2.2.3.jar删除,因为仿佛和Hiberate中的生成代理用的asm.jar冲突,我把Spring2.0 AOP Liberaries排到最后仍旧有冲突,所以只好删掉了,不知道大家碰到过这种情形么).
我们利用myeclise自带的Derby数据库,在里面建一个表BOOK:
ID bigint primary key,autoincrement
NAME varchar(20)
ISBM varchar(20)
AUTHOR varchar(15)
然后操纵myeclipse的hibernate反向工程生成范畴模子:Book.java, DAO:BookDAO.jar, Book 的映射文件Book.hbm.xml:
生成的代码及配置文件以下:
Book.java:
package edu.jlu.fuliang.domain;
/** *//**
* Book generated by MyEclipse Persistence Tools
*/
public class Book implements java.io.Serializable {
// Fields
private Long id;
private String name;
private String isbm;
private String author;
// Constructors
/** *//** default constructor */
public Book() {
}
/** *//** minimal constructor */
public Book(Long id, String name, String isbm) {
this.id = id;
this.name = name;
this.isbm = isbm;
}
/** *//** full constructor */
public Book(Long id, String name, String isbm, String author) {
this.id = id;
this.name = name;
this.isbm = isbm;
this.author = author;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbm() {
return this.isbm;
}
public void setIsbm(String isbm) {
this.isbm = isbm;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public String toString(){
return "[id=" + id + ",name=" + name + ",isbm=" + isbm + ",author=" + author + "]";
}
}
edu.jlu.fuliang.dao.BookDAO.java:
package edu.jlu.fuliang.dao;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import edu.jlu.fuliang.domain.Book;
/** *//**
* Data access object (DAO) for domain model class Book.
*
* @see edu.jlu.fuliang.domain.Book
* @author MyEclipse Persistence Tools
*/
public class BookDAO extends HibernateDaoSupport ...{
private static final Log log = LogFactory.getLog(BookDAO.class);
// property constants
public static final String NAME = "name";
public static final String ISBM = "isbm";
public static final String AUTHOR = "author";
protected void initDao() ...{
// do nothing
}
public void save(Book transientInstance) ...{
log.debug("saving Book instance");
try ...{
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) ...{
log.error("save failed", re);
throw re;
}
}
public void delete(Book persistentInstance) ...{
log.debug("deleting Book instance");
try ...{
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) ...{
log.error("delete failed", re);
throw re;
}
}
public Book findById(java.lang.Long id) ...{
log.debug("getting Book instance with id: " + id);
try ...{
Book instance = (Book) getHibernateTemplate().get(
"edu.jlu.fuliang.domain.Book", id);
return instance;
} catch (RuntimeException re) ...{
log.error("get failed", re);
throw re;
}
}
public List findByExample(Book instance) ...{
log.debug("finding Book instance by example");
try ...{
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) ...{
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) ...{
log.debug("finding Book instance with property: " + propertyName
+ ", value: " + value);
try ...{
String queryString = "from Book as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) ...{
log.error("find by property name failed", re);
throw re;
}
}
public List findByName(Object name) ...{
return findByProperty(NAME, name);
}
public List findByIsbm(Object isbm) ...{
return findByProperty(ISBM, isbm);
}
public List findByAuthor(Object author) ...{
return findByProperty(AUTHOR, author);
}
public List findAll() ...{
log.debug("finding all Book instances");
try ...{
String queryString = "from Book";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) ...{
log.error("find all failed", re);
throw re;
}
}
public Book merge(Book detachedInstance) ...{
log.debug("merging Book instance");
try ...{
Book result = (Book) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) ...{
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Book instance) ...{
log.debug("attaching dirty Book instance");
try ...{
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) ...{
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Book instance) ...{
log.debug("attaching clean Book instance");
try ...{
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) ...{
log.error("attach failed", re);
throw re;
}
}
public static BookDAO getFromApplicationContext(ApplicationContext ctx) ...{
return (BookDAO) ctx.getBean("BookDAO");
}
}
Book.hbm.xml:
< ?xml version="1.0" encoding="utf-8"?>
< !DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
< !--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
< hibernate-mapping>
< class name="edu.jlu.fuliang.domain.Book" table="BOOK" schema="CLASSICCARS">
< id name="id" type="java.lang.Long">
< column name="ID" />
< generator class="identity" />
< /id>
< property name="name" type="java.lang.String">
< column name="NAME" length="20" not-null="true" />
< /property>
< property name="isbm" type="java.lang.String">
< column name="ISBM" length="20" not-null="true" unique="true" />
< /property>
< property name="author" type="java.lang.String">
< column name="AUTHOR" length="15" />
< /property>
< /class>
< /hibernate-mapping>
以上是“谈谈对Struts多模块的熟习[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |