栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSH框架的搭建及应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSH框架的搭建及应用

SSH框架的搭建及应用

一、创建maven程序,引入依赖
struts-core、apache-commons-fileupload,struts-convention.plugin
poi、poi-ooxml
spring-context、sprng-jdbc、spring-tx、spring-context-suport、aspectjweaver
spring-web、struts2-spring-plugin
hibernate-core、mysql、
spring-orm
druid




  4.0.0

  com.Hibernate
  ssh
  1.0-SNAPSHOT
  war

  ssh Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    

    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    
    
    
      org.apache.struts
      struts2-core
      2.5.22
    
     
      org.apache.struts
      struts2-convention-plugin
      2.5.22
    

    
      commons-fileupload
      commons-fileupload
      1.4
    

    
    
      org.apache.poi
      poi-ooxml-schemas
      4.1.2
    
    
      org.apache.poi
      poi-ooxml
      4.1.2
    

    
    
      org.springframework
      spring-context
      5.2.4.RELEASE
    
    
      org.springframework
      spring-context-support
      5.2.4.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.4.RELEASE
    
    
      org.springframework
      spring-tx
      5.2.4.RELEASE
    

    
      org.aspectj
      aspectjweaver
      1.9.6
      runtime
    
    

    
      org.springframework
      spring-web
      5.2.4.RELEASE
    

    
      org.apache.struts
      struts2-spring-plugin
      2.5.22
    

    
      org.hibernate.orm
      hibernate-core
      6.0.0.Final
    
    
      mysql
      mysql-connector-java
      5.1.29
    
    
    
      org.springframework
      spring-orm
      5.3.10
    
    
      com.alibaba
      druid
      1.2.1
    

  

  
    ssh
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

二、搭建Struts框架
1、引入依赖
2、在web.xml中配置核心入口(filter)




    
        struts2
        
            org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        
    
    
        struts2
        
public class HibernateUtil {
    private static SessionFactory factory;
    static {

        //方式一:
       

        //方式二
        StandardServiceRegistry r = new StandardServiceRegistryBuilder().configure().build();
        Configuration cfg=new Configuration();
        factory=cfg.buildSessionFactory(r);
    }
    
    public static Session getSession(){
        return factory.openSession();
    }
}

注意:因为后面要实现sh整合,sh整合后,spring负责管理hibernatede 数据源,工厂,事务,所以配置文件中不需要配置数据源信息和hibernate封装了

六:sh整合
所谓的ss整合,就是由spring负责管理hibernate数据源,工厂,事务
1、引入依赖
2、实现hibernate整合的配置:管理数据源、管理工厂、管理事务



    
    
        
        
        
        
    
    
    
        
        
    
    
    
        
    
      
    

事务管理又分为三个点:
事务管理者(谁管理事务)

 
        
    

事务策略(如何管理事务)
事务切入点(什么时候管理事务)
注意:事务策略和事务切入点可以使用注解

  
    

注意:sh整合时,对dao的操作也有影响:因为.原来dao与数据库交互时,使用我们自己管理的工厂获得session对象
现在:sh整合后,需要使用spring管理的工厂获得session对象

dao继承父类hibernateDaoSupport,然后为Dao的父类注入SessionFactory工厂,在方法中,通过父类方法获得hibernate操作的模板对象super.getHIbernateTemplate,该模板提供了crud方法

SSH框架的应用

1、创建包结构,功能类,实现orm,实现IOC,DI,excel文件
注意:在application.xml文件中,开启spring,IOC DI的注解编程

 
    

注意:sh整合后,dao需要到spring管理的sessionFactory中获得session对象,1:dao继承HIbernateDaoSupport 为dao(父类)注入SessionFactory(曲线救国的方式)

@Repository
public class CarDao extends HibernateDaoSupport {
    
    @Autowired
    public void setFactory (SessionFactory factory){
        super.setSessionFactory(factory);
    }
}

2、设计网页,选择文件上传



    

3、action处理请求(接收文件,读取excel,实现批量保存)
注意:
action注解实现的请求映射

 @Action(value="upload")
    public void upload(){
        //poi读取excel文件

    }

注意:struts文件上传

    private File excel;
    public void setExcel(File excel){
        this.excel=excel;
    }

注意:poi读取excel文件

 
    @Action(value="upload")
    public void upload() throws IOException {
        List cars=new ArrayList<>();
        //poi读取excel文件
        Workbook book= WorkbookFactory.create(excel);
        Sheet sheet=book.getSheetAt(0);
        for(int i=1;i<=sheet.getLastRowNum();i++){
            //获取行
            Row r=sheet.getRow(i);
            //获取列的单元格
            Cell c1 = r.getCell(0);
            Cell c2 = r.getCell(1);
            Cell c3 = r.getCell(2);
            Cell c4 = r.getCell(3);
            int cno=(int)c1.getNumericCellValue();
            String cname=c2.getStringCellValue();
            String color=c3.getStringCellValue();
            int price=(int)c4.getNumericCellValue();
            Car car=new Car(cno,cname,color,price);
            cars.add(car);
        }
        //批量保存
        carService.saves(cars);
        //直接响应
        ServletActionContext.getResponse().getWriter().write("上传成功");
    }

