SpringMVC+MyBatis+C3P0连接池详细配置

关于SpringMVC+MyBatis+C3P0连接池结合的框架做了一个配置:

首先导入相关的架包:

SpringMVC+MyBatis+C3P0连接池详细配置_第1张图片SpringMVC+MyBatis+C3P0连接池详细配置_第2张图片

其中有些可以删除,像commons-fileupload-1.2.1.jar是做上传用的,我们可以扔掉,还有一些看自己所需吧。

配置web.xml:


 
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
		

	
		org.springframework.web.context.ContextLoaderListener
	
    
		contextConfigLocation
		classpath:applicationContext.xml
	
                forceEncoding
                true
                
  CharacterEncodingFilter
  /*


 
  

	
		springMVC
		org.springframework.web.servlet.DispatcherServlet
		  
        contextConfigLocation  
        classpath:springMVC-servlet.xml  
          
		1
	
	
		springMVC
		*.do
	

在配置Spring文件之前,我们要先配置一下属性文件:jdbc.properties(这里用的oracle数据库)

datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
datasource.username=dglt
datasource.password=12345


c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=60
c3p0.minPoolSize=5
c3p0.maxPoolSize=100
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=60


Spring相关文件配置(applicationContext.xml):

1)配置c3p0连接池:


        
	        
	            classpath:jdbc.properties
	        
        

    
    
    
        
            ${datasource.driverClassName}
        
        
            ${datasource.url}
        
        
            ${datasource.username}
        
        
            ${datasource.password}
        
         
        
            ${c3p0.acquireIncrement}
        
         
        
            ${c3p0.initialPoolSize}
        
        
            ${c3p0.minPoolSize}
        
        
            ${c3p0.maxPoolSize}
        
         
        
            ${c3p0.maxIdleTime}
        
         
        
            ${c3p0.idleConnectionTestPeriod}
        
         
        
            ${c3p0.maxStatements}
        
         
        
            ${c3p0.numHelperThreads}
        
    
 2)创建SqlSessionFactory,同时指定数据源

          
	       
	      
 
3)配置模板类 这个是mybatis提供的一个辅助模板,用来获取session


	
4)配置事务驱动

   
    	
 
5)配置自动扫描(将dao层和services层扫进来即可)

 


配置和前台相关的xml(springMVC-servlet.xml)



 
 
  
    


    
    
    

     
    
        
          
        
    
      
     
 

配置MyBatis sql映射文件(SqlMapConfig.xml):

  
  
       
       	
       		
       		
       	
      		 
       		
       			  
       			  
       		
       		
       		
       		
       			  
       			  
       		
       


  
Mapper配置(相关的sql):

 







 














 




insert into hetong(id,jzmc,hetong)values(#{id},#{jzmc},#{hetong})



  
        SELECT HETONG_SEQ.Nextval  FROM DUAL  
   
	insert into  
	hetong(
	id,
	jzmc,
	hetong,
	qibie_id,
	quyu_id,
	zhenqu_id,
	mianji,
	dizhi,
	yzmc,
	yzdh) 
	values
	(
	#{id,jdbcType=VARCHAR},
	#{jzmc,jdbcType=VARCHAR},
	#{hetong,jdbcType=VARCHAR},
	#{qibie_id,jdbcType=VARCHAR},
	#{quyu_id,jdbcType=VARCHAR},
	#{zhenqu_id,jdbcType=VARCHAR},
	#{mianji,jdbcType=VARCHAR},
	#{dizhi,jdbcType=VARCHAR},
	#{yzmc,jdbcType=VARCHAR},
	#{yzdh,jdbcType=VARCHAR}
	)











 


delete from hetong where id=#{id111}




delete from hetong where id in

#{item}




	update hetong 
	
		jzmc=#{jzmc},
		hetong=#{hetong},
		qibie_id=#{qibie_id},
		quyu_id=#{quyu_id},
		zhenqu_id=#{zhenqu_id},
		mianji=#{mianji},
		dizhi=#{dizhi},
		yzmc=#{yzmc},
		yzdh=#{yzdh},
	
	where id=#{id}






 
  
 大致的相关配置差不多就这些了。 
  


 

你可能感兴趣的:(Java)