读书人

零配置,SSH全注解

发布时间: 2012-09-02 21:00:34 作者: rapoo

0配置,SSH全注解

全注解帮我们省去了大量的配置文件,但是必要的配置还是不可少的

    spring.xml配置文件中需要配置:
dataSource(数据源)sessionFactory(会话工厂)hibernateTemplate(HibernateTemplate类)transactionManager(事务管理类,不用事务的话可省去)<context:annotation-config></context:annotation-config>(声明用注解进行依赖注入)<context:component-scan base-package="com" />(告诉spring容器扫描com下的包,对使用注解的bean进行管理)<tx:annotation-driven transaction-manager="transactionManager"/>(声明使用注解的事务)

spring.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:aop="http://www.springframework.org/schema/aop"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-2.5.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">        <!-- 用注解进行依赖注入 --><context:annotation-config></context:annotation-config><!-- 用注解让spring管理bean --><context:component-scan base-package="com" /><!-- 注解的事务 --><tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置数据源 --><bean id="dataSource"value="jdbc:mysql:///ssh621"></property><property name="username" value="root"></property><property name="password" value=""></property></bean><!-- 配置会话工场 --><bean id="sessionFactory"ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop></props></property><property name="annotatedClasses"><list><value>com.bmh.po.League</value><value>com.bmh.po.Team</value><value>com.bmh.po.ChampionShip</value><value>com.bmh.po.Group</value></list></property></bean><!-- 配置hibernateTemplate类 --><bean id="hibernateTemplate"/></constructor-arg></bean><!-- 配置事务管理类 --><bean id="transactionManager"ref="sessionFactory"></property></bean></beans>

?需要注意的是:

sessionFactory中没用mappingResources这个属性了,取而代之的是annotatedClasses,因为实体bean同样使用了注解,不再需要映射文件了sessionFactory对应的class变了,这里需要的是支持注解的类

????? 2.? struts.xml配置文件中的package标签中的配置也都不用了,struts.xml中基本可以不用写什么

????? 3.? web.xml不变

????? 4.? action层的类:BmhAction的注解

@Controller("bmhAction")//声明是控制层的组件,名为bmhAction,不指定的话默认是类的简单名的第一个字母小写@Namespace("/")//声明命名空间,相当于struts.xml中<package namespace="/">@Results({//如果return的字符串为"test"则转到testDirection.jsp@Result(name="test",location="/WEB-INF/test/testDirection.jsp"),@Result(name="error",location="/WEB-INF/test/error.jsp")})@ExceptionMappings({//如果方法中出现该异常则返回"error",error根据上面的配置,让浏览器跳转@ExceptionMapping(exception = "java.lang.Exception",result="error")})//这里要继承ActionSupport,不然可能出现找不到action的错误public class BmhAction extends ActionSupport{@Resource(name = "bmhService")//依赖注入,默认按名称装配,如果没有匹配的才按类型装配,                                                              //这里我直接指明name=,就只能按名称装配了private IBmhService bs;

?????? 5.?? service层的类:BmhService的注解

@Service("bmhService")//声明是业务层的组件@Transactional//对类中所有方法采用默认的事务管理public class BmhService implements IBmhService {@Resource(name = "bmhDao")private IBmhDao bd;//也可以写着某些方法上@Transactional(propagation=Propagation.REQUIRED)public void testTx(){bd.testTx1();bd.testTx2();}

????? 6.?? DAO层的类:BmhDao的注解:

@Repository("bmhDao")public class BmhDao implements IBmhDao {@Resource(name = "hibernateTemplate")private HibernateTemplate ht;

????? 7.?? 最后,还要对实体类进行注解:

@Entity//声明式实体类@Table(name="groupinfo")//声明对应的表public class Group {@Id//该属性对应的字段为主键@GeneratedValue(generator="group")//采用名为group的生成策略  @GenericGenerator(name="group",strategy="native")//主键生成策略  @Column(name="groupid")//数据库中对应的字段名private int groupId;  @Column(name="groupname")//普通的字段private String groupName;

???注意,上面的@Entity和@Table注解导入的是javax包下的类而不是hibernate的

?

读书人网 >软件架构设计

热点推荐