S2SH开发环境搭建

来源:互联网 发布:linux批量安装 编辑:程序博客网 时间:2024/06/01 08:07

一、创建数据库并设置编码为utf8

CREATE DATABASE testdb DEFAULT CHARACTER SET utf8;

二、创建MyEclipse工程

1.新建基于web工程的SSH项目,并设置编码为UTF-8

2.添加框架环境

A.添加Junit(MyEclipse自带)
B.添加Struts2环境(jar包、struts.xml、web.xml)
  1)所需jar包
struts2-core-2.3.30.jarxwork-core-2.3.30.jarognl-3.0.19.jarcommons-fileupload-1.3.2.jarcommons-io-2.2.jarfreemarker-2.3.22.jarjavassist-3.11.0.GA.jarcommons-lang3-3.2.jarcommons-logging-1.1.3.jar
  2)web.xml配置
    web.xml
<!-- 配置Struts2的核心的过滤器 --><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>
  3)struts.xml配置(拷贝一个struts.xml模版到src目录,进行适当修改)
    struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC<span style="white-space:pre"></span>"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><span style="white-space:pre"></span><!-- 配置为开发模式 --><constant name="struts.devMode" value="true" /><!-- 把扩展名配置为action --><constant name="struts.action.extension" value="action" /><!-- 把主题配置为simple --><constant name="struts.ui.theme" value="simple" /><package name="default" namespace="/" extends="struts-default"><action name="" class=""><result name=""></result></action></package></struts>
C.添加Hibernate环境(jar包、hibernate.cfg.xml、*.hbm.xml)
  1)所需Jar包
antlr-2.7.7.jardom4j-1.6.1.jarhibernate-commons-annotations-4.0.5.Final.jarhibernate-core-4.3.11.Final.jarhibernate-jpa-2.1-api-1.0.0.Final.jarjandex-1.1.0.Final.jarjavassist-3.18.1-GA.jar(Struts2框架中也有,二选一)jboss-logging-3.1.3.GA.jarjboss-logging-annotations-1.2.0.Beta1.jarjboss-transaction-api_1.2_spec-1.0.0.Final.jar
jdbc驱动mysql-connector-java-5.1.39-bin.jar
c3p0数据库连接池c3p0-0.9.2.1.jarmchange-commons-java-0.2.3.4.jar
  2)hibernate.cfg.xml配置(拷贝一个hibernate.cfg.xml模版到src目录,进行适当修改)
    hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- 1.数据库连接信息 --><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf8</property><property name="connection.username">root</property><property name="connection.password">123</property><!-- 2.其他配置 --><property name="show_sql">true</property><property name="format_sql">true</property><property name="hbm2ddl.auto">update</property><span style="white-space:pre"></span><!-- 告诉Hibernate让它不使用单纯的JDBC连接,而使用连接池的方式,因为Hibernate默认是使用jdbc方式来获取值 --><property name="hibernate.temp.use_jdbc_metadata_defaults">false</property><!-- 3.导入映射文件 --><!-- <mapping resource="" /> --></session-factory></hibernate-configuration>
  3)*.hbm.xml配置(拷贝一个Person.hbm.xml模版到src目录,进行适当修改)
    Person.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 package=""><class name="" table="">
<id name=""><span style="white-space:pre"></span><generator class=""/><span style="white-space:pre"></span></id><property name=""/></class></hibernate-mapping>
D.添加Spring环境(jar包、applicationContext.xml/beans.xml)
  1)所需Jar包
commons-logging-1.2.jar(Struts2框架已经有了)spring-aop-4.3.3.RELEASE.jarspring-beans-4.3.3.RELEASE.jarspring-context-4.3.3.RELEASE.jarspring-core-4.3.3.RELEASE.jarspring-expression-4.3.3.RELEASE.jarspring-jdbc-4.3.3.RELEASE.jarspring-orm-4.3.3.RELEASE.jarspring-tx-4.3.3.RELEASE.jarspring-web-4.3.3.RELEASE.jar
  2)applicationContext.xml配置(拷贝一个applicationContext.xml模版到src目录,进行适当修改)
    applicationContext.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: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-4.3.xsd<span style="white-space:pre"></span>http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<span style="white-space:pre"></span><!-- 自动扫描与装配bean --><context:component-scan base-package=""/></beans>

