Java三大框架搭建

嗯,分享一下Java的三大框架(struts、spring、Hibernate) 的搭建,虽然很简单,但是我觉得可能还是有很多新手需要,希望可以留个参考吧!
环境什么的肯定大家都搭建好了吧,哈哈(>ω<)

先给大家梳理一下顺序↓↓↓↓↓
1.导入各个框架的jar包,加入支持–简称导包
2.创建spring 的配置文件(创建完了就写里面的内容吧)–简称春天的配置╭(′▽`)╯
3.写struts 的配置文件–简称视图与服务的连接点⊙﹏⊙‖∣好像太长了…
4.最后的最后 配置web.xml 文件
直接看代码:
这是applicationContext.xml 放在src目录下

<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
	http://www.springframework.org/schema/aop 
	 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
	 http://www.springframework.org/schema/tx 
	 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
	">

<!-- 定义数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"  />
		<property name="username" value="test" />
		<property name="password" value="456" />
		<property name="defaultAutoCommit" value="false" /><!-- 自动提交事务属性为true -->
	</bean>
	
<!-- 定义SessionFactory Bean -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" /> <!-- 注入定义好的数据源 -->
		<property name="hibernateProperties" ><!-- 添加hibernate配置参数 -->
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<!-- <prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop> -->
			</props>
		</property>
		<property name="mappingDirectoryLocations" ><!-- 添加对象关系映射文件 -->
			<list>
				<value>classpath:cn/jbit/test/entity/</value>
			</list>
		</property>
	</bean>

	<!-- 定义dao -->
	<bean id="accountDao" class="cn.jbit.test.dao.impl.AccountDaoImpl" >
		 <!-- autowire="byName" -->
		 <property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 定义service -->
	<bean id="accountService" class="cn.jbit.test.service.impl.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"  />
	</bean>
	
	<!-- 定义事务管理器 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" /><!-- 注入sessionFactory -->
	</bean>
	
	<!-- 定义事务增强,指定事务管理器 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="submit*" propagation="REQUIRED" />
			<tx:method name="register*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			
			<tx:method name="find*" read-only="true" /><!-- 事务只读true,能提高事务处理性能 -->
			<tx:method name="search*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="do*" propagation="REQUIRED"/>
			<!-- <tx:method name="*" propagation="REQUIRED" read-only="true"/> -->
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
	<!-- 定义切面 -->
		<aop:pointcut expression="execution(* cn.jbit.test.dao..*.*(..))" id="daoMethod"/>
		<!-- 把切入点和事务增强关联 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod"/>
	</aop:config>
</beans>

下面是struts.xml 的文件,也是放在src目录下

<?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>
<!--  -->
<constant name="struts.ui.theme" value="simple" />
<package name="p1" namespace="/" extends="struts-default">
	
	
	<!-- 定义全局结果 -->
	<!-- <global-results>
		<result name="login" type="redirect">/login.jsp</result>
	</global-results> -->

  <action name="get" class="cn.jbit.test.action.AccountAction" method="get">
      <result name="success" >/get.jsp</result>
  </action>
  <action name="register" class="cn.jbit.test.action.UserAction" method="register">
  	<result name="success">/registerSuccess.jsp</result>
  	<result name="input">/register.jsp</result>
  </action>
</package>
</struts>    

4.这是web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
  <display-name>StrutsSpringHib</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
  
  <!-- 告诉spring的监听器spring主配置文件的位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>  
  <!-- 配置一个监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 配置OpenSessionInViewFilter过滤器 -->
  <filter>
  	<filter-name>OpenSessionInViewFilter</filter-name>
  	<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>OpenSessionInViewFilter</filter-name>
  	<url-pattern>/*</url-pattern><!-- *.action -->
  </filter-mapping>
  
    <!-- 配置struts核心控制器 -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter -->
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
</web-app>

最后呢,我觉得吧,三大框架最难的的地方就是怎么整合了,至于什么jsp 页面啊,Action 控制器啊,或许还有什么Service层,还有DAO层啊,这些在applicationContext.xml 文件中已经充分表现出来了,需要的自己写就OK了。
spring 的配置文件也是可以拆开写的,随着需要项目规模的增加,需要交给spring 容器管理的类肯定不少,对不对,所以spring 的配置文件肯定很臃肿 对不对,这样不利于代码维护啊,所以需要拆开,至于怎么拆,,,,,你猜猜●▽●,不要打我,说笑的,下次更加精彩

其实我想直接传源代码的,这样就可以省事儿了,但是不知道怎么传,谁知道滴?

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值