2014-03-20 Spring的学习(5)------ORM工具进行数据访问+集成Web框架

1.开发环境配置      

     1.1 Java运行环境:JDK1.6

    1.2 J2EE:JAVAEE_5 Libraries

    1.3 Web APP Libraries 

          a.Hibernate:requriedJar目录下所有架包

             antlr-2.7.6.jar

                    asm-attrs.jar

                    asm.jar

                    cglib-2.1.3.jar

                    commons-collections-2.1.1.jar

                    commons-logging-1.0.4.jar

                    dom4j-1.6.1.jar

                    ehcache-1.2.3.jar

                    hibernate3.jar

                    jta.jar

        b.Spring:          

                     aspectjrt.jar

              aspectjweaver.jar

              cglib-nodep-2.1_3.jar

              common-annotations.jar

               commons-dbcp.jar

               commons-pool.jar

              spring-webmvc-struts.jar

              spring.jar

        c:Strut1.3:lib目录下将 JSTL和 Stander版本为1.1,其他导入

        d:MySQL数据库连接驱动架包

        开发环境目录如图:

    2014-03-20 Spring的学习(5)------ORM工具进行数据访问+集成Web框架

2.Spring和Hibernate的整合

User.java
package cn.itcast.domain;
public class User {
	private Integer id;
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
}

     

User.hbm.xml

   

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.domain">
	<class name="User" table="tb_user2" lazy="true">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" column="name" not-null="true" length="255"></property>
		
	</class>
</hibernate-mapping>

  

beans.xml
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <context:component-scan base-package="cn.itcast.dao"/>
    <context:property-placeholder location="classpath:jdbc.properties"/>
	<bean id="dataSource" destroy-method="close"
			class="org.apache.commons.dbcp.BasicDataSource">
			<property name="driverClassName" value="${jdbc.driverClassName}" />
			<property name="url" value="${jdbc.url}" />
			<property name="username" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
		
			<property name="initialSize" value="${jdbc.initialSize}" />
			<property name="maxActive" value="${jdbc.maxActive}" />
		
			<property name="maxIdle" value="${jdbc.maxIdle}" />
		
			<property name="minIdle" value="${jdbc.minIdle}" />
    </bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>cn/itcast/domain/User.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.show_sql=true
				hibernate.format_sql=true
				hibernate.hbm2ddl.auto=update
			</value>
		</property>
	</bean>
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<context:annotation-config></context:annotation-config>
	<tx:annotation-driven transaction-manager="txManager"/>
</beans>

    

UserDaoImpl.java
package cn.itcast.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;

@Transactional @Repository
public class UserDaoImpl implements UserDao {
	@Resource 
	private SessionFactory sessionFactory;
	/* (non-Javadoc)
	 * @see cn.itcast.dao.impl.UserDao#add(cn.itcast.domain.User)
	 */
	@Override
	public void add(User user){
		Session session = sessionFactory.getCurrentSession();
		session.persist(user);
	}
	/* (non-Javadoc)
	 * @see cn.itcast.dao.impl.UserDao#findById(java.lang.Integer)
	 */
	@Override
	public User findById(Integer id){
		return (User) sessionFactory.getCurrentSession().get(User.class, id);		
    }
	/* (non-Javadoc)
	 * @see cn.itcast.dao.impl.UserDao#update(cn.itcast.domain.User)
	 */
	@Override
	public void update(User user){
		sessionFactory.getCurrentSession().merge(user);
	}
	/* (non-Javadoc)
	 * @see cn.itcast.dao.impl.UserDao#delete(cn.itcast.domain.User)
	 */
	@Override
	public void delete(Integer id){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(User.class, id));
	}
	/* (non-Javadoc)
	 * @see cn.itcast.dao.impl.UserDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll(){
		return sessionFactory.getCurrentSession().createQuery("FROM User").list();
	}
}

 

整合成功:测试正确

3.Spring和Struts1.3的整合

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">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>ActionServlet</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
    </servlet>
	<servlet-mapping>
	   <servlet-name>ActionServlet</servlet-name>
	   <url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <action-mappings>
        <action path="/user/list" 
                type="cn.itcast.web.controller.UserAction"
                validate="false">
                <forward name="list" path="/WEB-INF/page/listUser.jsp"/>
        </action>
    </action-mappings>
	<controller>
		<set-property property="processorClass"
			value="org.springframework.web.struts.DelegatingRequestProcessor" />
	</controller>
</struts-config>
beans.xml
添加配置
<bean name="/user/list" class="cn.itcast.web.controller.UserAction"></bean>
package cn.itcast.web.controller;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.itcast.dao.UserDao;
public class UserAction extends Action {

	@Resource private UserDao dao;
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("users", dao.findAll());
		return mapping.findForward("list");
	}

}
测试结果:表示整合完毕!!

2014-03-20 Spring的学习(5)------ORM工具进行数据访问+集成Web框架

4.Spring和其他Web框架的整合

      我们可以参考到Spring开发手册,特别的详细。

结束.....

PS:在试验过程中,将项目用Tomcat启动时,发现错误.

An internal error occurred during: "Launching PoiSearch on Tomcat  6.x". java.lang.NullPointerExcept

如果出现了上述的错误按照如下的3个步骤解决:
1、首先关闭MyEclipse工作空间。
2、然后删除工作空间下的
“/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.genuitec.eclipse.ast.deploy.core.prefs”
这个文件。在这个文件中存储了一些部署项目的信息。删了这个部署的项目的信息就没有错误了。
3、重启启动MyEclipse即可解决了。

........



你可能感兴趣的:(2014-03-20 Spring的学习(5)------ORM工具进行数据访问+集成Web框架)