SSH框架搭建

在Eclipse中搭建一个SSH框架
新建一个web项目:

1.把ssh框架所需要的包拷贝到:WebContent -->> WEB-INF -->> lib文件夹里面

  ⑴.Struts2架包下载:http://www.apache.org/

  ⑵.spring架包:http://projects.spring.io/spring-framework/

<1>

 <2>

<3>

 <4>

 <5>

 

<6>选择一个自己所需要的版本,点击进入后:

 

  ⑶.hibernate架包:http://hibernate.org/orm/downloads/

  ⑷.比如我的下好之后就是这样的《资源包全部放到WebContent-->>WEB-INF-->>lib的文件夹里》:

  ⑸.《这里的Struts2是2.3版本》将它们都解压缩之后:struts2的资源包在:struts-2.3.30-appsstruts-2.3.30apps里面的一个叫struts2-showcase.war的项目里,将这个项目导入eclipse,找到项目里的:WebContent-->>WEB-INF-->>lib文件,将里面的架包拷贝到自己的项目。但要注意需要删掉一些架包,当从spring拷贝架包过来的时候会让架包冲突。

  ⑹.spring的资源包:在spring-framework-4.2.2.RELEASE-distspring-framework-4.2.2.RELEASElibs文件架里,直接拷贝到自己的项目。由于上面已经把冲突的包删了,这里全部拷贝过来就行了。

  ⑺.hibernate的资源包:在hibernate-release-5.2.2.Finallib equired文件夹里,全部拷贝过来就可以了。但是要注意可能会有一个包会冲突,记得检查。《版本不同可能会出现》

  ⑻.C3P0的数据包:在hibernate-release-5.2.2.Finalliboptionalc3p0文件夹里,全部拷贝到自己的项目就行了

2.配置配置文件
配置文件在WebContent -->> WEB-INF文件夹里面

新的配置文件打开一般是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SSH</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

配置的时候可以删除一些不需要的代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SSH</display-name>
  <welcome-file-list>
    <welcome-file>index.action</welcome-file>
  </welcome-file-list>
</web-app>

配置文件可以参考前面struts2-showcase.war的项目里的配置,注意这个项目的配置为《Struts2.5》配置方式,和前一个版本有点不一样,需要将配置更改一些。

需要把struts2-showcase.war的项目里的这些配置拷贝到自己的项目:

 注意,我们现在用的是2.3版本的Struts2,配置应用下面的配置,如果要使用上面的配置,请下载2.5以上的版本!!!

自己项目的配置还需要加一些东西:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssh_001</display-name>
  <welcome-file-list>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

3.建包和一些相关的xml文件:Java Resources -->> src文件夹下建包和相关的xml文件
一般情况,我们都需要建4个包,分别是,控制层:action,数据层:dao,实体层:entity,业务层:service
xml文件一般要创建:applicationContext.xml , struts.xml
而相对于比较大一点的工程一般struts.xml都是需要多个子类,这样利于管理,修改的时候只需要修改某个子类就行了。
例如:在这个目录下,新建一个xml文件:shh001.xml
那么在struts.xml文件里用下面语句引用就行了:<include file="shh001.xml" />

   

  3.1《文件的头信息部分可以在事例在项目中拷贝!!》

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- 上面的头,注意版本,从样例里复制过来 showcase.warWEB-INFsrcjavastruts.xml -->

<!-- include文件用于分割,实现多人并发不冲突 -->
<struts>
    <!-- 告知Struts2运行时使用Spring来创建对象 -->
    <constant name="struts.objectFactory" value="spring" />
    <include file="s001.xml" />
    <include file="s002.xml" />
    <include file="s003.xml" />
</struts>

  3.2配置struts的子文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- 上面的头,注意版本,从样例里复制过来 showcase.warWEB-INFsrcjavastruts.xml -->

<struts>
    <!-- 第1步:先定义一个包 -->
    <package name="mypck001" extends="struts-default">
        <action name="Index" class="myIndexAction" method="execute1">
            <result name="success">/WEB-INF/jsp/index2.jsp</result>
            <result name="error">/WEB-INF/jsp/s_tag.jsp</result>
        </action>
    </package>
