SSH的纯注解整合
1、搭建开发环境
(1)创建Web
项目,引入jar
包
- 引入
SSH
整合的jar
包 - 引入
Struts2
的注解开发包struts2-convention-plugin-2.3.24.jar
(2)引入配置文件
-
web.xml
Struts2
核心过滤器Spring
监听器
-
jdbc.properties
-
applicationContext.xml
-
log4j.xml
(3)创建相关的包结构
(4)创建相关类
(5)编写或引入相关页面
2、开发
(1)在Action
类中编写方法
(2)在Spring
配置文件中配置Action
,将其交给Spring
管理
-
开启组件扫描
<!-- 在Spring配置文件中 --> <!-- 开启组件扫描 --> <context:component-scan base-package="com.poetr.anno_ssh"/>
-
在类上添加注释
/** * 将Action交给Spring管理: * <bean id="customerAction" class="com.poetr.anno_ssh.web.action.CustomerAction" scope="prototype"> * </bean> */ @Controller("customerAction") @Scope("prototype") public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { ... }
(3)在Struts2
中配置Action
,Action
负责处理请求和页面跳转
-
现在使用
Struts2
注解的方式-
在类上面添加注释
/** * 配置Action的访问 * <package name="demo" extends="struts-default" namespace="/"> * <action name="customer_save" class="customerAction" method="save"> * </action> * <action name="customer_update" class="customerAction" method="update"> * </action> * </package> */ @ParentPackage("struts-default") @Namespace("/") public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { ... }
-
在类中的方法上添加注释
/** * <package name="demo" extends="struts-default" namespace="/"> * <action name="customer_save" class="customerAction" method="save"> * </action> * <action name="customer_update" class="customerAction" method="update"> * </action> * </package> */ // 保存客户的方法:save @Action(value="customer_save",results={@Result(name="success",location="/login.jsp")}) public String save(){ System.out.println("Action中的save方法执行了..."); customerService.save(customer); return SUCCESS; }
-
(4)Action
中调用Service
方法
-
Service
交给Spring
管理/** * 客户管理的Service的实现类 * @author poetr * <bean id="customerService" class="com.poetr.anno_ssh.service.impl.CustomerServiceImpl"> * </bean> */ @Service("customerService") @Transactional public class CustomerServiceImpl implements CustomerService { ... }
-
Action
中注入Service
// 注入Service: @Resource(name="customerService") private CustomerService customerService;
-
Action
中调用业务层方法// 保存客户的方法:save @Action(value="customer_save", results={@Result(name="success",location="/login.jsp")}) public String save(){ System.out.println("Action中的save方法执行了..."); customerService.save(customer); return SUCCESS; }
(5)Service
中调用DAO
-
DAO
交给Spring
管理/** * 客户管理的DAO的实现类 * @author poetr * <bean id = "customerDao" class="com.poetr.anno_ssh.dao.impl.CustomerDaoImpl"> * </bean> */ @Repository("customerDao") public class CustomerDaoImpl implements CustomerDao { ... }
-
在
Service
中注入DAO
// 注入DAO: @Resource(name="customerDao") private CustomerDao customerDao;
(6)创建实体和映射(映射使用的是注解)
-
用注解配置映射
@Entity // 实体注解 @Table(name="cst_customer") // 用来将实体和表建立映射 public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long cust_id; // 建立主键和OID映射 //@Column(name="cust_name") // 普通属性和表的字段的值一样,那么@Column注解可以省略 private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_phone; private String cust_mobile; ... }
(7)在Spring
中整合Hibernate
<!-- Spring整合Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate的相关属性 -->
<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>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载映射 -->
<property name="packagesToScan" value="com.poetr.anno_ssh.domain"></property>
</bean>
(8)在DAO
中使用模板
-
不能让
DAO
继承HibernateDaoSupport
,因为属性注入不能使用注解方式。 -
自己在
DAO
中注入模板-
定义
Hibernate
模板<!-- 定义Hibernate模板 --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
-
注入模板
@Resource(name="hibernateTemplate") private HibernateTemplate hibernateTemplate;
-
-
在
DAO
中使用模板@Override public void save(Customer customer) { System.out.println("DAO中的save方法执行了..."); this.hibernateTemplate.save(customer); }
(9)配置事务管理