oracle:
thin:
@192.168.0.95:
1521:
ZXDB2"/>
创建sessionFactory工厂
classpath:
hibernate.cfg.xml
考来用
7.创建实体类
就是写一个bean含有get/set方法
8.在实体类所在的目录下,写一个类名.hbm.xml文件,在配置文件中添加属性
xmlversion="1.0"encoding="utf-8"?
>
DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"
主键的类型为Long
SEQ_LB_USER指定主键生成方式,必须在pl/sql创建主键生成方式
所有属性的name必须与bean中的set后的名称一样
9.创建一个Dao实现操作方法
10创建service 调用 Dao中的方法
11.创建 Action 将service的得到的user加到Action中
12.在applicationContext.xml中配置
将sessionFactory注入到userDao中
将UserDao注入到userService中
将userService注入到userAction中
13.在struts.xml中配置Action
/userShow.jsp
此处配置了访问时的路径:
/user/userAdd
14.将8中的配置文件加入到4中
引用配置文件
15.写jsp页面表单提交到Action
9.10.11:
是Action调用Service,Service调用Dao也是分层的体现
Web.xml是web工程的配置文件
ApplicationContext 是spring的配置文件
Hibernate.hbm.xml是hibernate的配置文件
建表是注意创建主键时name为表名
Struts2,Spring,Hibernate整合例子一个(自己笔记)
开发本项目的具体步骤请看:
Struts2,Spring,Hibernate整合步骤
项目目录:
User.java
packagecom.zx.test.model;
publicclassUser{
privateLongid;
privateStringname;
privateIntegerage;
privateStringpassword;
// get/set方法
}
User.hbm.xml
xmlversion="1.0"encoding="utf-8"?
>
DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"
SEQ_LB_USER
UserDao.java
packagecom.zx.test.dao;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
importcom.zx.test.model.User;
publicclassUserDaoextendsHibernateDaoSupport{
/**
*保存user
*@paramuser
*/
publicvoidsaveUser(Useruser){
// spring自带方法保存
this.getHibernateTemplate().save(user);
}
//update,delete,findAll方法同上,省略!
}
UserService.java
packagecom.zx.test.service;
importcom.zx.test.dao.UserDao;
importcom.zx.test.model.User;
publicclassUserService{
privateUserDaouserDao;
publicvoidsetUserDao(UserDaouserDao){
this.userDao=userDao;
}
publicvoidsaveUser(Useruser){
userDao.saveUser(user);
}
//在此处添加业务逻辑方法;
}
UserAction.java
packagecom.zx.test.action;
importcom.opensymphony.xwork2.ActionSupport;
importcom.zx.test.model.User;
importcom.zx.test.service.UserService;
publicclassUserActionextendsActionSupport{
privateUseruser;
publicUsergetUser(){
returnuser;
}
publicvoidsetUser(Useruser){
this.user=user;
}
privateUserServiceuserService;
publicvoidsetUserService(UserServiceuserService){
this.userService=userService;
}
publicStringuserAdd(){
this.userService.saveUser(user);
returnSUCCESS;
}
//添加action方法
}
struts.xml:
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
/userShow.jsp
hibernate.cfg.xml
xmlversion='1.0'encoding='UTF-8'?
>
DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"
--基本配置-->
org.hibernate.dialect.Oracle9Dialect
false
true
true
2
8
20
applicationContext.xml
oracle:
thin:
@192.168.0.95:
1521:
ZXDB2"/>
classpath:
hibernate.cfg.xml
web.xml:
--配置Struts2-->
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
-- 配置Spring -->
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath*:
*.xml
index.jsp:
<