SSH整合学习笔记(一)

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中配置ActionAction负责处理请求和页面跳转

  • 现在使用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)配置事务管理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值