3.整合SSH

A.Struts2与Spring整合
  a.单独测试Struts2
  1)新建TestAction类
    TestAction.java
package my.ssh.demo.test;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport {@Overridepublic String execute() throws Exception {System.out.println("===> TextAction.execute()");return super.execute();}}
  2)在WebRoot下新建test.jsp
    test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
<span style="white-space:pre"></span><head><title>My JSP 'test.jsp' starting page</title></head>  <body>Struts2添加成功!</body></html>
  3)在struts.xml中配置Action
    struts.xml
<package name="default" namespace="/" extends="struts-default">
<span style="white-space:pre"></span><!-- 配置测试用的Action,未与Spring整合,class属性写类的全限定名 --><action name="test" class="my.ssh.demo.test.TestAction"><result name="success">/test.jsp</result></action></package>
  4)发布项目到服务器,启动服务器
访问http://localhost:8080/SSH/test.action测试可以通过test.action正常访问test.jsp页面和是否输出"===> TextAction.execute()"
  b.单独测试Spring
  1)新建SpringTest类
    SpringTest.java
package my.ssh.demo.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest {private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");@Testpublic void testBean() throws Exception {TestAction testAction = (TestAction) ac.getBean("testAction");System.out.println(testAction);}}
  2)向Spring容器注入bean(使用注解注入,修改TestAction.java)
    TestAction.java
package my.ssh.demo.test;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionSupport;@Controller@Scope("prototype")public class TestAction extends ActionSupport {@Overridepublic String execute() throws Exception {System.out.println("===> TextAction.execute()");return super.execute();}}
  3)修改applicationContext.xml
    applicationContext.xml
<!-- 自动扫描与装配bean --><context:component-scan base-package="my.ssh.demo.test"/>
  4)运行SpringTest类中的testBean()方法
测试可以输出Spring容器中名为testAction的bean实例
  c.整合Spring与Struts2
  1)在web.xml配置Spring的监听器
    web.xml
<!-- 配置Spring的用于初始化容器对象的监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext*.xml</param-value></context-param>
  2)加入Struts2与Spring整合的一个jar包
struts2-spring-plugin-2.3.30.jar
  3)修改struts.xml
    struts.xml
<package name="default" namespace="/" extends="struts-default"><!--配置测试用的Action,当Struts2与Spring整合后的,class属性可以写bean的名称--><action name="test" class="testAction"><result name="success">/test.jsp</result></action></package>
  4)修改test.jsp
    test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html><head>
<span style="white-space:pre"></span><title>My JSP 'test.jsp' starting page</title></head>  <body>
<span style="white-space:pre"></span>Struts2添加成功!<br/>Struts2与Spring整合成功!</body></html>
  5)发布项目到服务器,启动服务器
访问http://127.0.0.1:8080/SSH/test.action测试可以通过test.action正常访问test.jsp页面和是否输出"===> TextAction.execute()"
B.Spring与Hibernate整合
  1)配置applicationContext.xml
    applicationContext.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: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-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 自动扫描与装配bean --><context:component-scan base-package="my.ssh.demo.test"/><!-- 导入外部的properties文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!-- 指定Hibernate的配置文件位置 --><property name="configLocation" value="classpath:hibernate.cfg.xml"/><!-- 配置c3p0数据库连接池 --><property name="dataSource"><bean class="com.mchange.v2.c3p0.ComboPooledDataSource"><!-- 数据库连接信息 --><property name="driverClass" value="${driverClass}"/><property name="jdbcUrl" value="${jdbcUrl}"/><property name="user" value="${user}"/><property name="password" value="${password}"/><!-- 其他配置 --><!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --><property name="initialPoolSize" value="3"/><!--连接池中保留的最小连接数。Default: 3 --><property name="minPoolSize" value="3"/><!--连接池中保留的最大连接数。Default: 15 --><property name="maxPoolSize" value="5"/><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --><property name="acquireIncrement" value="3"/><!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --><property name="maxStatements" value="8"/><!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --><property name="maxStatementsPerConnection" value="5"/><!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --><property name="maxIdleTime" value="1800"/></bean></property></bean><!-- 配置声明式事务管理(采用注解的方式) --><bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><tx:annotation-driven transaction-manager="txManager"/></beans>
  2)修改hibernate.cfg.xml
    hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory>
