ssh三层架构
来源:互联网 发布:北京大学网络教育 编辑:程序博客网 时间:2024/06/10 00:45
注:
注解必须提供除spring和hibernet的正常的包外还需要相应的三个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")(必须和action的new的封装次一样)
@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;
}
}
- java 三层架构ssh
- java 三层架构ssh
- Java 三层架构ssh
- ssh三层架构
- SSH框架mvc三层架构
- 从MVC和三层架构说到ssh整合开发-上
- 从MVC和三层架构说到ssh整合开发
- 从MVC和三层架构说到ssh整合开发-下
- 从MVC和三层架构说到ssh整合开发-上
- 从MVC和三层架构说到ssh整合开发
- SSH三层架构包结构通俗命名规则
- SSH学习之MVC和三层架构的区别
- SSH框架整合实现Java三层架构实例(一)
- 【三层】三层架构初识
- 三层架构
- 三层架构
- 三层架构
- 三层架构
- php 基础视频语法简记
- 产品与运营之调整与运营核心工作
- js知识点大全1
- 静态页面html防止js加载缓存
- 机器学习面经
- ssh三层架构
- ubuntu apt-get update提示无法验证签名
- Python网络爬虫(2)处理网页数据find和findall函数
- 快递100接口使用
- yarn 替代npm
- HDU-2570 迷瘴 (贪心)
- 面试经验分享之机器学习、大数据问题
- 入职任务之自己做一套DX API~
- RedisDesktopManager无法连接虚拟机中启动的redis服务问题解决