SSH小例子

来源:互联网 发布:男声变女声软件中文版 编辑:程序博客网 时间:2024/06/05 20:53

配置:

hibernate-spring.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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"default-lazy-init="false"><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="oracle.jdbc.OracleDriver"/>  <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>  <property name="user" value="hzy"/>  <property name="password" value="accp"/>   <property name="maxPoolSize" value="40"/>   <property name="minPoolSize" value="1"/>  <property name="initialPoolSize" value="1"/>  <property name="maxIdleTime" value="20"/>   </bean> <bean id="MySf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property>  <property name="mappingResources">      <list>               <value>org/hzy/entity/Dept.hbm.xml</value>      </list>  </property>  <property name="hibernateProperties"> <props>   <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>  <prop key="hibernate.hbm2ddl.auto">update</prop>   <!-- <prop key="hibernate.current_session_context_class">thread</prop> --><prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop><prop key="hibernate.show_sql">true</prop>   <prop key="hibernate.format_sql">true</prop>  </props>     </property> </bean><bean id="transactionManager"        class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="MySf" />    </bean>        <tx:advice id="txAdvice" transaction-manager="transactionManager">    <tx:attributes>    <tx:method name="*" propagation="REQUIRED"/>    </tx:attributes>    </tx:advice>        <aop:config>    <aop:pointcut id="target" expression="execution(* org.hzy.services.impl.DeptService.*(..))"/>    <aop:advisor advice-ref="txAdvice" pointcut-ref="target"/>    </aop:config></beans>

beans.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:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"default-lazy-init="false" default-autowire="no"><import resource="hibernate-spring.xml"/><bean abstract="true" id="BaseDao" class="org.hzy.dao.impl.BaseDao"><property name="sf" ref="MySf"/></bean><bean id="DeptDao" class="org.hzy.dao.impl.DeptImpl" parent="BaseDao"/><bean id="Ds" class="org.hzy.services.impl.DeptService"><property name="ideptdao" ref="DeptDao"/></bean><bean id="login_action" scope="prototype" class="org.hzy.actions.LoginAction"><property name="is" ref="Ds" /></bean><!--<bean id="LogAop" class="org.hzy.aop.LoggAop"><property name="sf" ref="MySessionFactory"/></bean>--><!-- <aop:config><aop:pointcut id="target" expression="execution(* org.hzy.services.impl.DeptService.*(..))"/><aop:advisor advice-ref="LogAop" pointcut-ref="target"/></aop:config> --></beans>

注:action的scope是prototype

struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.i18n.encoding" value="UTF-8" /><constant name="struts.objectFactory" value="spring" /><package name="pack1" namespace="/" extends="struts-default"><action name="Login-*" method="{1}" class="login_action"><!--对应beans.xml中的action的Id--><result name="ok">success.jsp</result></action></package></struts>
web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans1.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter>  <filter-name>encoding_filter</filter-name>  <filter-class>org.accp.CharsetFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>encoding_filter</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>  <filter>  <filter-name>f</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>f</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

Dept.hbm.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="org.hzy.entity.Dept" table="DEPT" schema="SCOTT" dynamic-insert="true">        <id name="deptno" type="java.lang.Integer">            <column name="DEPTNO" precision="2" scale="0" />            <generator class="assigned">            </generator>        </id>        <property name="dname" type="java.lang.String">            <column name="DNAME" length="14" />        </property>        <property name="loc" type="java.lang.String">            <column name="LOC" length="13" />        </property>     </class></hibernate-mapping>   

Dept.java
package org.hzy.entity;import javax.persistence.Column;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.OneToMany;/** * Dept entity. @author MyEclipse Persistence Tools */public class Dept implements java.io.Serializable {// Fieldsprivate Integer deptno;private String dname;private String loc;//private Set emps=new HashSet();  //hibernate会自己实例化变成持久化集合persientset//private List emps=new ArrayList();//private Map emps=new HashMap();// Constructors//public Set getEmps() {//return emps;//}////public void setEmps(Set emps) {//this.emps = emps;//}/** default constructor */public Dept() {}/** minimal constructor */public Dept(Integer deptno) {this.deptno = deptno;}/** full constructor */public Dept(Integer deptno, String dname, String loc) {this.deptno = deptno;this.dname = dname;this.loc = loc;}// Property accessorspublic Integer getDeptno() {return this.deptno;}public void setDeptno(Integer deptno) {this.deptno = deptno;}public String getDname() {return this.dname;}public void setDname(String dname) {this.dname = dname;}public String getLoc() {return this.loc;}public void setLoc(String loc) {this.loc = loc;}}

DeptDaoImpl.java(接口省略)

public class DeptImpl extends BaseDao<Dept> implements IDeptDao{public Dept findDeptById(Integer id) {// TODO Auto-generated method stubDept d=super.get_object(id);return d;}}

DeptServiceImpl.java(接口省略)

public class DeptService implements IDeptService{private IDeptDao ideptdao;public IDeptDao getIdeptdao() {return ideptdao;}public void setIdeptdao(IDeptDao ideptdao) {this.ideptdao = ideptdao;}public Dept findDept(Integer id) {// TODO Auto-generated method stub//super.getSf().getCurrentSession().beginTransaction();return ideptdao.findDeptById(id);//super.getSf().getCurrentSession().getTransaction().commit();}
LoginAction.java

package org.hzy.actions;import org.hzy.entity.Dept;import org.hzy.services.IDeptService;import org.hzy.vos.DeptVo;public class LoginAction {private IDeptService is;private DeptVo dv;public IDeptService getIs() {return is;}public void setIs(IDeptService is) {this.is = is;}public DeptVo getDv() {return dv;}public void setDv(DeptVo dv) {this.dv = dv;}public String login(){System.out.println("login!!!!!!!!!!!!!!!!!!!!!!!");if(is.findDept(dv.getId())!=null){return "ok";}return "error";}}









原创粉丝点击