通过注解的方式配置整合Spring+Hibernate+Struts2 基本设置
项目结构:

applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--hibernate 的配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/food_02"> </property> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean> <!-- session工厂的配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <!-- 实体类的 元数据配置文件 --> <property name="annotatedClasses"> <list> <value>com.jfkj.Bean.User</value></list> </property></bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 开启事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 扫描包 扫描包中的注解 --> <context:component-scan base-package="com.jfkj"></context:component-scan> </beans>
BaseDao的代码:
package com.jfkj.Utils; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate4.HibernateTemplate; public class BaseDao extends HibernateTemplate{ //通过注解 获取 session 工厂类 @Resource public void SetSessionFactory(SessionFactory sessionfactory){ super.setSessionFactory(sessionfactory); } //添加方法 public void add(Object obj){ super.save(obj); } }
Dao层代码
package com.jfkj.Dao.Imp; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.jfkj.Bean.User; import com.jfkj.Utils.BaseDao; //相当于在Spring的XML 中 注册对象.<bean></bean> @Component("userDao") public class UserDaoimp extends BaseDao implements IUserDao { @Override //配置事务的传播特性和隔离级别 @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED) public boolean add(User user) { // TODO Auto-generated method stub try { super.add(user); return true; } catch (Exception e) { return false; } } }
service中的配置
package com.jfkj.Service.Imp; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.jfkj.Bean.User; import com.jfkj.Dao.Imp.IUserDao; //通过注解,在Spring容器中注册此Service对象 @Service("userService") public class UserServiceImp implements IUserService { //通过Userdao 注册 的名字,将UserDao对象注入,不需要set和get方法 @Resource(name="userDao") private IUserDao iud; @Override //配置事务的传播特性和隔离级别 @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED) public boolean add(User user) { return iud.add(user); } }
action中的代码:
package com.jfkj.action; import javax.annotation.Resource; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.jfkj.Bean.User; import com.jfkj.Service.Imp.IUserService; import com.opensymphony.xwork2.ActionSupport; /**配置struts2*/ @ParentPackage("struts-default") @Namespace("/") @Component("uaction") @Scope("prototype") public class UserAction extends ActionSupport{ //通过注解,向Spring容器获取Dao层对象 @Resource(name="userService") private IUserService ius; //------------------获得页面提交数据注入实体类中 begin------------------ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } //------------------获得页面提交数据注入实体类中 end------------------ /*** * Action:设置action的访问路径和返回值 * value:访问路径 * Results:设置返回的字符串和返回页面 * | * name:返回的字符串 * location:返回页面的路径 */ @Action(value="addUs",results={@Result(name="show",location="/show.jsp"), @Result(name="error",location="/error.jsp")}) public String saveUs(){ if(ius.add(user)){ return "show"; }else{ return "error"; } } }

浙公网安备 33010602011771号