一、定义一个Bean,person.java
1 package cn.itcast.bean; 2 3 public class Person { 4 private Integer id; 5 private String name; 6 7 public Person() {} 8 9 public Person(String name) { 10 this.name = name; 11 } 12 13 public Integer getId() { 14 return id; 15 } 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 }
二、定义一个接口,PersonService
1 package cn.itcast.Service; 2 3 import java.util.List; 4 5 import cn.itcast.bean.Person; 6 7 public interface PersonService { 8 9 public abstract void save(Person person); 10 11 public abstract void update(Person person); 12 13 public abstract Person getPerson(Integer personId); 14 15 public abstract void delete(Integer personId); 16 17 public abstract List<Person> getPersons(); 18 19 }
三、定义接口的实现类 PersonServiceBean
1 package cn.itcast.Service.Impl; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.springframework.transaction.annotation.Propagation; 10 import org.springframework.transaction.annotation.Transactional; 11 12 import cn.itcast.Service.PersonService; 13 import cn.itcast.bean.Person; 14 @Transactional 15 public class PersonServiceBean implements PersonService { 16 @Resource 17 private SessionFactory sessionFactory; 18 19 @Override 20 public void save(Person person){ 21 //取被spring容器管理的session 22 sessionFactory.getCurrentSession().persist(person); 23 } 24 25 @Override 26 public void update(Person person){ 27 sessionFactory.getCurrentSession().merge(person); 28 } 29 30 @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true) 31 @Override 32 public Person getPerson(Integer personId){ 33 return (Person) sessionFactory.getCurrentSession().get(Person.class, personId); 34 35 } 36 37 @Override 38 public void delete(Integer personId){ 39 sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personId)); 40 } 41 42 @Transactional(propagation =Propagation.NOT_SUPPORTED,readOnly = true) 43 @SuppressWarnings("unchecked") 44 @Override 45 public List<Person> getPersons(){ 46 return sessionFactory.getCurrentSession().createQuery("from Person").list(); 47 48 } 49 }
四、配置文件beans.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 12 13 <!-- <context:property-placeholder location="classpath:jdbc.properties"/> --> 14 <context:annotation-config/> 15 <!-- 配置dataSource --> 16 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 17 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 18 <property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/> 19 <property name="username" value="tsrescue"/> 20 <property name="password" value="123456"/> 21 <!-- 连接池启动时的初始值 --> 22 <property name="initialSize" value="1"/> 23 <!-- 连接池的最大值 --> 24 <property name="maxActive" value="500"/> 25 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> 26 <property name="maxIdle" value="2"/> 27 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> 28 <property name="minIdle" value="1"/> 29 </bean> 30 <!-- 配置sessionFactory --> 31 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 32 <property name="dataSource" ref="dataSource"/> 33 <property name="mappingResources"> 34 <list> 35 <value>cn/itcast/bean/Person.hbm.xml</value> 36 </list> 37 </property> 38 <property name="hibernateProperties"> 39 <props> 40 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 41 <prop key="hibernate.show_sql">true</prop> 42 <prop key="hibernate.format_sql">true</prop> 43 </props> 44 45 </property> 46 </bean> 47 <!-- spring提供的针对hibernate的事务管理器 --> 48 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 49 <property name="sessionFactory" ref="sessionFactory"/> 50 </bean> 51 <!-- 基于注解的方式声明事务 --> 52 <tx:annotation-driven transaction-manager="txManager"/> 53 <bean id = "personService" class="cn.itcast.Service.Impl.PersonServiceBean"/> 54 </beans>
五,定义Person.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2016-6-1 10:08:44 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="cn.itcast.bean"> 6 <class name="Person" table="person"> 7 <id name="id"> 8 <generator class="native" /> 9 </id> 10 <property name="name"/> 11 </class> 12 </hibernate-mapping>
六、定义测试类 PersonServiceBeanTest
1 package cn.itcast.Service.Impl; 2 3 import static org.junit.Assert.*; 4 5 import java.util.List; 6 7 import org.junit.BeforeClass; 8 import org.junit.Test; 9 import org.springframework.context.ApplicationContext; 10 import org.springframework.context.support.ClassPathXmlApplicationContext; 11 12 import cn.itcast.Service.PersonService; 13 import cn.itcast.bean.Person; 14 15 public class PersonServiceBeanTest { 16 private static PersonService ps; 17 @BeforeClass 18 public static void setUpBeforeClass() throws Exception { 19 try { 20 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 21 ps = (PersonService) ctx.getBean("personService"); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 27 @Test 28 public void testSave() { 29 for(int i = 1;i<=10;i++){ 30 ps.save(new Person("小明"+i)); 31 } 32 } 33 34 @Test 35 public void testUpdate() { 36 Person person = ps.getPerson(1); 37 person.setName("小明修改版"); 38 ps.update(person); 39 } 40 41 @Test 42 public void testGetPerson() { 43 Person p = ps.getPerson(1); 44 System.out.println(p.getName()); 45 } 46 47 @Test 48 public void testDelete() { 49 ps.delete(3); 50 } 51 52 @Test 53 public void testGetPersons() { 54 List<Person> list = ps.getPersons(); 55 for (Person person : list) { 56 System.out.println(person.getName()); 57 } 58 } 59 60 }
以上实现了对数据库的操作,下面通过整合Struts来实现将数据传递到页面
一、首先定义一个action,PersonAction 继承Action
1 package cn.itcast.web.action; 2 3 import javax.servlet.ServletRequest; 4 import javax.servlet.ServletResponse; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 import org.apache.struts.action.Action; 9 import org.apache.struts.action.ActionForm; 10 import org.apache.struts.action.ActionForward; 11 import org.apache.struts.action.ActionMapping; 12 import org.springframework.web.context.WebApplicationContext; 13 import org.springframework.web.context.support.WebApplicationContextUtils; 14 15 import cn.itcast.Service.PersonService; 16 17 public class PersonAction extends Action { 18 19 @Override 20 public ActionForward execute(ActionMapping mapping, ActionForm form, 21 HttpServletRequest request, HttpServletResponse response) 22 throws Exception { 23 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext()); 24 PersonService personService = (PersonService) ctx.getBean("personService"); 25 request.setAttribute("persons", personService.getPersons()); 26 return mapping.findForward("list"); 27 28 } 29 }
二、定义struts-config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts-config PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 4 "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 5 <struts-config> 6 <action-mappings> 7 <action path = "/person/list" type = "cn.itcast.web.action.PersonAction" 8 validate = "false"> 9 <forward name = "list" path = "/WEB-INF/page/personlist.jsp"/> 10 </action> 11 </action-mappings> 12 </struts-config>
三、定义web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:beans.xml</param-value> 8 </context-param> 9 <!-- 对Spring容器进行实例化 --> 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 14 <servlet> 15 <servlet-name>struts</servlet-name> 16 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 17 <init-param> 18 <param-name>config</param-name> 19 <param-value>/WEB-INF/struts-config.xml</param-value> 20 </init-param> 21 <load-on-startup>0</load-on-startup> 22 </servlet> 23 <servlet-mapping> 24 <servlet-name>struts</servlet-name> 25 <url-pattern>*.do</url-pattern> 26 </servlet-mapping> 27 <welcome-file-list> 28 <welcome-file>index.jsp</welcome-file> 29 </welcome-file-list> 30 </web-app>
四、定义一个jsp页面,用来显示数据。
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>人员列表</title> 9 </head> 10 <body> 11 <c:forEach items="${persons }" var="person"> 12 ${person.id },${person.name }<br> 13 </c:forEach> 14 </body> 15 </html>
好了,至此简单的ssh框架整合完毕!
时间: 2024-11-06 03:31:01