spring整合mybatis

1.将SqlSessionFactory交给spring管理(单例模式),并由spring创建SqlSession对象
2.由spring来生成mapper代理对象(扫描包,mapper.xml工作都给spring完成)
开始整合:
一.pom依赖


	4.0.0
	cn.geepy
	spring_mybatis
	0.0.1-SNAPSHOT


	
		
		
			org.springframework
			spring-core
			4.3.2.RELEASE
		
		
			org.springframework
			spring-context
			4.3.2.RELEASE
		
		
			org.springframework
			spring-test
			4.3.2.RELEASE
		
		
			org.springframework
			spring-tx
			4.3.2.RELEASE
		
		
			org.springframework
			spring-beans
			4.3.2.RELEASE
		
		
			org.springframework
			spring-jdbc
			4.3.2.RELEASE
		
		
		
			org.mybatis
			mybatis
			3.2.8
		
		
			org.mybatis
			mybatis-spring
			1.3.0
		
		
		
			mysql
			mysql-connector-java
			5.1.38
		
		
		
			junit
			junit
			4.12
		
		
			org.slf4j
			slf4j-api
			1.7.12
		
		
			org.slf4j
			slf4j-log4j12
			1.7.12
		
		
			log4j
			log4j
			1.2.17
		
		
		
			net.sf.ehcache
			ehcache-core
			2.6.11
		
		
		
			org.mybatis.caches
			mybatis-ehcache
			1.1.0
		
	
	
	
		
			
				maven-compiler-plugin
				
					1.8
					1.8
				
			
		
		
			
				src/main/java
				
					**/*.properties
					**.*.xml
					**/*.xml
				
			
			
				src/main/resources
				
					**/*.xml
				
			
		
	

写好依赖后准备配置文件,mybatis的SqlMapConfig.xml,数据库连接信息db.properties以及spring的配置文件applicationContext-dao.xml

二.applicationContext-dao.xml
配置数据源(配置sqlSessionFactory中需要数据源),使用dbpc:


			commons-dbcp
			commons-dbcp
			1.4

加载配置文件和配置数据源及SqlSessionFactory





	
		
		
		
		
		
		
	

	
		
		
		
		
		
		

再加入扫描mapp的配置


	
			
		
		
		
	

三.其他配置文件
db.properties中配置数据源中需要的参数即可,SqlMapConfig.xml中不用写关于mapper代理,扫描包之类的了,只用按需要配置一些mybatis的全局参数(二级缓存,延迟加载等),定义别名等即可

你可能感兴趣的:(ssm)