<!----><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock><v:shape id="_x0000_s1026" type="#_x0000_t75" wrapcoords="-90 0 -90 21459 21600 21459 21600 0 -90 0" style="MARGIN-TOP: 67.65pt; Z-INDEX: -1; LEFT: 0px; MARGIN-LEFT: 73.5pt; WIDTH: 179.25pt; POSITION: absolute; HEIGHT: 114.75pt; TEXT-ALIGN: left"><v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.jpg" o:title="spring+hibernate"></v:imagedata><w:wrap type="tight"></w:wrap></v:shape>
<v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape id="_x0000_s1026" type="#_x0000_t75" wrapcoords="-90 0 -90 21459 21600 21459 21600 0 -90 0" style="MARGIN-TOP: 5.4pt; Z-INDEX: -1; LEFT: 0px; MARGIN-LEFT: 0px; WIDTH: 179.25pt; POSITION: absolute; HEIGHT: 114.75pt; TEXT-ALIGN: left"><v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\01\clip_image001.jpg" o:title="spring+hibernate"></v:imagedata><w:wrap type="tight"></w:wrap></v:shape>à总体结构布局
à存放所有文件的包
àStudent POJO
àStudent DAO
àDAO 实现类
à实现类测试
àStudent数据表映射文件
àApplicationContext.xml配置文件
<o:p> </o:p>
1.首先加入我们的框架支持<o:p></o:p>
先Spring后hibernate,将hibernate-cfg.xml整合到applicationContext中,注意sessionFactory和dataSource文件的创建。
2.从数据表中建立我们需要的POJO DAO POJO.hbm.xml文件<o:p></o:p>
①POJO<o:p></o:p>
package all;
public class Student implements java.io.Serializable {
private String id;
private String name;
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
…..setter()…..getter()…..
}
②DAO<o:p></o:p>
package all;<o:p></o:p>
<o:p> </o:p>
import java.util.List;<o:p></o:p>
import org.apache.commons.logging.Log;<o:p></o:p>
import org.apache.commons.logging.LogFactory;<o:p></o:p>
import org.hibernate.LockMode;<o:p></o:p>
import org.springframework.context.ApplicationContext;<o:p></o:p>
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;<o:p></o:p>
public class StudentDAO extends HibernateDaoSupport {<o:p></o:p>
private static final Log log = LogFactory.getLog(StudentDAO.class);<o:p></o:p>
public static final String NAME = "name";<o:p></o:p>
public static final String AGE = "age";<o:p></o:p>
protected void initDao() {<o:p></o:p>
//do nothing<o:p></o:p>
}<o:p></o:p>
//保存数据<o:p></o:p>
public void save(Student transientInstance) {<o:p></o:p>
log.debug("saving Student instance");<o:p></o:p>
try {<o:p></o:p>
getHibernateTemplate().save(transientInstance);<o:p></o:p>
log.debug("save successful");<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("save failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
//删除对象<o:p></o:p>
public void delete(Student persistentInstance) {<o:p></o:p>
log.debug("deleting Student instance");<o:p></o:p>
try {<o:p></o:p>
getHibernateTemplate().delete(persistentInstance);<o:p></o:p>
log.debug("delete successful");<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("delete failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
//按照ID查询对象<o:p></o:p>
public Student findById( java.lang.String id) {<o:p></o:p>
log.debug("getting Student instance with id: " + id);<o:p></o:p>
try {<o:p></o:p>
Student instance = (Student) getHibernateTemplate()<o:p></o:p>
.get("all.Student", id);<o:p></o:p>
return instance;<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("get failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
//按照例子查询对象<o:p></o:p>
public List findByExample(Student instance) {<o:p></o:p>
log.debug("finding Student instance by example");<o:p></o:p>
try {<o:p></o:p>
List results = getHibernateTemplate().findByExample(instance);<o:p></o:p>
log.debug("find by example successful, result size: " + results.size());<o:p></o:p>
return results;<o:p></o:p>
} catch (RuntimeException re) {<o:p></o:p>
log.error("find by example failed", re);<o:p></o:p>
throw re;<o:p></o:p>
}<o:p></o:p>
} <o:p></o:p>
//照一个属性的名字和对应的值查询对象<o:p></o:p>
public List findByProperty(String propertyName, Object value) {<o:p></o:p>
log.debug("finding Student instance with property: " + propertyName<o:p></o:p>
+ ", value: " + value);<o:p></o:p>
try {<o:p></o:p>
String queryString = "from Student as model where model." <o:p></o:p>
+ propertyName + "= ?";<o:p></o:p>
return
分享到:
相关推荐
struts+spring+hibernate整合
struts + spring + hibernate + velocity + ajax + jotm + acegi
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
一个spring+struts+hibernate的例子,是eclipse的工程,用tomcat5和mysql,文件夹下包含所有的源码和库,另外还有一个.sql的文件用于建立数据库。大家觉得它有什么不好,欢迎交流
有关Struts2+Spring+Hibernate和Struts2+Spring+Ibatis的整合实例demo 原创,完全基于eclipse开发
Struts+Spring+Hibernate 整合教程Struts+Spring+Hibernate 整合教程Struts+Spring+Hibernate 整合教程Struts+Spring+Hibernate 整合教程
计算机毕业设计成品 JAVA JSP 车辆管理系统struts+hibernate+spring (源码+WORD文档) 计算机毕业设计成品 JAVA JSP 车辆管理系统struts+hibernate+spring (源码+WORD文档) 计算机毕业设计成品 JAVA JSP 车辆管理...
Struts2+Spring3+Hibernate4+Maven+EasyUI整合视频教程源码
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
struts+spring+hibernate整合源码 struts+spring+hibernate整合源码struts+spring+hibernate整合源码struts+spring+hibernate整合源码
本书重点介绍如何整合Struts 2.2+Spring 3.0+Hibernate 3.6进行Java EE开发,主要包括三部分,第一部介绍Java EE开发的基础知识,以及如何搭建开发环境。第二部分详细讲解Struts 2.2、Spring 3.0和Hibernate 3.6三...
ssh三大框架的整合步骤,非常详细。包含spring整合struts、spring整合hibernate、声明式事务说明等。
SSH+Redis整合
Struts1+Spring2+Hibernate2整合详细例子
Struts2.3.16.1+Hibernate3.6.10+Spring3.2.8整合 能够运行,没有任何问题 另外附带applicationContext.xml、struts.xml、hibernate.cfg.xml
maven整合spring+hibernate+struts2
struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版 struts2 的方式自己去看简单。 spring2.5 是用注释来注入 hibernate3.2 是用ejb3注解映射关系 hibernate3 +个属性可以自动...
struts+hibernate+spring+xdoclet+ant+log4j 技术整合,有实例参考,最好的框架!
一个简单的spring+struts2+hibernate+mybatis整合(数据库脚本放在项目资源文件的sql目录下) 因为没想好mvc用springmvc好,还是struts2好 所以没有整合进去
Struts+Spring+Hibernate整合Struts+Spring+Hibernate整合