SSH全注解开发笔记

来源:互联网 发布:生产流程管理优化论文 编辑:程序博客网 时间:2024/05/13 23:09

SSH全注解开发笔记

一、添加Struts2与Spring注解

使用了Struts2注解后,无需再使用struts.xml了。然而Strut2通过在action中使用伪类,在Spring中定义该Action的Bean的方式,将Service对象注入给了Action。一旦删除了struts.xml,在Spring中定义的Action的Bean,将与Struts2无关。而这个关系,通过Spring的注解可以再次建立

Struts2注解要点:

1、继承的包:@ParentPackage(“struts-default”)

2、Action的多例:@Scope(“prototype”)

3、Action中service的注入

@Autowired
@Qualifier("studentService") //byName方式自动注入
private IStudentService studentService;


4、命名空间:@Namespace(“/test”)


Spring注解要点:

1、在Spring配置文件中注册组件扫描的基本包

<!-- 组件扫描器 --> <context:component-scan base-package="com.hk.*"></context:component-scan>


2、在Spring配置文件中开启Spring事务注解驱动

<!-- 事务注解驱动 --> <tx:annotation-driven transaction-manager="transactionManager"/>


3、在Spring配置文件中删除dao、service、action的Bean定义

保留数据源、sessionFactory以及事务管理器的配置

4、删除struts.xml

添加Hibernate的注解

Hibernate注解是替换xx.hbm.xml文件的

1、实体、表名

@Entity@Table(name="student")


2、id的注册

@Id@GeneratedValue(generator="myGenerator")@GenericGenerator(name="myGenerator",strategy="native")private Integer id;


3、删除映射文件

4、在Spring配置文件的SessionFactory定义中,删除映射文件的相关配置mappingResources

5、在Spring配置文件的SessionFactory定义中,添加实体包扫描器

<!-- 配置映射文件所在文件夹 --><!-- <property name="mappingDirectoryLocations" value="classpath:com/hk/beans"/> --><!-- 实体所在包 --><property name="packagesToScan" value="com.hk.beans"/>

附上全部源码:http://pan.baidu.com/s/1pLPSlr9

0 0
原创粉丝点击