</struts>

   3.3这里为控制层的一些基本代码:

 

package ssh.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import ssh.dao.IndexDao;
import ssh.dao.IndexDaoImpl;
import ssh.entity.BookCard;
import ssh.service.IndexService;
import ssh.service.IndexServiceImpl;
import ssh.util.MyConnection;

public class IndexAction extends ActionSupport {
    
    //声明service,但不给它创建具体的实现类的实例,
    private IndexService is = null;
    public void setIs(IndexService is) {
        this.is = is;
    }

    public String execute1() {
        List<BookCard> myBookCardList = is.getAllBookCard();
        System.out.println("结果集:"+myBookCardList.size());
        ActionContext ac = ActionContext.getContext();
        ac.put("myBookCardList", myBookCardList);
        return "success";
    }
    
    public String formatDouble(double s){
        DecimalFormat fmat=new DecimalFormat("u00A4##.0"); 
        return fmat.format(s);
    }
}

   3.4在配置applicationContext.xml的配置文件时还需要配置实体类entity的映射:

  《1》

   《2》编写实体类BookCard

package ssh.entity;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Entity;

/*
 * 跟数据库表一致,作为一个java对象
 * 1个对象代表的是数据库表中的一行记录
 * 1个属性代表的是表中的一个字段
 */

public class BookCard {
    private int cid  ;
    private String name;
    private String sex ;
    private Date cardDate;
    private Double deposit;
    
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getCardDate() {
        return cardDate;
    }
    public void setCardDate(Date cardDate) {
        this.cardDate = cardDate;
    }
    public Double getDeposit() {
        return deposit;
    }
    public void setDeposit(Double deposit) {
        this.deposit = deposit;
    }
}

  《3》编写BookCard.hbm.xml文件,注意:实体类的名字要与映射的名字一样:

<?xml version="1.0"?>  
<!DOCTYPE hibernate-mapping PUBLIC   
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="ssh.entity.BookCard" table="BookCard">
        <id name="cid" column="cid">
            <generator class="native"></generator>
        </id>
        <property name="name" column="name"></property>
        <property name="sex" column="sex"></property>
        <property name="cardDate" type="date" column="cardDate"></property>
        <property name="deposit" type="double" column="deposit"></property>
        
    </class>

</hibernate-mapping>  

  3.5最后是applicationContext.xml 的配置: 

  《1》总体预览:

<?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:p="http://www.springframework.org/schema/p"  
        xmlns:aop="http://www.springframework.org/schema/aop"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:jee="http://www.springframework.org/schema/jee"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="    
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
   <!-- 头信息一般不会变--> 
  <!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由spring去管理 --> <bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"> <!-- setIs(myIndexService) --> <property name="is" ref="myIndexService"/> </bean> <!-- myIndexService = new ssh.service.IndexServiceImpl() --> <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype"> <property name="id" ref="myIndexDao"/> </bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> <!-- 数据库连接池是单例 --> <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 注入连接池,包含了数据库用户名,密码等等信息 --> <property name="dataSource" ref="myDataSource"/> <!-- 配置Hibernate的其他的属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.connection.autocommit">false</prop> <!-- 开机自动生成表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingResources"> <list> <value>ssh/entity/BookCard.hbm.xml</value> </list> </property> </bean> <!-- C3P0连接池 --> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/BookCardDB"/> <property name="user" value="root"/> <property name="password" value=""/> <!-- 每300秒检查所有连接池中的空闲连接 --> <property name="idleConnectionTestPeriod" value="300"></property> <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 --> <property name="maxIdleTime" value="900"></property> <!-- 最大连接数 --> <property name="maxPoolSize" value="2"></property> </bean> <!-- dbcp连接池 --> <bean id="myDataSource2" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/BookCardDB"/> <property name="username" value="root"/> <property name="password" value=""/> </bean> </beans>

  《2》:

  《3》数据库连接池的配置(这里我用的是c3p0):

原文地址:https://www.cnblogs.com/thz-weiai/p/5842208.html