SSH关于 annotation 整合方式

本文介绍如何从零开始搭建一个基于Spring、Hibernate和Struts2框架的Web项目,包括配置文件的设置、实体类定义、数据库连接、事务管理和页面交互等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 创建web项目
  2. 导jar包(47)
  3. 在src目录下准备一堆配置文件
    1. applicationContext.xml
    2. struts.xml
    3. db.propertes
    4. log4j.propertes  --可省略
  4. 实体类,在实体类上使用hibernate注解
    1. @Entity
    2. @Table
    3. @Id
    4. @GeneratedValue(strategy=GenerationType.IDENTITY)
  5. 在applicationcontext.xml中配置sessionFactory
<!-- 加载properties文件 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 开启注解扫描  @Respostory  @Service  @Controller-->
	<context:component-scan base-package="cn.itheima"/>

	<!-- 配置连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!-- 声明sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 加载连接池 -->
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<value>
				hibernate.show_sql=true
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.hbm2ddl.auto=update
				hibernate.format_sql=true
			</value>
		</property>
		<!-- 加载注解类 -->
		<property name="packagesToScan">
			<list>
				<value>cn.itheima.domain</value>
			</list>
		</property>
	</bean>
	
	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 事务注解驱动 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

6.  在web.xml中配置ContextLoaderListener

<!-- 以下是配置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>

启动服务器,看表是否会自动创建 表自动创建,代表前面的配置没有问题,可以使用sessionFactory,然后在写dao接口, 在dao中使用sessionFactory

@Repository("userDao")
public class UserDAOImpl extends HibernateDaoSupport implements IUserDAO {

	@Autowired
	@Qualifier("sessionFactory")
	public void setSuperSessionFactory(SessionFactory factory) {
		super.setSessionFactory(factory);
	}

	@Override
	public void add(User user) {
		this.getHibernateTemplate().save(user); // session.save()
	}

7. 然后写service接口,调用dao接口

@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {

	@Autowired
	@Qualifier("userDao")
	private IUserDAO userDao;

8. 写个测试跑service接口,确保service接口是不是正确

  1. Service接口测试通过后,然后配置action
    1. @Contrller
    2. @Scope("prototype")
    3. @Namespace("/")
    4. @ParaentPackage("sturts-default")
    5. @Action(name="",results={@Result(name="",location="",type="redirect")})

9. 在web.xml中配置StrutsPrepareAndExecuteFilter

<!-- openSessionInViewFilter -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

10. 写页面,测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值