<span style="white-space:pre"></span><!-- 1.数据库连接信息 --><property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- 2.其他配置 --><property name="show_sql">true</property><property name="format_sql">true</property><property name="hbm2ddl.auto">update</property><!-- 告诉Hibernate让它不使用单纯的JDBC连接,而使用连接池的方式,因为Hibernate默认是使用jdbc方式来获取值 --><property name="hibernate.temp.use_jdbc_metadata_defaults">false</property><!-- 3.导入映射文件 --><!-- <mapping resource="" /> --></session-factory></hibernate-configuration>
  3)在src下新建jdbc.properties
    jdbc.properties
driverClass=com.mysql.jdbc.DriverjdbcUrl=jdbc\:mysql\://localhost\:3306/testdb?useUnicode\=true&characterEncoding\=utf8user=rootpassword=123
  4)在SpringTest类添加测试testSessionFactory()方法
    SpringTest.java
//测试SessionFactory@Testpublic void testSessionFactory() throws Exception {SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");System.out.println(sessionFactory);}
  5)运行SpringTest类中的testSessionFactory()方法
测试可以输出Spring容器中名为sessionFactory的bean的实例
  6)新建Person类
    Person.java
package my.ssh.demo.domain;public class Person {private Long id;private String name;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
  7)修改Person.hbm.xml,和Person.java放在同一包下
    Person.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 package="my.ssh.demo.domain"><class name="Person" table="test_person"><id name="id">
<span style="white-space:pre"></span><generator class="native"/></id><property name="name"/></class></hibernate-mapping>
  8)将Person.hbm.xml映射文件加入到hibernate.cfg.xml中
    hibernate.cfg.xml
<!-- 3.导入映射文件 --><mapping resource="my/ssh/demo/domain/Person.hbm.xml" />
  9)新建TestService类
    TestService.java
package my.ssh.demo.test;import javax.annotation.Resource;import javax.transaction.Transactional;import my.ssh.demo.domain.Person;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.stereotype.Service;@Service("testService")public class TestService {@Resourceprivate SessionFactory sessionFactory;@Transactionalpublic void saveTwoPersons(){Session session = sessionFactory.getCurrentSession();session.save(new Person());int a = 1 / 0;//这行会抛异常session.save(new Person());}}
  10)在SpringTest类添加测试testTransaction()方法
    SpringTest.java
//测试事务@Testpublic void testTransaction() throws Exception {TestService testService = (TestService) ac.getBean("testService");System.out.println(testService);}
  11)运行SpringTest类中的testTransaction()方法
测试抛异常,查看数据库没有记录,说明事务回滚将TestService.java中的int a = 1 / 0;注释掉之后再运行SpringTest类中的testTransaction()方法测试运行成功,数据库有记录,说明事务已提交,每次测试表中数据id都会间隔1
C.Struts2与Spring与Hibernate整合
  1)修改TestAction类
    TestAction.java
package my.ssh.demo.test;import javax.annotation.Resource;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionSupport;@Controller@Scope("prototype")public class TestAction extends ActionSupport {@Resourceprivate TestService testService;@Override
<span style="white-space:pre"></span>public String execute() throws Exception {
<span style="white-space:pre"></span>System.out.println("===> TextAction.execute()");
<span style="white-space:pre"></span>testService.saveTwoPersons();
<span style="white-space:pre"></span>return super.execute();}}
  2)修改test.jsp
    test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
<span style="white-space:pre"></span><head><title>My JSP 'test.jsp' starting page</title></head><body>Struts2添加成功!<br/>Struts2与Spring整合成功!<br/>Struts2与Spring与Hibernate整合成功!</body></html>
  3)发布项目到服务器,启动服务器
访问http://127.0.0.1:8080/SSH/test.action测试通过test.action正常访问test.jsp页面和输出"===> TextAction.execute()",查看数据表有数据,事务已提交!再将TestService.java中的int a = 1 / 0;注释放开,访问http://127.0.0.1:8080/SSH/test.action抛异常,查看数据库表无数据,事务回滚!
项目整合成功,框架中的jar没有全部添加,视具体项目采用哪种技术再自行添加!
                                             
0 0
原创粉丝点击