最近在自学SSH这个老框架,很多朋友问我你为什么不直接学习SSM框架,我说想先拿SSH先试试手,而且现在有一些公司还依旧有SSH的应用市场,经过13天的学习终于将SSH整合在一起。不多说,进入正题。
一、SSH框架版本
(1)、Struts2采用的是2.5.16,主要的核心Jar包
这里不解释包的作用,Jar包的数量也是根据自己的需求,没有固定,这里只加入基础的Jar包
(2)、Hibernate采用5.2.12,核心Jar包
这里除了我用红线的圈起来的不是必要的,剩下都是基本的
(3)、最后是Spring5.0.7,核心Jar包
Spring的Jar在libs文件中,其中后缀不带javadoc、sources都需要导入,需要21个基础包
二、SSH整合容易出现的问题
(1)、项目结构
这里只做基础的项目,只是为做测试
(2)、测试IOC
测试类:
package com.CRM.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.CRM.service.ICustomerService;
/**
* 测试IOC
* @author Tom
*
*/
public class TestSpring {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ICustomerService service = (ICustomerService) ac.getBean("customerService");
service.findAllCustomer();
}
}
bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置service -->
<bean id="customerService" class="com.CRM.service.Impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<!-- 配置dao -->
<bean id="customerDao" class="com.CRM.dao.Impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
如果该测试通过了,证明你在IOC这一步中配置是正确的
(3)、测试Hibernate
测试类:
package com.CRM.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.CRM.domain.Customer;
/**
* hibernate测试
* @author Tom
*
*/
public class TestHibernate {
public static void main(String[] args) {
Customer c = new Customer();
c.setCustName("ssh整合");
Configuration configuration = new Configuration();
configuration.configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.getCurrentSession();
Transaction transaction = session.beginTransaction();
session.save(c);
transaction.commit();
session.close();
factory.close();
}
}
hibernate.cfg.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">密码</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/数据库?useUnicode=true&character=UTF-8</property>
<property name="hibernate.connection.username">账户</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/CRM/domain/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
(4)、整合Spring和hibernate
package com.CRM.dao.Impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import com.CRM.dao.ICustomerDao;
import com.CRM.domain.Customer;
/**
* 客户的持久层实现类
* @author Tom
*
*/
public class CustomerDaoImpl implements ICustomerDao{
@Autowired
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public List<Customer> findAllCustomer() {
System.out.println("123");
return (List<Customer>) hibernateTemplate.find("from Customer");
}
@Override
public Customer findCustomerBycustId() {
return null;
}
@Override
public void saveCustomer(Customer customer) {
hibernateTemplate.save(customer);
}
@Override
public void updateCustomer(Customer customer) {
hibernateTemplate.update(customer);
}
@Override
public void deleteCustomer(Integer custId) {
hibernateTemplate.delete(custId);
}
}
当你在Dao实现类中采用hibernateTemplate类时,在Spring容器中声明,仅配置到这一步时,没有配置事务,进行测试会出现一个问题
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置service -->
<bean id="customerService" class="com.CRM.service.Impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<!-- 配置dao -->
<bean id="customerDao" class="com.CRM.dao.Impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置sessionFactory
用Spring提供的一个sessionFactory:localSessionFactoryBean
创建SessionFactory有三部分必不可少的信息,三部分信息在hibernate主配置文件信息都有
把hibernate主配置文件信息的路径注入进来
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
测试类:
package com.CRM.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.CRM.domain.Customer;
import com.CRM.service.ICustomerService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:bean.xml"})
public class TestSpringsave {
@Autowired
private ICustomerService cs;
@Test
public void testSave() {
Customer customer = new Customer();
customer.setCustName("SSH");
cs.saveCustomer(customer);
}
}
当你运行这个测试的时候,你会发现测试中,会提示你没有事务
这个时候,各位不要急,这时候在Spring容器中开启事务,然后在hibernate.cfg.xml的文件注释掉这一句话,关闭当前绑定的session,这时候大多数人的问题已经得到了解决了,但是如果还是出现了问题,那怎么办?
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置service -->
<bean id="customerService" class="com.CRM.service.Impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<!-- 配置dao -->
<bean id="customerDao" class="com.CRM.dao.Impl.CustomerDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置sessionFactory
用Spring提供的一个sessionFactory:localSessionFactoryBean
创建SessionFactory有三部分必不可少的信息,三部分信息在hibernate主配置文件信息都有
把hibernate主配置文件信息的路径注入进来
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 扫描事务 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* com.CRM.service.impl.*.*(..))" id="pt1"/>
<!-- 建立切入点表达式和事务通知的关联 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">gyx123456</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/crm?useUnicode=true&character=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- <property name="hibernate.current_session_context_class">thread</property>-->
<mapping resource="com/CRM/domain/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
如果你注释过后,测试还是无法通过,恭喜你,你进入到了今天的主题了
依旧提示你没有事务,并且提示你只有读操作,并没有写操作,到这里我基本被气到了,到网上到处找解决方案,半天下来没有什么用,我试过很多方法,其中有说在web.xml中设置hibernate的过滤器,但是并没什么卵用,可能是命运吧,尴尬
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
同时我试了另一个办法,
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="checkWriteOperations" value="false">/<property>
</bean>
其实这个方法我觉得最不可靠的,因为如果要Spring容器中加入property属性,你必须有这个方法的setter方法,你进入源码看过后,会发现并没有这个setter的方法,最后终于让我找到了一个合适我的方法
<!-- 扫描事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
然后在Service层中开启事务@Transaction
这是我第一次写博客,如果有些方法确实能成功的,而我的做法有问题的,可以私信我。
最后祝大家都能成为非常优秀的程序员,继续努力中。。。。。