SSH框架集成环境搭建
- 前言:
对于初学SSH框架的ITor来说,学完了3个框架之后最渴望做的事情就是整合3个框架,就是我们常说的SSH框架整合;可是,初学者可能对于SSH搭建没有头绪,即便是课上听讲,课后查阅资料也是没能搭建成功;你可能是因为知识学的还不够扎实,也可能是因为你的Jar有冲突,也可能是因为你没有有效的利用百度谷歌搜索工具的习惯以及进行解决问题的耐心。
但是幸运的是,你看到了这篇文章;本文将一步一步地解析SSH框架的搭建,并且已经提供所需的原材料,就是Jar包;好了,前(废)言(话)就说这么多吧!开始SSH搭建之旅ing...
2018-06-11 00:00:00
- 版本说明:
框架以及应用名称 | 版本 |
Struts2 | struts-2.3.34 |
Spring | spring-framework-4.3.4.RELEASE |
Hibernate | hibernate-release-5.0.1.Final |
MySQL | 8.0 |
- 准备Jar包:下载原材料[SSH整合Jar包.rar]
- 首先,新建一个Dynamic Web Project 项目;一般的,习惯于将编码改为UTF-8格式。
- S(truts2) + S(pring) + H(ibernate) 先进行S(pring) + H(ibernate)整合:
- 导入Jar包:先导入S(pring) + H(ibernate)的Jar包,整合测试成功后再导入Struts2的jar包;
1 spring-beans-4.3.4.RELEASE.jar 2 spring-context-4.3.4.RELEASE.jar 3 spring-core-4.3.4.RELEASE.jar 4 spring-expression-4.3.4.RELEASE.jar 5 commons-logging-1.2.jar 6 log4j-1.2.17.jar
1 spring-aspects-4.3.4.RELEASE.jar 2 spring-aop-4.3.4.RELEASE.jar 3 aspectjweaver.jar 4 aopalliance-1.0.jar
1 spring-tx-4.3.4.RELEASE.jar 2 spring-jdbc-4.3.4.RELEASE.jar
1 spring-test-4.3.4.RELEASE.jar
1 spring-orm-4.3.4.RELEASE.jar
1 spring-web-4.3.4.RELEASE.jar
1 详细9个Jar在[hibernate-release-5.0.1.Final\lib\required]中
1 slf4j-log4j12-1.7.2.jar
1 ehcache-core-2.4.3.jar 2 hibernate-ehcache-5.0.1.Final.jar 3 slf4j-api-1.6.1.jar
1 c3p0-0.9.2.1.jar 2 hibernate-c3p0-5.0.1.Final.jar 3 mchange-commons-java-0.2.3.4.jar
1 hibernate-entitymanager-5.0.1.Final.jar
1 mysql-connector-java-8.0.11.jar
1 junit-4.12.jar 2 hamcrest-library-1.3.jar 3 hamcrest-core-1.3.rc2.jar
- 搭建Spring环境:
当Web项目部署到Tomcat中时,Spring容器被加载;若要完成此方案可在web.xml中添加Spring核心的监听器;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>CRMRelease</display-name> 4 <!-- 配置Struts2核心过滤器 --> 5 <filter> 6 <filter-name>struts2</filter-name> 7 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 8 </filter> 9 <filter-mapping> 10 <filter-name>struts2</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping> 13 <welcome-file-list> 14 <welcome-file>index.html</welcome-file> 15 <welcome-file>index.htm</welcome-file> 16 <welcome-file>index.jsp</welcome-file> 17 <welcome-file>default.html</welcome-file> 18 <welcome-file>default.htm</welcome-file> 19 <welcome-file>default.jsp</welcome-file> 20 </welcome-file-list> 21 </web-app>
- 接着在classpath路径src目录下创建classpath:applicationContext.xml配置;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 15 16 17 18 </beans>
- 在 applicationContext.xml中开启注解扫描,以便于Spring通过注解的方式管理类;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 15 16 17 <!-- Spring容器创建,扫描的包 --> 18 <context:component-scan base-package="com.zhangpn.daoImpl"></context:component-scan> 19 <context:component-scan base-package="com.zhangpn.serviceImpl"></context:component-scan> 20 <context:component-scan base-package="com.zhangpn.domain"></context:component-scan> 21 22 23 </beans>
- 接下来配置hibernateTemplate;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 15 16 17 <!-- Spring容器创建,扫描的包 --> 18 <context:component-scan base-package="com.zhangpn.daoImpl"></context:component-scan> 19 <context:component-scan base-package="com.zhangpn.serviceImpl"></context:component-scan> 20 <context:component-scan base-package="com.zhangpn.domain"></context:component-scan> 21 22 <!-- 配置hibernateTemplate --> 23 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> 24 <property name="sessionFactory" ref="sessionFactory"></property> 25 </bean> 26 27 </beans>
- 在配置hibernateTemplate时,需要注入sessionFactory;
- 接下来配置sessionFactory;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 15 16 17 <!-- Spring容器创建,扫描的包 --> 18 <context:component-scan base-package="com.zhangpn.daoImpl"></context:component-scan> 19 <context:component-scan base-package="com.zhangpn.serviceImpl"></context:component-scan> 20 <context:component-scan base-package="com.zhangpn.domain"></context:component-scan> 21 22 <!-- 配置hibernateTemplate --> 23 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> 24 <property name="sessionFactory" ref="sessionFactory"></property> 25 </bean> 26 27 <!-- 配置sessionFactory --> 28 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 29 <property name="dataSource" ref="dataSource"></property> 30 <property name="hibernateProperties"> 31 <props> 32 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 33 <prop key="hibernate.show_sql">true</prop> 34 <prop key="hibernate.format_sql">true</prop> 35 <prop key="hibernate.hbm2ddl.auto">update</prop> 36 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> 37 </props> 38 </property> 39 <property name="packagesToScan"> 40 <array> 41 <value>com.zhangpn.domain</value> 42 </array> 43 </property> 44 </bean> 45 46 </beans>
- 配置sessionFactory需要注入dataSource数据源;
- 接下来配置dataSource数据源;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 15 16 17 <!-- Spring容器创建,扫描的包 --> 18 <context:component-scan base-package="com.zhangpn.daoImpl"></context:component-scan> 19 <context:component-scan base-package="com.zhangpn.serviceImpl"></context:component-scan> 20 <context:component-scan base-package="com.zhangpn.domain"></context:component-scan> 21 22 <!-- 配置hibernateTemplate --> 23 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> 24 <property name="sessionFactory" ref="sessionFactory"></property> 25 </bean> 26 27 <!-- 配置sessionFactory --> 28 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 29 <property name="dataSource" ref="dataSource"></property> 30 <property name="hibernateProperties"> 31 <props> 32 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 33 <prop key="hibernate.show_sql">true</prop> 34 <prop key="hibernate.format_sql">true</prop> 35 <prop key="hibernate.hbm2ddl.auto">update</prop> 36 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> 37 </props> 38 </property> 39 <property name="packagesToScan"> 40 <array> 41 <value>com.zhangpn.domain</value> 42 </array> 43 </property> 44 </bean> 45 46 <!-- 配置dataSource --> 47 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 48 <property name="driverClass" value="${driverClass}"></property> 49 <property name="jdbcUrl" value="${jdbcUrl}"></property> 50 <property name="user" value="${user}"></property> 51 <property name="password" value="${password}"></property> 52 </bean> 53 </beans>
- 在src目录下创建jdbc.properties属性配置文件保存我们连接数据库的一些信息引入到配置元中去;
- 接下来配置jdbc.properties属性配置文件;
1 driverClass=com.mysql.cj.jdbc.Driver 2 jdbcUrl=jdbc:mysql://localhost:3306/ssh?serverTimezone=GMT&useSSL=false 3 user=root 4 password=zhangpn
- 将jdbc.properties属性配置文件添加到applicationContext.xml中:
<context:property-placeholder location="classpath:jdbc.properties"/>
- 接下来Spring计划通过注解的方式声明式事务
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 15 16 17 <!-- Spring容器创建,扫描的包 --> 18 <context:component-scan base-package="com.zhangpn.daoImpl"></context:component-scan> 19 <context:component-scan base-package="com.zhangpn.serviceImpl"></context:component-scan> 20 <context:component-scan base-package="com.zhangpn.domain"></context:component-scan> 21 22 <!-- 配置hibernateTemplate --> 23 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> 24 <property name="sessionFactory" ref="sessionFactory"></property> 25 </bean> 26 27 <!-- 配置sessionFactory --> 28 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 29 <property name="dataSource" ref="dataSource"></property> 30 <property name="hibernateProperties"> 31 <props> 32 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 33 <prop key="hibernate.show_sql">true</prop> 34 <prop key="hibernate.format_sql">true</prop> 35 <prop key="hibernate.hbm2ddl.auto">update</prop> 36 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> 37 </props> 38 </property> 39 <property name="packagesToScan"> 40 <array> 41 <value>com.zhangpn.domain</value> 42 </array> 43 </property> 44 </bean> 45 46 <!-- 配置dataSource --> 47 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 48 <property name="driverClass" value="${driverClass}"></property> 49 <property name="jdbcUrl" value="${jdbcUrl}"></property> 50 <property name="user" value="${user}"></property> 51 <property name="password" value="${password}"></property> 52 </bean> 53 54 <context:property-placeholder location="classpath:jdbc.properties"/> 55 56 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 57 <property name="sessionFactory" ref="sessionFactory"></property> 58 </bean> 59 <tx:annotation-driven transaction-manager="transactionManager"/> 60 </beans>
- 接下来,创建一个客户实体类:Customer
1 package com.zhangpn.domain; 2 3 import java.io.Serializable; 4 5 import javax.persistence.Column; 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.GenerationType; 9 import javax.persistence.Id; 10 import javax.persistence.Table; 11 12 /** 13 * 客户实体类 14 * 15 * @author Administrator 16 * 17 */ 18 19 @Entity 20 @Table(name="cst_customer") 21 public class Customer implements Serializable { 22 23 @Id 24 @Column(name="cust_id") 25 @GeneratedValue(strategy=GenerationType.IDENTITY) 26 private Long custId; 27 28 @Column(name="cust_name") 29 private String custName; 30 31 @Column(name="cust_source") 32 private String custSource; 33 34 @Column(name="cust_industry") 35 private String custIndustry; 36 37 @Column(name="cust_level") 38 private String custLevel; 39 40 @Column(name="cust_address") 41 private String custAddress; 42 43 @Column(name="cust_phone") 44 private String custPhone; 45 46 public Long getCustId() { 47 return custId; 48 } 49 50 public void setCustId(Long custId) { 51 this.custId = custId; 52 } 53 54 public String getCustName() { 55 return custName; 56 } 57 58 public void setCustName(String custName) { 59 this.custName = custName; 60 } 61 62 public String getCustSource() { 63 return custSource; 64 } 65 66 public void setCustSource(String custSource) { 67 this.custSource = custSource; 68 } 69 70 public String getCustIndustry() { 71 return custIndustry; 72 } 73 74 public void setCustIndustry(String custIndustry) { 75 this.custIndustry = custIndustry; 76 } 77 78 public String getCustLevel() { 79 return custLevel; 80 } 81 82 public void setCustLevel(String custLevel) { 83 this.custLevel = custLevel; 84 } 85 86 public String getCustAddress() { 87 return custAddress; 88 } 89 90 public void setCustAddress(String custAddress) { 91 this.custAddress = custAddress; 92 } 93 94 public String getCustPhone() { 95 return custPhone; 96 } 97 98 public void setCustPhone(String custPhone) { 99 this.custPhone = custPhone; 100 } 101 102 @Override 103 public String toString() { 104 return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource 105 + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress 106 + ", custPhone=" + custPhone + "]"; 107 } 108 109 }
- 实现了Serializable接口,通过注解的方式生成表以及主键策略,并提供get/set方法。
- 创建一个Dao接口:ICustomerDao
1 package com.zhangpn.dao; 2 3 import java.util.List; 4 5 import org.hibernate.criterion.DetachedCriteria; 6 7 import com.zhangpn.domain.Customer; 8 9 /** 10 * 客户Dao接口 11 * @author Administrator 12 * 13 */ 14 public interface ICustomerDao { 15 16 /** 17 * 查找全部 18 * @param dCriteria 19 * @return 20 */ 21 List<Customer> findAll(DetachedCriteria dCriteria); 22 23 /** 24 * 保存客户 25 * @param customer 26 */ 27 void save(Customer customer); 28 29 }
- 接口中提供了findAll、save:查找全部、保存客户的方法。
- 创建一个Dao实现类:CustomerDaoImpl
1 package com.zhangpn.daoImpl; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.hibernate.criterion.DetachedCriteria; 8 import org.springframework.orm.hibernate5.HibernateTemplate; 9 import org.springframework.stereotype.Repository; 10 11 import com.zhangpn.dao.ICustomerDao; 12 import com.zhangpn.domain.Customer; 13 14 /** 15 * 客户Dao接口实现类 16 * @author Administrator 17 * 18 */ 19 @Repository("customerDao") 20 public class CustomerDaoImpl implements ICustomerDao { 21 22 @Resource(name="hibernateTemplate") 23 private HibernateTemplate hibernateTemplate; 24 25 @Override 26 public List<Customer> findAll(DetachedCriteria dCriteria) { 27 return (List<Customer>) hibernateTemplate.findByCriteria(dCriteria); 28 } 29 30 @Override 31 public void save(Customer customer) { 32 hibernateTemplate.save(customer); 33 } 34 35 }
- 实现类中实现了ICustomerDao接口中的findAll、save:查找全部、保存客户的方法,并且通过注解装配了hibernateTemplate模板。
- 创建业务逻辑层接口:ICustomerService
1 package com.zhangpn.service; 2 3 import java.util.List; 4 5 import org.hibernate.criterion.DetachedCriteria; 6 7 import com.zhangpn.domain.Customer; 8 9 /** 10 * 客户业务逻辑层接口 11 * @author Administrator 12 * 13 */ 14 public interface ICustomerService { 15 /** 16 * 查找全部用户 17 * @param dCriteria 18 * @return 19 */ 20 List<Customer> findAllCustomer(DetachedCriteria dCriteria); 21 22 /** 23 * 保存用户 24 * @param customer 25 */ 26 public void saveCustomer(Customer customer); 27 }
- 创建客户业务逻辑层实现类:CustomerServiceImpl实现了业务逻辑。
1 package com.zhangpn.serviceImpl; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.hibernate.criterion.DetachedCriteria; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Propagation; 10 import org.springframework.transaction.annotation.Transactional; 11 12 import com.zhangpn.dao.ICustomerDao; 13 import com.zhangpn.domain.Customer; 14 import com.zhangpn.service.ICustomerService; 15 16 /** 17 * 客户业务逻辑层实现类 18 * @author Administrator 19 * 20 */ 21 @Service("customerService") 22 @Transactional(readOnly=true,propagation=Propagation.SUPPORTS) 23 public class CustomerServiceImpl implements ICustomerService { 24 25 @Resource(name="customerDao") 26 private ICustomerDao customerDao; 27 28 @Override 29 public List<Customer> findAllCustomer(DetachedCriteria dCriteria) { 30 return customerDao.findAll(dCriteria); 31 } 32 33 @Override 34 @Transactional(readOnly=false,propagation=Propagation.REQUIRED) 35 public void saveCustomer(Customer customer) { 36 customerDao.save(customer); 37 } 38 39 }
- 通过Junit进行测试:CustomerServiceTest
1 package com.zhangpn.Test; 2 3 import java.util.List; 4 5 import org.hibernate.criterion.DetachedCriteria; 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.test.context.ContextConfiguration; 10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 12 import com.zhangpn.domain.Customer; 13 import com.zhangpn.service.ICustomerService; 14 15 /** 16 * 测试 17 * 18 * @author Administrator 19 * 20 */ 21 @RunWith(SpringJUnit4ClassRunner.class) 22 @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) 23 public class CustomerServiceTest { 24 25 @Autowired 26 private ICustomerService customerService; 27 28 @Test 29 public void testFindAll() { 30 DetachedCriteria dCriteria = DetachedCriteria.forClass(Customer.class); 31 List<Customer> customers = customerService.findAllCustomer(dCriteria); 32 for (Customer customer : customers) { 33 System.out.println(customer); 34 } 35 } 36 37 @Test 38 public void testSave() { 39 Customer customer = new Customer(); 40 customer.setCustAddress("刘德华"); 41 customer.setCustId(4L); 42 customer.setCustIndustry("American"); 43 customer.setCustLevel("90"); 44 customer.setCustName("zhangpn"); 45 customer.setCustPhone("13654874562"); 46 customer.setCustSource("5"); 47 customerService.saveCustomer(customer); 48 } 49 }
- testFindAll()执行后,没有数据(原因是数据库没有这张表更没有数据),此时,ORM映射表已创建,手动插入数据,再次执行testFindAll()数据出现!
- 执行testSave方法后,再执行testFindAll方法,数据已经成功被插入数据库
此时,Spring与Hibernate算是已经搭建成功了。
- 接下来与Struts2进行整合
- 导入jar包:其中,javassit-3.11.0.GA.jar不导入,原因是之前已经导入过了;
- 本项目整合开始时在web.xml中添加了监听器[org.springframework.web.context.ContextLoaderListener]配置Spring核心的监听器;
- 本项目计划使用注解,因此不需要创建Struts.xml配置文件了,但是我们需要在web.xml中添加Struts2的过滤器;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>CRMRelease</display-name> 4 <!-- 配置Struts2核心过滤器 --> 5 <filter> 6 <filter-name>struts2</filter-name> 7 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 8 </filter> 9 <filter-mapping> 10 <filter-name>struts2</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping> 13 <!-- 配置Spring核心的监听器 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:applicationContext.xml</param-value> 17 </context-param> 18 <listener> 19 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 20 </listener> 21 <welcome-file-list> 22 <welcome-file>index.html</welcome-file> 23 <welcome-file>index.htm</welcome-file> 24 <welcome-file>index.jsp</welcome-file> 25 <welcome-file>default.html</welcome-file> 26 <welcome-file>default.htm</welcome-file> 27 <welcome-file>default.jsp</welcome-file> 28 </welcome-file-list> 29 </web-app>
- 接下来创建一个CustomerAction:
1 package com.zhangpn.action; 2 3 import java.util.List; 4 5 import javax.annotation.Resource; 6 7 import org.apache.struts2.convention.annotation.Action; 8 import org.apache.struts2.convention.annotation.Namespace; 9 import org.apache.struts2.convention.annotation.ParentPackage; 10 import org.apache.struts2.convention.annotation.Result; 11 import org.apache.struts2.convention.annotation.Results; 12 import org.hibernate.criterion.DetachedCriteria; 13 import org.springframework.context.annotation.Scope; 14 import org.springframework.stereotype.Controller; 15 16 import com.opensymphony.xwork2.ActionSupport; 17 import com.opensymphony.xwork2.ModelDriven; 18 import com.zhangpn.domain.Customer; 19 import com.zhangpn.service.ICustomerService; 20 21 /** 22 * 客户Action类 23 * 24 * @author Administrator 25 * 26 */ 27 @Controller("customerAction") 28 @Scope("prototype") 29 @ParentPackage("struts-default") 30 @Namespace("/customer") 31 @Results({ @Result(name = "addUI", type = "dispatcher", location = "/WEB-INF/jsp/customer/add.jsp"), 32 @Result(name = "findAll", type = "dispatcher", location = "/WEB-INF/jsp/customer/list.jsp"), 33 @Result(name = "listCustomer", type = "redirectAction", location = "findAllCustomer"),}) 34 public class CustomerAction extends ActionSupport implements ModelDriven<Customer> { 35 private Customer customer = new Customer(); 36 private List<Customer> customers; 37 @Resource(name = "customerService") 38 private ICustomerService customerService; 39 40 @Override 41 public Customer getModel() { 42 return customer; 43 } 44 45 /** 46 * 查询所有客户 47 * 48 * @return 49 */ 50 @Action("findAllCustomer") 51 public String findAllCustomer() { 52 DetachedCriteria dCriteria = DetachedCriteria.forClass(Customer.class); 53 customers = customerService.findAllCustomer(dCriteria); 54 return "findAll"; 55 } 56 57 /** 58 * 跳转到增加用户 59 * 60 * @return 61 */ 62 @Action("addUICustomer") 63 public String addUICustomer() { 64 return "addUI"; 65 } 66 67 /** 68 * 增加用户 69 * @return 70 */ 71 @Action("addCustomer") 72 public String addCustomer() { 73 customerService.saveCustomer(customer); 74 return "listCustomer"; 75 } 76 77 public List<Customer> getCustomers() { 78 return customers; 79 } 80 81 public void setCustomers(List<Customer> customers) { 82 this.customers = customers; 83 } 84 85 }
- CustomerAction类继承了ActionSupport实现了ModelDriven接口
- 按照结果集,新增2个视图界面:
- 在WEB-INF目录中创建jsp目录,在jsp目录中创建customer目录,在customer目录中创建add.jsp和list.jsp文件
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Add a new customer</title> 8 </head> 9 <body> 10 <form action="addCustomer" method="post"> 11 address<input type="text" name="custAddress"/><br/> 12 industry<input type="text" name="custIndustry"/><br/> 13 level<input type="text" name="custLevel"/><br/> 14 name<input type="text" name="custName"/><br/> 15 phone<input type="text" name="custPhone"/><br/> 16 source<input type="text" name="custSource"/><br/> 17 <input type="submit" value="add"/> 18 </form> 19 </body> 20 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib uri="/struts-tags" prefix="s" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <table> 12 <s:iterator value="customers"> 13 <tr> 14 <td>${ custId }</td> 15 <td>${ custName }</td> 16 <td>${ custSource }</td> 17 <td>${ custIndustry }</td> 18 <td>${ custLevel }</td> 19 <td>${ custAddress }</td> 20 <td>${ custPhone }</td> 21 </tr> 22 </s:iterator> 23 </table> 24 </body> 25 </html>
- 在WebContent新建一个主页跳转
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Hello</title> 8 </head> 9 <body> 10 CRMRelease<br/><br/><br/> 11 <a href="/CRMRelease/customer/addUICustomer">add 进入添加客户界面</a><br/><br/> 12 <a href="/CRMRelease/customer/findAllCustomer">findAll 查看所有客户界面</a> 13 </body> 14 </html>
接下来进行测试
点击之后
点击add增加
已经添加进了数据库中,致此,SSH搭建已经结束了。
凝一眸碧水,拈一缕清风,于一怀静谧中倾听凡尘的落音。
不再奢求什么,做简单的自己,过简单的生活,心在,梦在,你在,便是光阴赐予我的最美。