关闭

Spring整合案例

标签: spring
216人阅读 评论(0) 收藏 举报
分类:
使用Struts2、Spring和Hibernate进行开发的小案例
applicationContext.xml:
     在applicationContext.xml中对Hibernate还有c3p0的控制反转和依赖注入。
<?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:context="http://www.springframework.org/schema/context"
    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/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd
        ">


	<!-- 连接数据库连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day35"></property>
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123"></property>
	</bean>
	
	<!-- SessionFactory的配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>demo/entity/Student.hbm.xml</value>
			</list>
		</property>
	</bean>
	
	<!-- hibernateTemplate的配置 -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- StudentDao的配置 -->
	<bean id="studentDao" class="demo.dao.StudentDao">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	
	<!-- StudentService的配置 -->
	<bean id="studentService" class="demo.service.StudentService">
		<property name="studentDao" ref="studentDao"></property>
	</bean>
	
	<!-- StudentAction的配置 -->
	<bean id="studentAction" class="demo.action.StudentAction" scope="prototype">
		<property name="studentService" ref="studentService"></property>
	</bean>
	
</beans>

web.xml:
在web.xml文件对Struts2的整合,加入Spring的核心监听器和Struts2的核心过滤器。
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">
  <display-name></display-name>
  
  <!-- Struts2的核心过滤器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- Spring的核心监听器 -->
 <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   
   <context-param>
   	<param-name>contextConfigLocation</param-name>
   	<param-value>classpath:applicationContext.xml</param-value>
   </context-param>
  	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


以上的步骤就可以使Spring对Struts2和Hibernate进行整合
下面的是三层架构的程序:
Student:
package demo.entity;


public class Student {


	private int id;
	private String name;
	private String age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
	
	
}




Student.hbm.xml:
<?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="demo.entity">
	
	<class name="Student"  table="student">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" />
		<property name="age" />
		
	</class>
</hibernate-mapping>




StudentService:
package demo.service;


import java.util.List;


import demo.dao.StudentDao;
import demo.entity.Student;


public class StudentService {


	private StudentDao studentDao;
	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}
	
	public List<Student> findAll(){
		return studentDao.findAll();
	}
}






StudentDao:
package demo.dao;


import java.util.List;


import org.springframework.orm.hibernate4.HibernateTemplate;


import demo.entity.Student;


public class StudentDao {
	private HibernateTemplate hibernateTemplate;
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}




	public List<Student> findAll(){
		return hibernateTemplate.loadAll(Student.class);
	}
}






StudentAction:
	package demo.action;


import java.util.List;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


import demo.entity.Student;
import demo.service.StudentService;


public class StudentAction extends ActionSupport {


	private StudentService studentService;
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	
	public String list(){
		List<Student> list = studentService.findAll();
		ActionContext.getContext().put("list", list);
		
		return "success";
	}
}



成功执行程序后的跳转页面:
list.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>显示用户</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->


  </head>
  
  <body>
    <s:iterator value="#list" var="s">
    	姓名:<s:property value="#s.name"/>,id:<s:property value="#s.id"/>,年龄:<s:property value="#s.age"/><br />
    </s:iterator>
  </body>
</html>



以后的小案例就是简单的使用ssh2的框架进行开发的,希望能帮助到大家
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:39239次
    • 积分:1299
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:1篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论