ssh三层架构

来源:互联网 发布:北京大学网络教育 编辑:程序博客网 时间:2024/06/10 00:45


注:

注解必须提供除springhibernet的正常的包外还需要相应的三个jar

*********************************webxml************************

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0">

 <display-name>Demotext</display-name>

 <welcome-file-list>

   <welcome-file>index.html</welcome-file>

   <welcome-file>index.htm</welcome-file>

   <welcome-file>index.jsp</welcome-file>

   <welcome-file>default.html</welcome-file>

   <welcome-file>default.htm</welcome-file>

   <welcome-file>default.jsp</welcome-file>

 </welcome-file-list>

<!-过滤器->

 <filter>

   <filter-name>OSIV</filter-name>

   <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

 </filter>

 <filter-mapping>

   <filter-name>OSIV</filter-name>

   <url-pattern>/*</url-pattern>

 </filter-mapping>

<!-监听器->

 <listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

<!-配置监听的路径->

 <context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:beans.xml</param-value>

 </context-param>

<!-核心过滤器->

 <filter>

   <filter-name>MyFilter</filter-name>

   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

 </filter>

 <filter-mapping>

   <filter-name>MyFilter</filter-name>

   <url-pattern>/*</url-pattern>

 </filter-mapping>

</web-app>

*******************Beans.xml************************

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:p="http://www.springframework.org/schema/p"

   xmlns:aop="http://www.springframework.org/schema/aop"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="

   http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context-3.1.xsd

   http://www.springframework.org/schema/aop

   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

   http://www.springframework.org/schema/tx

   http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

 

 

   <beanid="dataSource"

      class="org.apache.commons.dbcp.BasicDataSource">

      <propertyname="url"

          value="jdbc:oracle:thin:@localhost:1521:orcl">

      </property>

      <propertyname="username"value="textdemo"></property>

      <propertyname="password"value="textdemo"></property>

      <propertyname="driverClassName"value="oracle.jdbc.OracleDriver"></property>

   </bean>

   <beanid="sessionFactory"

      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

      <propertyname="dataSource">

          <refbean="dataSource"/>

      </property>

      <propertyname="hibernateProperties">

          <props>

             <propkey="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>

             <propkey="hibernate.show_sql">true</prop>

             <propkey="hibernate.format_sql">true</prop>

          </props>

      </property>

      

      

      

   <!--使用扫描(装配) -->

      <!--扫描实体类 -->

      <propertyname="packagesToScan"value="com.libo.po"/>

   </bean>

   <!--管理bean自动装配(扫描) -->

   <context:component-scanbase-package="com.libo"></context:component-scan>

   

   <beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">

      <!--需要哪个sessionFactory使用到事务 -->

      <propertyname="sessionFactory"ref="sessionFactory"></property>

   </bean>

   

   <!--开启事务注解 -->

   <tx:annotation-driventransaction-manager="transactionManager"/>

   

      </beans>

*****************JAVA.SRC*********************

package com.libo.dao.impl;

 

import java.util.List;

 

import javax.annotation.Resource;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.stereotype.Repository;

 

import com.libo.dao.StudentDao;

import com.libo.po.Student;

@Repository("sDao")

public class StudentDaoImpl implements StudentDao{

 @Resource

  privateSessionFactory sessionFactory;

 

   publicList<Student> getall() {

   //Sessionsession=sessionFactory.openSession();

       Sessionsession=sessionFactory.getCurrentSession();

   returnsession.createQuery("from Student").list();

   }

   @Override

   public voidsave(Student stu) {

       Sessionsession=sessionFactory.getCurrentSession();

       session.save(stu);     

   }

}

package com.libo.service.impl;

 

import java.util.List;

 

 

import javax.annotation.Resource;

 

import org.springframework.stereotype.Service;

importorg.springframework.transaction.annotation.Transactional;

 

import com.libo.dao.StudentDao;

 

import com.libo.po.Student;

import com.libo.service.StudentService;

@Service("service")(必须和actionnew的封装次一样)

@Transactional(提交事物的注解单词)

publicclass StudentServiceImplimplements StudentService{

   @Resource(引用他的父类)

  private StudentDaosDao;

   public List<Student> getall() {

       returnsDao.getall();

   }

   @Override

   publicvoid save(Student stu) {

       sDao.save(stu);

   }

   

}

package com.libo.action;

 

import java.util.List;

 

import javax.annotation.Resource;

 

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Result;

 

 

 

 

import com.libo.po.Student;

import com.libo.service.StudentService;

import com.opensymphony.xwork2.ActionSupport;

 

 

@Action(value ="login", results = {@Result(location ="/index.jsp"),@Result(name ="saves", location ="login", type ="redirectAction") })

 

publicclassLoginAction extends ActionSupport{

   @Resource

   private StudentServiceservice;

   private List<Student>list;

   private Student student;

   @Override

   public String execute()throws Exception {

       list=service.getall();

       returnSUCCESS;

   }

   public String save(){

       System.out.println(student);

       service.save(student);

       return"saves";

   }

   

   

   

   

   

   public Student getStudent() {

       returnstudent;

   }

   publicvoid setStudent(Student student) {

       this.student = student;

   }

   

   public List<Student> getList() {

       returnlist;

   }

   publicvoid setList(List<Student> list) {

       this.list = list;

   }

 

}

 

0 0
原创粉丝点击