使用Struts,spring和HibernateXML方式进行整合
来源:互联网 发布:mac删除bootcamp文件 编辑:程序博客网 时间:2024/05/14 23:03
书写持久层
public class Dept { private Integer deptno; private String deptname; public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; }}
持久层对应的配置文件
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.happy.bean"> <class name="Dept" table="Dept"> <id name="deptno" column="deptno"> <generator class="native"></generator> </id> <property name="deptname"></property> </class></hibernate-mapping>
书写dao层接口
public interface DeptDao { public void addDept(Dept dept);}dao层接口的实现类
public class DeptImpl implements DeptDao { private SessionFactory sessionFactory; public void addDept(Dept dept) { Session session = sessionFactory.getCurrentSession(); session.save(dept); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }}
书写service层
public interface DeptService { public void addDept(Dept dept);}
service层接口的实现
@Transactionalpublic class DeptServiceImpl implements DeptService { private DeptImpl dao; public void addDept(Dept dept) { dao.addDept(dept); } public DeptImpl getDao() { return dao; } public void setDao(DeptImpl dao) { this.dao = dao; }}
action层定义DeptAction
public class DeptAction extends ActionSupport { private Dept dept; private DeptService service; public String Hello(){ service.addDept(dept); return SUCCESS; } public DeptService getService() { return service; } public void setService(DeptService service) { this.service = service; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; }
编写配置文件
applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!--数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="jdbcUrl" value="${jdbc.url}"/> </bean> <!--jdbc.properties--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--2.SessionFactory 类:Local--> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <!--hibernate.xxxxxx必须以hibernate--> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <!--with current thread bind session和线程绑定的session--> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> </props> </property> <!--扫描小配置文件 所有的hbm文件--> <property name="mappingDirectoryLocations" value="classpath:cn/happy/bean"></property> </bean> <!--3.dao--> <bean id="deptDAO" class="cn.happy.dao.impl.DeptImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--4.service--> <bean id="deptService" class="cn.happy.service.impl.DeptServiceImpl"> <property name="dao" ref="deptDAO"></property> </bean> <!--要用Spring去创建Action对象 Struts2的Action是多例的--> <bean id="deptAction" class="cn.happy.action.DeptAction" scope="prototype"> <property name="service" ref="deptService"></property> </bean> <!-- 6.事务管理器--> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--7.事务--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>
数据库链接的jdbc.properties文件
jdbc.driverClassName=oracle.jdbc.OracleDriverjdbc.url=jdbc:oracle:thin:@localhost:1521:orcljdbc.username=rootjdbc.password=rootstruts文件struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.devMode" value="true"></constant> <!--将对象工厂的生成权设置成spring--> <!-- <constant name="struts.objectFactory" value="spring"></constant>--> <package name="default" namespace="/" extends="struts-default"> <action name="add" class="deptAction" method="Hello"> <result>/index.jsp</result> </action> </package></struts>编写WEB-INF下的web.xml
web.xml主要作用就是在程序启动时加载配置文件,添加过滤器和拦截器
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--1.监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
简单的测试add.jsp和index.jsp如果添加成功直接跳转到页面index
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>添加页面</title></head><body><form action="/add" method="post"> 部门名称:<input name="dept.deptname"/> <input type="submit"/></form></body></html>index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>成功</title></head><body><h2>添加成功!!!</h2></body></html>
阅读全文
0 0
- 使用Struts,spring和HibernateXML方式进行整合
- 使用注解方式进行spring和hibernate整合
- 使用注解方式进行spring和hibernate整合
- 使用注解方式进行spring和hibernate整合
- spring+struts整合方式
- struts spring 整合方式
- 整合 Struts 和 Spring
- struts和spring整合
- 整合 Struts 和 Spring
- 整合 Struts 和 Spring
- Spring和Struts整合
- 整合spring 和struts
- spring和struts整合
- spring 和 struts 整合
- struts 和spring 整合
- 整合Struts和Spring
- Spring和Struts整合
- 使用struts、hibernate、spring进行框架整合流程详细说明
- 互联网高可用架构技术实践
- iOS开发畅销书作者Erica Sadun的书籍作品
- 获取access_token中参数问题
- 第一个JAVA小项目(太阳系行星运动模型)
- 画子图(add_subplot & subplot)
- 使用Struts,spring和HibernateXML方式进行整合
- 论钓鱼和互动营销的相似之处
- 揭秘C可变参数原理
- GitForWindows的基本安装和配置
- 原生Js实现图片轮播
- Fiori 代码规范0---导读
- 数据结构学习记录-链式队列
- Java Web 应用所需的外部包都应放在 WebRoot->WEB-INF->lib下
- 编写高性能js注意点