Myeclipse搭建SSH框架
SSH(Spring Struts2 Hibernate )
学习了ssh框架,感觉很丰富,将配置写下来,以后还会再看,写的不好勿喷。
Myeclipse集成SSH框架很方便,怎么搭建百度上一大堆,只是说一下步骤。
1.先加上Struts2框架
2.集成Spring框架
3.集成Hibernate框架,集成是要先链接配置数据库,不创建SessionFactory和hibernate.conf.xml,交由Spring管理
做完三步之后 就会之后会集成到applicationContex.xml中 ,即Spring管理hibernate。
1.整合后hibernate配置文件不再需要,同时SessionFactory也整合到了Spring的配置文件,sessionFactory通过实例注入获得,需要Session时就用session = sessionFactory.getCurrentSession();
2.然后所有的实例,都是通过注释的方式获得,通过扫描包,Spring自动加载注册bean
3事务由Spring管理,session的开启关闭也由Spring管理
配置文件:1.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>SSH</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> </web-app>
2.Struts2.xml Struts2的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name ="myPackage" extends ="struts-default"> <action name="userAction" class = "com.HelloAction.userAction" > <result name ="query">query.jsp</result> <result name ="add">index.jsp</result> <result name ="queryByid">queryByid.jsp</result> </action> </package> <constant name="struts.devMode" value="true"/> <constant name="struts.i18n.encoding" value="gbk"/> </struts>
3.applicationContex.xml 实际上通过Myeclipse集成之后,里面的东西基本配置的差不多了,只要加上自己需要的扫描的包类。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx"> <context:component-scan base-package="com.Dao,com.Hibernate" /> <context:property-placeholder location="classpath:mysql.properties" />
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- hibernate 配置文件 通过Spring管理HIbernate的bean--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.autoReconnect">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingResources"> <value>com/Hibernate/User.hbm.xml</value> </property> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 对@Transactional注解的驱动,事物配置声明 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>4.mysql.property
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:3306/javaweb?useUnicode\=true&characterEncoding\=gbk jdbc.username=root jdbc.password=xidaojia
上面说的是Spring和Hibernate整合的下面说一些没有整合的细节
1.struts2中文乱码,在struts.xml中加(package之外)
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="gbk" /
2. 使用flush时同步更新 数据库 ,在hibernate.cfg.xml中加入
<property name="connection.autocommit">true</property>
3. hibernate自动创建数据库,在hibernate.cfg.xml中加入
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="myeclipse.connection.profile">MySQL</property>
<property name="current_session_context_class">thread</property>
4. applicationContext.xml没有放在WEB-INF下而是在src中,需要在web.xml中配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>