用spring+hibernate+struts 项目记录以及常用的用法进等
一、hibernate
1.
-----BaseDao------
// 容器注入
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
2.
---通过id查找
public T findById(int id) {
return (T) sessionFactory.getCurrentSession().get(clazz, id);
}
@Override
public void delete(int id) {
sessionFactory
.getCurrentSession()
.createQuery("delete from " + className + " where id=?")
.setParameter(0, id).executeUpdate();
}
3.
---通过id查找
@Override
public Employee findById(int id) {
// String hql = "from Employee e left join fetch e.dept where e.id=?";
String hql = "from Employee e where e.id=?";
return (Employee) getSessionFactory()
.getCurrentSession()
.createQuery(hql)
.setParameter(0, id)
.uniqueResult();
}
4.
-------通过id删除
public void delete(int id) {
sessionFactory
.getCurrentSession()
.createQuery("delete from " + className + " where id=?")
.setParameter(0, id).executeUpdate();
}
-------返回链表
EmployeeDao
@SuppressWarnings("unchecked")
@Override
public List<Employee> getAll(String employeeName) {
return getSessionFactory().getCurrentSession()//
.createQuery("from Employee where empName like ?")//
.setParameter(0, "%" +employeeName + "%")//
.list();
}
@Override
public Employee findById(int id) {
// String hql = "from Employee e left join fetch e.dept where e.id=?";
String hql = "from Employee e where e.id=?";
return (Employee) getSessionFactory()
.getCurrentSession()
.createQuery(hql)
.setParameter(0, id)
.uniqueResult();
}
5.
查找全部的
@Override
public List<Employee> getAll(String employeeName) {
return getSessionFactory().getCurrentSession()//
.createQuery("from Employee where empName like ?")//
.setParameter(0, "%" +employeeName + "%")//
.list();
}
--------下拉链表--------
<!--
Struts下拉列表标签:
name="deptId" 下拉列表标签的名称(服务器根据这个名称获取选择的项的实际的值value值)
headerKey 默认选择项的实际的值
headerValue 默认下拉列表显示的内容
list 下拉列表显示数据的集合
listKey 集合对象的哪个属性作为下拉列表的实例的值,即value值
listValue 集合对象的哪个属性作为下拉列表显示的值
value 默认选择的项的设置
-->
<s:select
name="deptId"
headerKey="-1"
headerValue="请选择"
list="#request.listDept"
listKey="id"
listValue="name"
value="-1"
></s:select>
6.
//struts2的ognl 的iterator 跳出循环
做项目遇到一个需求,前台已经获取到了一个小组所有人的昵称,需要展示出一个管理员的昵称,但是小组中又有普通成员又有管理员,而且管理员不一定只有一个。
在用s:iterator遍历这个小组成员时需要过滤出管理员,然后在找到第一个管理员以后停止循环,下面是我的代码:
[javascript] view plain copy print?
<s:set name="index" value="1" />
<s:iterator value="#detail.members" var="memeber" id="memeber" status="status">
<s:if test='%{#memeber.attr1 == "admin" }'>
<s:if test="#index==1">
<s:set name="index" value="2" />
<s:property value="#memeber.nickName"/>
</s:if>
</s:if>
</s:iterator>
s:if可以合成一个,这里写成两个方便大家理解,这样做的实质没有跳出循环,但是巧妙的控制了循环的次数。
7. request进行传递数据
可以通过这样方式给页面进行传数据,将数据放到栈里。
这块要有这个 Map<String, Object> request;
cn.dy.action.GoodsAction
public class GoodsAction extends ActionSupport implements ModelDriven<Goods>, RequestAware{
// 接收框架运行时候传入的代表request对象的map
private Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
}
8.如何放入值栈
//why为啥 这块要进将数据进行pop ,而后进行push进行
public String viewUpdate(){
// 获取要修改的记录的id
int id = employee.getId();
// 1. 根据员工的主键查询 (lazy="false")
Employee emp = employeeService.findById(id); // 已经有部门信息
// 2. 查询所有的部门
List<Dept> listDept = deptService.getAll();
// 数据回显
ValueStack vs = ActionContext.getContext().getValueStack();
vs.pop(); // 移除栈顶元素
vs.push(emp); // 入栈
// 保存
request.put("listDept", listDept);
return "edit";
}
//不继承BaseDao 的普通dao写法
public class DeptDao implements IDeptDao {
//
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public Dept findById(int id) {
return (Dept) sessionFactory.getCurrentSession().get(Dept.class, id);
}
@SuppressWarnings("unchecked")
@Override
public List<Dept> getAll() {
//查出来的数据放在list
return sessionFactory.getCurrentSession().createQuery("from Dept").list();
}
}
9.在hibernate中,要注意的是,
a.注意:在使用sessionFactory中要十分注意不同类之间不可共用一个sessionFactory ,否则会报 Struts has
* detected an unhandled exception:
* Messages: File: cn/dy/dao/impl/RepertoryDao.java Line number: 48
b.在进行用hibernate中, 要进查找, 特别设计到外键后, 注意, 用jdbc是无法进行完成其外键的查询的
二、spring
1.配置bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引入其他配置文件 -->
<import resource="config/bean-base.xml"/>
<import resource="config/bean-dao.xml"/>
<import resource="config/bean-service.xml"/>
<import resource="config/bean-action.xml"/>
</beans>
2.bean-action.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Action中需要注入Service -->
<!-- 1. 员工管理模块 -->
<bean id="employeeAction" class="cn.dy.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
<property name="deptService" ref="deptService"></property>
</bean>
<!-- 2. 管理员模块 -->
<bean id="adminAction" class="cn.dy.action.AdminAction" scope="prototype">
<property name="adminService" ref="adminService"></property>
</bean>
<!-- 商品的管理模块 -->
<bean id="goodsAction" class="cn.dy.action.GoodsAction" scope="prototype">
<property name="goodsService" ref="goodsService"></property>
<property name="categoryService" ref="categoryService"></property>
<property name="repertoryService" ref="repertoryService"></property>
</bean>
<!-- 入库订单的管理模块 -->
<bean id="storeGdsFormAction" class="cn.dy.action.StoreGdsFormAction" scope="prototype">
<property name="storeGdsFormService" ref="storeGdsFormService"></property>
<property name="goodsServer" ref="goodsService"></property>
<property name="recordStoreGdsServers" ref="recordStoreGdsServers"></property>
</bean>
<!-- 分页显示入库单 -->
<bean id="showSeparatePageStoreGdsAction" class="cn.dy.action.showSeparatePageStoreGdsAction" scope="prototype">
<property name="storeGdsFormService" ref="storeGdsFormService"></property>
</bean>
<!-- 表单ajax 验证 进行注入 -->
<bean id="checkStoreIdAction" class="cn.dy.action.CheckStoreIdAction" scope="prototype">
<property name="storeGdsFormService" ref="storeGdsFormService"></property>
</bean>
<!-- 出库订单的管理模块 -->
<bean id="exportGdsFormAction" class="cn.dy.action.ExportGdsFormAction" scope="prototype">
<property name="exportGdsFormServers" ref="exportGdsFormServers"></property>
<property name="goodsServer" ref="goodsService"></property>
<property name="recordExportGdsServers" ref="recordExportGdsServers"></property>
</bean>
<!-- 报表展现的管理模块 -->
<bean id="reportAction" class="cn.dy.action.ReportAction" scope="prototype">
<property name="reportShowExportDao" ref="reportShowExportDao"></property>
<property name="reportShowStoreDao" ref="reportShowStoreDao"></property>
</bean>
</beans>
3.bean-base.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<!-- 1. 连接池实例 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://192.168.8.134:3306/hib_demo"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
</bean>
<!-- 2. Spring管理SessionFactory 【全部配置都写到spring中】 -->
<!-- # 注入DataSource、 注入常用配置属性、映射配置属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
</props>
</property>
<property name="annotatedClasses">
<list>
<value>cn.dy.entity.UserInfo</value>
<value>cn.dy.entity.Permission</value>
<value>cn.dy.entity.FunctionComponents</value>
<value>cn.dy.entity.Goods</value>
<value>cn.dy.entity.Category</value>
<value>cn.dy.entity.Repertory</value>
<value>cn.dy.entity.StoreGdsForm</value>
<value>cn.dy.entity.ExportGdsForm</value>
<value>cn.dy.entity.RecordExportGds</value>
<value>cn.dy.entity.RecordStoreGds</value>
<value>cn.dy.entity.ShowRecordExportgds</value>
<value>cn.dy.entity.ShowRecordStoreGds</value>
</list>
</property>
</bean>
<!-- 3. 事务相关配置 -->
<!-- 3.1 事务管理器类 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 3.2 事务增强(如何管理事务)-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 3.3 Aop配置 = 切入点表达式(拦截目标对象,生成代理) + 事务增强应用-->
<aop:config>
<aop:pointcut expression="execution(* cn.dy.service.impl.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
4.bean-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Dao 注入 SessionFactory -->
<bean id="adminDao" class="cn.dy.dao.impl.AdminDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="deptDao" class="cn.dy.dao.impl.DeptDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="employeeDao" class="cn.dy.dao.impl.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="goodsDao" class="cn.dy.dao.impl.GoodsDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="repertoryDao" class="cn.dy.dao.impl.RepertoryDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="categoryDao" class="cn.dy.dao.impl.CategoryDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="storeGdsFormDao" class="cn.dy.dao.impl.StoreGdsFormDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="recordStoreGdsDao" class="cn.dy.dao.impl.RecordStoreGdsDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="exportGdsFormDao" class="cn.dy.dao.impl.ExportGdsFormDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="recordExportGdsDao" class="cn.dy.dao.impl.RecordExportGdsDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 报表出库
<bean id="reportShowExportDao" class="cn.dy.dao.impl.ReportShowExportDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->
<!-- 报表入库
<bean id="reportShowStoreDao" class="cn.dy.dao.impl.ReportShowStoreDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->
</beans>
5.bean-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Service 需要注入 Dao -->
<bean id="adminService" class="cn.dy.service.impl.AdminService">
<property name="adminDao" ref="adminDao"></property>
</bean>
<bean id="deptService" class="cn.dy.service.impl.DeptService">
<property name="deptDao" ref="deptDao"></property>
</bean>
<bean id="employeeService" class="cn.dy.service.impl.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="goodsService" class="cn.dy.service.impl.GoodsService">
<property name="goodsDao" ref="goodsDao"></property>
</bean>
<bean id="repertoryService" class="cn.dy.service.impl.RepertoryService">
<property name="repertoryDao" ref="repertoryDao"></property>
</bean>
<bean id="categoryService" class="cn.dy.service.impl.CategoryService">
<property name="categoryDao" ref="categoryDao"></property>
</bean>
<!-- 入库单 -->
<bean id="storeGdsFormService" class="cn.dy.service.impl.StoreGdsFormService">
<property name="storeGdsFormDao" ref="storeGdsFormDao"></property>
</bean>
<!-- 入库单记录表 -->
<bean id="recordStoreGdsServers" class="cn.dy.service.impl.RecordStoreGdsServers">
<property name="recordStoreGdsDao" ref="recordStoreGdsDao"></property>
</bean>
<!-- 出库 单 -->
<bean id="exportGdsFormServers" class="cn.dy.service.impl.ExportGdsFormServers">
<property name="exportGdsFormDao" ref="exportGdsFormDao"></property>
</bean>
<!-- 出库单记录表 -->
<bean id="recordExportGdsServers" class="cn.dy.service.impl.RecordExportGdsServers">
<property name="recordExportGdsDao" ref="recordExportGdsDao"></property>
</bean>
</beans>
用spring+hibernate+struts 项目记录以及常用的用法进等的更多相关文章
- spring+hibernate删除单条记录的几种方法
spring+hibernate删除单条记录的几种方法
- Spring+Hibernate+Struts(SSH)框架整合
SSH框架整合 前言:有人说,现在还是流行主流框架,SSM都出来很久了,更不要说SSH.我不以为然.现在许多公司所用的老项目还是ssh,如果改成流行框架,需要成本.比如金融IT这一块,数据库dao层还 ...
- spring+hibernate+struts整合(1)
spring+hibernate:整合 步骤1:引入类包 如下图:这里是所有的类包,为后面的struts整合考虑
- spring+hibernate+struts整合(2)
spring和struts2的整合 1:配置Web.xml文件 <filter> <filter-name>struts2</filter-name> <fi ...
- Spring,hibernate,struts的面试笔试题及答案
Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation ...
- Spring,hibernate,struts的面试笔试题(含答案)
Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久 ...
- Spring+hibernate+struts
一.Spring 主要功能:解耦和(对象之间可配置,依赖注入的) 1.概念: 容器:容器可以装载对象,实例化对象,配置对象之间的依赖关系. IOC/DIIOC:Inversion of Control ...
- spring,hibernate,struts的面试笔试题
1 Action是不是线程安全的?如果不是 有什么方式可以保证Action的线程安全?如果是,说明原因不是声明局部变量,或者扩展RequestProcessor,让每次都创建一个Action,或者在s ...
- jbpm与spring hibernate struts整合
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
随机推荐
- Java 7 Concurrency Cookbook 翻译 第一章 线程管理之二
三.中断一个线程 一个拥有多个线程的Java程序要结束,需要满足两个条件之一:一是所有的非后台线程都执行结束了:二是某个线程执行了 System.exit() 方法.当你想要终结一个运行中的Java程 ...
- 第五章第二例题关于Vector(LRJ)
vector(动态数组)(粘) 一.概述 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector是一个容器,它能够存放各种类型的对象,简 ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [KOJ95603]全球奥运
[COJ95603]全球奥运 试题描述 一个环形的图中有N个城市,奥运会重要项目就是传递圣火,每个城市有A[i]个圣火,每个城市可以向它相邻的城市传递圣火(其中1号城市可以传递圣火到N号城市或2号城市 ...
- SNMP报文抓取与分析(二)
SNMP报文抓取与分析(二) SNMP报文抓取与分析(二) 1.SNMP报文表示简介 基本编码规则BER 标识域Tag表示 长度域length表示 2.SNMP报文详细分析(以一个get-respon ...
- 应用HTK搭建语音拨号系统1:数据准备
选自:http://maotong.blog.hexun.com/6204849_d.html 应用HTK搭建语音拨号系统--数据准备 苏统华 哈尔滨工业大学人工智能研究室 2006年10月30日 声 ...
- 【MySQL】MySQL server has gone away 怎么处理?
直接上代码: from django.db import connection ... def is_connection_usable(): try: connection.connection.p ...
- ASP.NET MVC 如何解决“上下文的模型已在数据库创建后发生更改”问题
问题描述:支持"XXContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库. 问题解决:坑爹的MVC会 ...
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 学习Selenium2Library的好例子
最近好几个人问我有没有好的例子可以帮助学习Selenium2Library怎么用.对于公司同事,可以把脚本直接给过去,其他人则不行了.所以一直想做一个好的学习样例,这个样例应该有如下特性: 能够非常好 ...