用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 ...
随机推荐
- 修复 VirtualBox 下 Ubuntu 14.10 屏幕分辨率问题
在 Windows 7 下使用 VirtualBox 安装了一个 Ubuntu 14.10 后,碰到了一个 640×480 屏幕分辨率的问题. 在 ‘Display Settings' 设置界面的 ‘ ...
- Unity3D中脚本的执行顺序和编译顺序
http://www.cnblogs.com/champ/p/execorder.html 在Unity中可以同时创建很多脚本,并且可以分别绑定到不同的游戏对象上,它们各自都在自己的生命周期中运行.与 ...
- C++虚函数、虚继承、对象内存模型(转)
参考:http://blog.csdn.net/hxz_qlh/article/details/14633361 需要注意的是虚继承.多重继承时类的大小.
- GIT文件的三种状态
对于任何一个文件,在 Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged).已提交表示该文件已经被安全地保存在本地数据库 中了:已修改表示修改了某 ...
- 3.2---最小栈(CC150)
//思路:入栈时不是最小值,永远都没机会成为最小值. import java.util.Stack; class MinStack { private Stack<Integer> sta ...
- 浅谈python的import
动态加载模块: 使用__import__ c=__import__('sys') 等价于 import sys 不过前者可以在执行时决定. 静态加载: 也就是常规的import from xxx im ...
- 【架构】Google的大规模集群管理工具Borg
参考资料: http://www.cnblogs.com/YaoDD/p/5374393.html http://www.cnblogs.com/YaoDD/p/5351589.html
- JavaScript——特殊点总结
首先,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 下面分别说明: 先说 === ...
- 7 天玩转 ASP.NET MVC — 第 3 天
目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 我们假定你在开始学习时已经阅读了前两天的学习内容.在第 2 天我们完成了关于显示 Employee ...
- Design Elevator
From: https://discuss.leetcode.com/topic/89/write-elevator-program-using-event-driven-programming/9 ...