您当前的位置: 首页 >  ssh

Charge8

暂无认证

  • 1浏览

    0关注

    447博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

整合SSH三个框架

Charge8 发布时间:2018-09-24 09:39:05 ,浏览量:1

整合SpringMVC和Spring和Hibernate

新建一个maven项目sshweb: 

     SpringMVC和Spring:5.0.8

     Hibernate:5.2.17.Final

     mysql驱动:5.1.46

 

一、在 pom.xml 中导入包的依赖
  
  
		
			org.springframework
			spring-context
			5.0.8.RELEASE
		
		
			org.springframework
			spring-web
			5.0.8.RELEASE
		
		
			org.springframework
			spring-webmvc
			5.0.8.RELEASE
		
		
			org.springframework
			spring-tx
			5.0.8.RELEASE
		
		
			org.springframework
			spring-aspects
			5.0.8.RELEASE
		
		
			org.springframework
			spring-orm
			5.0.8.RELEASE
		
		
			org.springframework
			spring-jdbc
			5.0.8.RELEASE
		
		
        
			javax.servlet
			javax.servlet-api
			3.1.0
			provided
		
		
			javax.servlet.jsp
			jsp-api
			2.2
			provided
		

		
			log4j
			log4j
			1.2.17
		
		
			commons-logging
			commons-logging
			1.2
		
		
		
			org.apache.taglibs
			taglibs-standard-spec
			1.2.5
			bundle
		
		
			org.apache.taglibs
			taglibs-standard-impl
			1.2.5
			bundle
		
		
		
			org.hibernate
			hibernate-core
			5.2.17.Final
		
		
                
			org.hibernate
			hibernate-c3p0
			5.2.17.Final
		
		
		
			mysql
			mysql-connector-java
			5.1.46
		

			
		
			org.hibernate.validator
			hibernate-validator
			6.0.10.Final
		
		
		    org.hibernate
		    hibernate-validator-annotation-processor
		    6.0.10.Final
		

	        
	            com.fasterxml.jackson.core
	            jackson-core
	            2.9.6
	        
	        
	            com.fasterxml.jackson.core
	            jackson-annotations
	            2.9.6
	        
	        
	            com.fasterxml.jackson.core
	            jackson-databind
	            2.9.6
	        
	
                
                    commons-fileupload
                    commons-fileupload
                    1.3.3
                

	        
	            junit
	            junit
	            4.12
	            test
	        
	
	
	
		
			
				org.apache.felix
				maven-bundle-plugin
				true
			
		
	

 

二、web.xml中配置,将Spring(并整合Hibernate)和SpringMVC整合起来

       1、在web.xml中配置 contextLoaderListener ,并加入spring的配置文件 applicationContext.xml 。这样可以把service、dao、事务、缓存、以及和其它框架的整合放到spring的配置文件里面。

        2、在web.xml中配置 DispatcherServlet,并加入SpringMVC的的配置文件 springmvc.xml,

这时,两个配置文件中扫描的包有重合的时候出现某些bean会被初始化2次的问题:

解决方案:在扫描包的子节点下配置 exclude-filter 和 include-filter (见配置文件),

SpringMVC只扫描Controller和ControllerAdvice:

Spring排除扫描Controller和ControllerAdvice:

       1)web.xml :

	
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
        
            encoding
            utf-8
        
        
        
            forceEncoding
            true
        
    
    
        CharacterEncodingFilter
        /*
    

	
	
	
		contextConfigLocation
		classpath:spring.xml
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		springDispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springmvc.xml
		
		1
	
	
	
		springDispatcherServlet
		/
	
	
	
	
	
		hiddenHttpMethodFilter
		org.springframework.web.filter.HiddenHttpMethodFilter
	
	
		hiddenHttpMethodFilter
		/*
	
	
	
		openSessionInViewFilter
		org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
	
	
		openSessionInViewFilter
		/*
	

           2)spring.xml: 并整合Hibernate (orm使用*.hbm.xml),

使用JPA 注解映射实体类:https://mp.csdn.net/postedit/83475268


	
	
	
	
		
		
		
		
		
		
		
		
		
		
		
	
	
	
	
		
		
		
		
		
		
			
				${hibernate.dialect}
				${hibernate.show_sql}
				${hibernate.format_sql}
				${hibernate.hbm2ddl.auto}
			
		
	
	
	
	
		
		
	
	
	
	
		
	
	



	
	
		
			
			
			
			
		
	
	
	
		
		
	
	
	
	
		
	

        3)springmvc.xml :

	
	
	
	
	
	
	
		
		
	

	
	
		
		
	

	

	
	
	

	

	

三、需要的配置文件

      1)application.properties :

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sshweb?useUnicode=true&characterEncoding=utf8&useSSL=true
jdbc.user=root
jdbc.password=123456

c3p0.initialPoolSize=5
c3p0.acquireIncrement=5
c3p0.maxPoolSize=20
c3p0.minPoolSize=5
c3p0.maxStatements=200
c3p0.maxStatementsPerConnection=5

hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update

       2)log4j.properties :

log4j.rootLogger = debug,stdout, D
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.Threshold = INFO
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p %m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ./log4j.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%d %p %m%n

 

四、 运行项目测试

    新建一个UserController 类, 只会初始化一次。

@Controller
public class UserController {
	
	public UserController() {
		System.out.println("UserController 构造器初始化---");
	}
}

到此SSH整合就搞定了,小项目目录如下

      

 

 

关注
打赏
1664721914
查看更多评论
立即登录/注册

微信扫码登录

0.0428s