注意:service实现批量保存的方法需要指定事务

 
    @Transactional(isolation= Isolation.SERIALIZABLE,propagation= Propagation.REQUIRED,rollbackFor=Exception.class)
    public void saves(List cars){
        //循环保存
        for(Car car:cars){
            carDao.save(car);
        }
    }
完整代码

1、pom




  4.0.0

  com.Hibernate
  ssh
  1.0-SNAPSHOT
  war

  ssh Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    

    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    
    
    
      org.apache.struts
      struts2-core
      2.5.22
    
    
      org.apache.struts
      struts2-convention-plugin
      2.5.22
    

    
      commons-fileupload
      commons-fileupload
      1.4
    

    
    
      org.apache.poi
      poi-ooxml-schemas
      4.1.2
    
    
      org.apache.poi
      poi-ooxml
      4.1.2
    

    
    
      org.springframework
      spring-context
      5.2.4.RELEASE
    
    
      org.springframework
      spring-context-support
      5.2.4.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.2.4.RELEASE
    
    
      org.springframework
      spring-tx
      5.2.4.RELEASE
    

    
      org.aspectj
      aspectjweaver
      1.9.6
      runtime
    
    

    
      org.springframework
      spring-web
      5.2.4.RELEASE
    

    
      org.apache.struts
      struts2-spring-plugin
      2.5.22
    

    
      org.hibernate.orm
      hibernate-core
      6.0.0.Final
    
    
      mysql
      mysql-connector-java
      5.1.29
    

    
    
      org.springframework
      spring-orm
      5.3.10
    
    
      com.alibaba
      druid
      1.2.1
    

  

  
    ssh
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  


application.xml文件



    
    
        
        
        
        
    
    
    
        
        
    
    
    
        
    
    
    
   
    

hibernate.cfg.xml文件




    
        
        
        true
        
        true
        
        update
        
        org.hibernate.dialect.MySQLDialect
        
    

struts.xml文件



    
    

    

    

web.xml



    
    
        struts2
        
            org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        
    
    
        struts2
        
    @Action(value="upload")
    public void upload() throws IOException {
        List cars=new ArrayList<>();
        //poi读取excel文件
        Workbook book= WorkbookFactory.create(excel);
        Sheet sheet=book.getSheetAt(0);
        for(int i=1;i<=sheet.getLastRowNum();i++){
            //获取行
            Row r=sheet.getRow(i);
            //获取列的单元格
            Cell c1 = r.getCell(0);
            Cell c2 = r.getCell(1);
            Cell c3 = r.getCell(2);
            Cell c4 = r.getCell(3);
            int cno=(int)c1.getNumericCellValue();
            String cname=c2.getStringCellValue();
            String color=c3.getStringCellValue();
            int price=(int)c4.getNumericCellValue();
            Car car=new Car(cno,cname,color,price);
            cars.add(car);
        }
        //批量保存
        carService.saves(cars);
        //直接响应
        ServletActionContext.getResponse().getWriter().write("上传成功");
    }
}

Dao

package com.hibernate.dao;

import com.hibernate.domain.Car;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class CarDao extends HibernateDaoSupport {
    
    @Autowired
    public void setFactory (SessionFactory factory){
        super.setSessionFactory(factory);
    }

    
    public void save(Car car){
        super.getHibernateTemplate().save(car);
    }
}

service

package com.hibernate.service;

import com.hibernate.dao.CarDao;
import com.hibernate.domain.Car;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CarService {
    @Autowired
    private CarDao carDao;

    
    @Transactional(isolation= Isolation.SERIALIZABLE,propagation= Propagation.REQUIRED,rollbackFor=Exception.class)
    public void saves(List cars){
        //循环保存
        for(Car car:cars){
            carDao.save(car);
        }
    }
}

car实体

package com.hibernate.domain;

import jakarta.persistence.*;

import java.io.Serializable;

@Entity
@Table(name = "t_Car")
public class Car implements Serializable {
   @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer cno;
    private String cname;
    private String color;
    private Integer price;

    public Car() {
    }

    public Car(Integer cno, String cname, String color, Integer price) {
        this.cno = cno;
        this.cname = cname;
        this.color = color;
        this.price = price;
    }
    @Id
    public Integer getCno() {
        return cno;
    }

    public void setCno(Integer cno) {
        this.cno = cno;
    }
    @Column(name = "carname",length = 8)
    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }
    @Transient
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821972.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号