- 创建web项目
- 导jar包(47)
- 在src目录下准备一堆配置文件
- applicationContext.xml
- struts.xml
- db.propertes
- log4j.propertes --可省略
- 实体类,在实体类上使用hibernate注解
- @Entity
- @Table
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- 在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接口是不是正确
- Service接口测试通过后,然后配置action
- @Contrller
- @Scope("prototype")
- @Namespace("/")
- @ParaentPackage("sturts-default")
- @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. 写页面,测试