使用注解的方式完成SSH三大框架的搭建

1.新建web项目

2.使用myeclipe的DB Browser工具建立与数据库的连接,从而自动生成hibernate相关的配置文件和实体类

此处以mysql数据库为例:

3.回到项目中,新建包,分别存储实体类entity,控制器action,数据访问层dao,业务逻辑层service,使用hibernate反向工程生成实体类和相关注解:

4.此时没有导入jar的话会报错,手动导入准备好的jar包


5.编写web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list> 
	<!-- 配置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>
	</filter-mapping>
	
	<!-- 配置struts的核心过滤器 -->
	<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容器对象applicationContext
		通过加载spring配置文件,创建applicationContext对象
	 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-common.xml</param-value>
	</context-param>
	
	<!-- 配置spring的监听程序:检测需要注入事务的对象方法 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

6.编写database.properties,配置数据库连接属性:(以mysql为例)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/rain
uname=root
pwd=jzbr

7.编写spring配置文件spring-common.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	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/tx 
			http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context-3.2.xsd
			"
	default-autowire="byName">


	<!-- 加载数据库配置资源文件 把之前配置在hibernate.cfg.xml中的关于数据库参数配置在资源文件database.properties -->

	<bean id="propConfig"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:database.properties</value>
		</property>
	</bean>

	<!-- 配置数据源:用来产生SessionFactory对象 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">

		<!-- 从jdbc.properties文件中通过键值对读取数据库信息 -->
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${uname}"></property>
		<property name="password" value="${pwd}"></property>
	</bean>

	<!-- 配置SessionFactory对象 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<!-- 依赖数据源对象 -->
		<property name="dataSource" ref="dataSource"></property>

		<!-- 配置hibernate属性 将hibernate.cfg.xml剩余的属性搬到spring文件中 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>

		<!-- 加载hibernate写有注解的实体类 -->
		<property name="packagesToScan" value="com.entity"></property>
	</bean>

	<!-- 配置事务管理器:为每一个增删改的方法自动注入事务,也就自动提交事务 -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">

		<!-- 事务管理器需要依赖SessionFactory对象 -->
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 开启注解方式的事务注入功能 base-package="com.jboa" 指定需要扫描的写有spring注解的类的范围 -->
	<context:component-scan base-package="com"></context:component-scan>

	<!-- 配置注解方式的事务驱动对象,并指定事务对象的管理者 -->
	<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>

</beans>

 

8.编写struts配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	
	<!-- 开发模式,运行时会提供更多的日志或者debug信息。能够提高开发效率,但是性能方面会差一些。真正发布的时候会配置为false -->
	<constant name="struts.devMode" value="true" />

	<package name="default" namespace="/" extends="struts-default">
		 <action name="rainAction*" class="com.action.RainAction" method="{1}">
		    <result name="{1}">/index.jsp</result>
		 </action>

	</package>



</struts>

 

注意:此处的配置内容以自己项目为准。

9.编写dao层,实现数据的查询:

先新建接口,再创建接口的使用类,在实现类中完成数据的增删改查:.

10.编写service层,此处为了操作简便,略去service层,直接编写action层

11.编写jsp文件,实现数据的显示:(此处用到jstl标签)

 

12.最终访问效果如下:

13.进行增删改操作:

 

14.最后执行效果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kevin_feng老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值