<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    迷失北京

    BlogJava 聯(lián)系 聚合 管理
      60 Posts :: 0 Stories :: 13 Comments :: 0 Trackbacks

    Struts2+Hibernate+Spring環(huán)境的搭建

          最近這些天一直在學(xué)習(xí)ssh,有的人說(shuō)沒(méi)有必要為了學(xué)習(xí)框架而學(xué)習(xí)框架??!我覺(jué)得自己就是為了學(xué)習(xí)框架而學(xué)習(xí)框架,不過(guò)也是形勢(shì)所逼呀!暫且不管別人是怎么說(shuō)的了,學(xué)習(xí)這種東西只要學(xué)習(xí)了就是自己的,相信知識(shí)是不會(huì)白學(xué)習(xí)的,不要“書(shū)到用時(shí)方恨少”,哈哈...有點(diǎn)未雨綢繆的意思了。先學(xué)習(xí)用著再說(shuō),然后逐步提高吧還是?。?/p>

          把具體的搭建過(guò)程記錄一下:(項(xiàng)目運(yùn)行環(huán)境:Myeclipse+Mysql+JDK1.6)

          步驟一:在Myeclipse中創(chuàng)建三個(gè)user liberary分別是struts2,hibernate,spring運(yùn)行所必需的jar包,創(chuàng)建項(xiàng)目后引入剛才創(chuàng)建的三個(gè)liberary。

          步驟二:修改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">
    	
    	<!-- 配置加載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>
    	<!-- lazy initial session自動(dòng)關(guān)閉,但是頁(yè)面請(qǐng)求未完成!-->
    	<!-- 第二種解決方案openSessionInViewInterceptor -->
    	<filter>
    		<filter-name>openSessionInView</filter-name>
    		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>openSessionInView</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	
    	
    	<!-- struts2 -->
    	<filter>
    		<filter-name>struts2</filter-name>
    		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	
    	
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

      

          步驟三:修改spring的配置文件,具體代碼如下:

    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
               http://www.springframework.org/schema/tx           
               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    	<context:annotation-config />
    	<!-- 根據(jù)annotation注入 -->
    	<context:component-scan base-package="com.wk" />
    	<!-- 創(chuàng)建dataSource -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/spring" />
    		<property name="username" value="root" />
    		<property name="password" value="wangkang" />
    	</bean>
    	<!-- 整合hibernate -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan">
    			<list>
    				<value>com.wk.model</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    			</props>
    		</property>
    	</bean>
    	<!-- spring_xml事務(wù)管理-->
    	<bean id="txManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	<aop:config>
    		<aop:pointcut id="serviceBusiness" expression="execution(public * com.web..*.add(..))" />
    		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceBusiness"
    			order="2" />
    	</aop:config>
    	<tx:advice id="txAdvice" transaction-manager="txManager">
    		<tx:attributes>
    			<tx:method name="get*" read-only="true" />
    			<tx:method name="add*" propagation="REQUIRED" />
    		</tx:attributes>
    	</tx:advice>
    	<!-- HibernateTemplate -->
    	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    </beans>
    

      

    spring注入的時(shí)候用的是Annotation 的方式,hibernate映射的時(shí)候也是用的Annotation的方式

          貼出其他的代碼,主要是配置文件寫(xiě)的時(shí)候比較費(fèi)勁,其他的東西還是比較好搞定的:

          1.model---Person

    package com.wk.model;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import org.springframework.stereotype.Component;
    @Entity
    @Component("person")
    public class Person {
    	private int id;
    	private String name;
    	private String address;
    	private String profession;
    	public Person() {
    		super();
    	}
    	@Id
    	@GeneratedValue
    	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 getAddress() {
    		return address;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}
    	public String getProfession() {
    		return profession;
    	}
    	public void setProfession(String profession) {
    		this.profession = profession;
    	}
    }
    

      

          2.dao---PersonDAO

    package com.wk.dao;
    import com.wk.model.Person;
    public interface PersonDAO {
    	public int save(Person person);
    	public int delete(int id);
    	public int update(Person person);
    	public Person getPerson(int id);
    }
    

      

          3.impl---PersonDAOImpl

    package com.wk.dao.impl;
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.springframework.stereotype.Component;
    import com.wk.dao.PersonDAO;
    import com.wk.model.Person;
    import com.wk.util.HibernateUtil;
    @Component("personDAO")
    public class PersonDAOImpl implements PersonDAO {
    	public int delete(int id) {
    		Session session = HibernateUtil.getSessionfactory().getCurrentSession();
    		Person person = new Person();
    		try {
    			session.beginTransaction();
    			person.setId(id);
    			session.delete(person);
    			session.beginTransaction().commit();
    			return 1;
    		} catch (HibernateException e) {
    			e.printStackTrace();
    		}
    		return 0;
    	}
    	public Person getPerson(int id) {
    		Session session = HibernateUtil.getSessionfactory().getCurrentSession();
    		Person person = new Person();
    		try {
    			session.beginTransaction();
    			person = (Person) session.get(Person.class, id);
    			session.beginTransaction().commit();
    			return person;
    		} catch (HibernateException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    	public int save(Person person) {
    		Session session = HibernateUtil.getSessionfactory().getCurrentSession();
    		try {
    			session.beginTransaction();
    			session.save(person);
    			session.beginTransaction().commit();
    			return 1;
    		} catch (HibernateException e) {
    			e.printStackTrace();
    		}
    		return 0;
    	}
    	public int update(Person personVo) {
    		Session session = HibernateUtil.getSessionfactory().getCurrentSession();
    		Person person = new Person();
    		try {
    			session.beginTransaction();
    			person = (Person) session.load(Person.class, personVo.getId());
    			
    			person.setName(personVo.getName());
    			person.setAddress(personVo.getAddress());
    			person.setProfession(personVo.getProfession());
    			
    			session.update(person);
    			session.flush();//提高效率
    			session.beginTransaction().commit();
    			return 1;
    		} catch (HibernateException e) {
    			e.printStackTrace();
    		}
    		return 0;
    	}
    }
    

      

          4.strutsAction---PersonAction

    package com.wk.action;
    import javax.annotation.Resource;
    import org.springframework.stereotype.Component;
    import com.opensymphony.xwork2.ActionSupport;
    import com.wk.dao.PersonDAO;
    import com.wk.dao.impl.PersonDAOImpl;
    import com.wk.model.Person;
    @Component("personAction")
    public class PersonAction extends ActionSupport {
    	private static final long serialVersionUID = -3699819837781302739L;
    	PersonDAO personDAO = new PersonDAOImpl();
    	Person person = new Person();
    	int id = 0;
    	public String addPerson() {
    		int flag = personDAO.save(person);
    		if (flag == 1) {
    			return "success";
    		} else {
    			return "fail";
    		}
    	}
    	public String delPerson() {
    		int flag = personDAO.delete(id);
    		if (flag == 1) {
    			return "success";
    		} else {
    			return "fail";
    		}
    	}
    	public String updatePerson() {
    		int flag = personDAO.update(person);
    		if (flag == 1) {
    			return "success";
    		} else {
    			return "fail";
    		}
    	}
    	public String searchPerson() {
    		person = personDAO.getPerson(id);
    		if (person != null) {
    			return "success";
    		} else {
    			return "fail";
    		}
    	}
    	public Person getPerson() {
    		return person;
    	}
    	public void setPerson(Person person) {
    		this.person = person;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public PersonDAO getPersonDAO() {
    		return personDAO;
    	}
    	@Resource(name = "personDAO")
    	public void setPersonDAO(PersonDAO personDAO) {
    		this.personDAO = personDAO;
    	}
    }
    

      

    差不多就是這樣了,這是封裝了后代的代碼沒(méi)有前臺(tái)的展示,不過(guò)Junit全部測(cè)試通過(guò)了。

    posted on 2010-12-27 22:00 王康 閱讀(824) 評(píng)論(1)  編輯  收藏

    Feedback

    # re: Struts2+Hibernate+Spring環(huán)境的搭建【SSH搭建】!! 2012-11-24 08:10 吳宏偉
    博主 能不能把HibernateUtil這個(gè)類(lèi)貼出來(lái)啊   回復(fù)  更多評(píng)論
      


    只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 伊伊人成亚洲综合人网7777| 特黄特色的大片观看免费视频| 久久精品国产亚洲Aⅴ香蕉| 7723日本高清完整版免费| 好猛好深好爽好硬免费视频| 久久亚洲国产成人影院| 亚洲激情在线观看| 浮力影院亚洲国产第一页| 成年性生交大片免费看| 亚洲黄色免费电影| 久久这里只精品99re免费| a一级爱做片免费| 黄页网站在线免费观看| 亚洲欧好州第一的日产suv| 91亚洲导航深夜福利| 亚洲国产a∨无码中文777| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 1区1区3区4区产品亚洲| 亚洲精品夜夜夜妓女网| 亚洲中文字幕无码爆乳av中文| 四虎影在线永久免费四虎地址8848aa| 91嫩草国产在线观看免费| 黄页网站在线看免费| 4399影视免费观看高清直播| 91免费国产精品| 91成人在线免费观看| 91精品免费久久久久久久久| 精品一区二区三区无码免费视频| 久久久久免费看黄a级试看| 久久久久久影院久久久久免费精品国产小说 | 99精品全国免费观看视频..| 国产精品免费αv视频| 国产福利电影一区二区三区,免费久久久久久久精| 久久人午夜亚洲精品无码区| 风间由美在线亚洲一区| 国产成人高清亚洲一区91| 一区二区免费国产在线观看| 国产免费区在线观看十分钟| 三级网站在线免费观看| 亚欧日韩毛片在线看免费网站| 一区二区免费视频|