S2SH三大框架采用注释整合项目案例
spring+hibernate+struts2集成,注解方式配置。BookInfoAction.java:
/**
*
*/
package com.s2sh.annotation.book.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
import com.s2sh.annotation.book.service.BookInfoService;
import com.s2sh.annotation.entity.BookInfo;
import com.s2sh.annotation.entity.PageBean;
/**
* @author Administrator
*
*/
@Controller // 标注控制层对象
public class BookInfoAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Resource(name="bookInfoServiceImpl")
private BookInfoService bookInfoService;
private List<BookInfo> books;
private PageBean pageBean;
private BookInfo book;
/**
* @return the book
*/
public BookInfo getBook() {
return book;
}
/**
* @param book the book to set
*/
public void setBook(BookInfo book) {
this.book = book;
}
/**
* @return the pageBean
*/
public PageBean getPageBean() {
return pageBean;
}
/**
* @param pageBean the pageBean to set
*/
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
/**
* @return the books
*/
public List<BookInfo> getBooks() {
return books;
}
/**
* @param books the books to set
*/
public void setBooks(List<BookInfo> books) {
this.books = books;
}
public String showBookInfoList() {
pageBean.setPageSize(5);
this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
return SUCCESS;
}
}
applicationContext-persistence.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 使用注解方式装配属性文件 -->
<context:property-placeholder location="classpath:hibernate.template.properties" />
<!-- 配置数据源 -->
<bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<!-- 整合sessionFactory对象到spring容器进行管理 -->
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 把spring容器中的数据源注入到dataSource属性中 -->
<property name="dataSource" ref="springDataSource" />
<!-- 扫描装配需要管理的实体类 -->
<property name="packagesToScan">
<list>
<value>com.s2sh.annotation.entity</value>
</list>
</property>
<!-- 配置额外属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<!-- 使用注解形式来管理事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->
<context:component-scan base-package="com.s2sh.annotation" />
</beans>struts.xml:
<?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>
<!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
<constant name="struts.objectFactory" value="spring" />
<package name="base" extends="struts-default" namespace="/" abstract="true">
</package>
<include file="config/struts-bookInfo.xml" />
</struts>
S2SH三大框架采用注释整合项目案例
解压码:**** Hidden Message *****
谢谢分享了 非常感谢楼主的分享~:D 谢谢楼主分享 是最新的莫 谢谢楼主的分享,非常不错的源码 学习下,可以吗
:) study,study。。。。。 好的学习学习 我i我i我i