spring整合其他两个框架

Spring整合其他两大框架原理:
                            web层:用struts2+jsp然后Action对象交给Spring管理
                            service层:JavaBean直接交给Spring 管理
                            dao :hibernate中的sessionfactory和Session获得,aop事务都交给Spring管理都由Spring容器来创建和维护
                导包:
                       struts2:基本包+    struts2-spring-plugin-2.5.16是struts把Action对象交给Spring的插件如果没有Spring容器则会报错
                    Spring:基础包:core|bean.context,expression,logging,log4j.   web:-web,    aop:aop,aspect,aopweaving,aop联盟,事务:jdbc,tx,c3p0,orm,测试:-test,
                       hibernate:操作数据库的规范-entitymanager;
                    
                导入约束:
                    web应用单独配置Spring容器:在web 的xml配置如下:

 
                                        
                                                contextConfigLocation
                                                classpath*:/applicationContext3.xml
                                         

 web应用单独整合struts2:  在web 的xml配置如下:
                                        

                                 
                                         
                                              struts2
                                              
                                             org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
                                              
                                           
                                           
                                              struts2
                                              /*
                                           


  web单独整合hibernate :
                                    1.配置实体映射文件:
                                            

  
                                              
                                             
                                             
                                             
                                                    
                                                    
                                                        
                                                     This class contains the employee detail. 
                                                    
                                                    
                                                        
                                                                
                                                                  
                                                        
                                                        
                                                                
                                                            
                                                            
                                                    
                                             


                                    2.配置hibernate 配置文件:
                                            


                                                
                                                
                                                com.mysql.jdbc.Driver
                                                jdbc:mysql://localhost:3306/hibernate
                                                root
                                                123
                                                org.hibernate.dialect.MySQL5InnoDBDialect
                                                true
                                                 
                                                
                                            

spring整合struts:

导包:struts2-spring-plugin.jar是struts中的Action交于Spring容器
                  在struts.xml配置:
                                配置常量:struts.objectFactory=spring   :把action创建交给Spring容器
                                            struts.objectFactory.spring.autowise=name   ,Spring负者依赖注入属性
                            整合方式一:
                                         整合方案一:用原来的class属性来使用
                                                    由struts创建对象,Spring用来组装和管理    
                                        
                                            
                                            
                                                /index.jsp
                                            

                                        

                                    自动装配时其实就是属性的注入:必须提供setget方法,然后属性名与下的name一致,这样就可以交给Spring容器来创建管理对象
                            整合方式二(推荐使用):
                                在applicationContext.xml配置如下:class属性填写Spring中action对象的beanName,就是spring管理的xml中配置的bean的名字。完全有Spring来创建管理action的周期 注意;Spring不能自动组装,只能手动注入依赖属性
                                        
                                            
                                            
                                                
                                            

                                            
                                            

struts.xml如下:




		
		
		
		
		
		
			
			
				/index.jsp
			
			
			
				/index.jsp
			
		

引入连接池同时把池也交给spring来管理:

引入C3p0连接池:
									创建c3p0配置文件:
										在applicationContext.com读取到这个然后交给Spring容器注入到SessionFactory对象中
											
										
										
										
										
											
											
											
											
										
										
										
										
										
											
													
													org.hibernate.dialect.MySQL5InnoDBDialect
													true
													true
											
										

这样就是一个完整的spring整合struts的

spring整合hibernate:

Spring配置文件:
                              


                                    
                                    
                                        
                                            
                                                    update
                                                    com.mysql.jdbc.Driver
                                                    root
                                                    123
                                                    org.hibernate.dialect.MySQL5InnoDBDialect
                                                    true
                                                    true
                                            
                                        
                                        
                                        
                                             com/leo/domain/user.hbm.xml
                                        
                                        
                                        
                                             classpath:com/leo/domain
                                        
                                    

     
 扩大Session的作用域:
                                在web.xml中配置扩大session的作用域:
                                

 
                                     
                                          openSessionInView
                                          
                                        org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
                                          
                                       
                                       
                                          openSessionInView
                                          /*
                                       
/**
 * 通过用Spring提供的
 * @author leoi555
 *
 */
public class UserDaoImplByHibernateSupport extends HibernateDaoSupport implements UserDao{
	
	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		super.getHibernateTemplate().save(user);
	}

	@Override
	public void deleteUser(int id) {
		// TODO Auto-generated method stub
		
		SessionFactory sessionFactory=getHibernateTemplate().getSessionFactory();
		Session session=sessionFactory.openSession();
		User user=session.get(User.class, 3);//
		session.delete(user);//
		System.out.println("删除成功");
		session.close();
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User getUserbyId(Integer id) {
		// TODO Auto-generated method stub
		//执行查询
//		return getHibernateTemplate().execute(new HibernateCallback() {
//
//			@Override
//			public User doInHibernate(Session session) throws HibernateException {
//				// TODO Auto-generated method stub
//				String hql="from User where id=?";
//				Query createQuery = session.createQuery(hql);
//				createQuery.setParameter(0, id);
//				User user = createQuery.uniqueResult();
//				return user;
//			}
//		});
		//用criteria查询
		DetachedCriteria dc=DetachedCriteria.forClass(User.class);
		dc.add(Restrictions.eq("id",id));
		List list=(List) getHibernateTemplate().findByCriteria(dc);
		if (list!=null && list.size()>0) {
			return list.get(0);
		}else {
			return null;
		}
	}

	@Override
	public int getTotalCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List getAllUser() {
		// TODO Auto-generated method stub
		return null;
	}

}

测试:

/**
 * 通过交给Spring来实现
 * @author leoi555
 *
 */
public class HibernateSupportTest {
	private UserDao userDao;
	@Test
	public void delete() {
		ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
		userDao=(UserDao) context.getBean("userDao");
		User user=new User();
		user.setId(1);
		userDao.deleteUser(user.getId());
		System.out.println("删除成功");
	}
}

 

你可能感兴趣的:(框架)