1. 程式人生 > >【框架整合】三、整合mybatis

【框架整合】三、整合mybatis

一、匯入jar包

所需jar包為       mybatis-3.2.0.jar
    mybatis-spring-1.3.0.jar

    
下載地址
    https://repo1.maven.org/maven2/org/mybatis/mybatis/
    https://repo1.maven.org/maven2/org/mybatis/mybatis-spring/

二、 spring配置 引入mybatis

在剛剛的資料來源配置檔案applicationContext-datasource中,加上如下配置
<!-- mybatis 配置 開始-->
	
	<!-- mybatis的sqlSession工廠:sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		 <!-- mybatis不用配置  configLocation -->
		<!-- <property name="configLocation" value="classpath:ibatis-config.xml"></property> -->
		<property name="mapperLocations" value="classpath*:com/**/mappers/**/*.xml" />
	</bean> 
	
	
	<!-- mybatis自動掃描載入對映檔案,介面 basePackage:需要掃描的包-->
	<!-- basePackage配置錯 異常Injection of autowired dependencies failed  No qualifying bean of type [XXXMapper]-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.mvc.*.dao"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
	
	<!-- mybatis 配置 結束-->
上面的最終的正確配置 這邊遇到很多坑 一開始使用的配置是sqlSessionFactory
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> ?
				<property name="basePackage" value="com.xxxx.dal.mapper" />?
				<property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory" />
				<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
			</bean>

結果報錯 java.lang.ClassNotFoundException: ${jdbc.driver}

後來改用sqlSessionFactoryBeanName注入就沒有問題(不要使用sqlSessionFactory屬性注入,使用sqlSessionFactoryBeanName注入),因為這時不會立即初始化sqlSessionFactory,傳入的只是名字,非bean,所以不會引發提前初始化問題
   
   特別注意 改由使用sqlSessionFactoryBeanName注入之後 把ref 換成value 否則報錯
   org.springframework.beans.factory.BeanCreationException: Error creating bean with name  

   'org.mybatis.spring.mapper.MapperScannerConfigurer#0'   
   ......

   no matching editors or conversion strategy found

   這點粗心的話容易被忽略掉

三、測試

1.使用generatorConfig.xml自動生成 實力類、dao、及對映檔案

詳見: http://blog.csdn.net/xiaxiaozhang/article/details/72851075

並新建service類和controller類

最終結構如圖

附帶一下 controller的程式碼 簡單書寫,只為測試

package com.mvc.mybatis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mvc.mybatis.dao.model.MyInfo;
import com.mvc.mybatis.dao.model.MyInfoCriteria;
import com.mvc.mybatis.service.MyTestService;
/**
 * mybatis測試
 * 
 * @author xiazhang
 * @date   2017-6-15
 */
@Controller
public class MyBatisTestController {
	
	private static Logger logger = LoggerFactory.getLogger(MyBatisTestController.class);
	
	@Autowired
	private MyTestService myTestService;

	@RequestMapping(value="/myTest" , method = RequestMethod.POST)
	public void myTest(HttpServletRequest request , HttpServletResponse response) throws Exception{
		MyInfoCriteria myInfo = new MyInfoCriteria();
		List<MyInfo> myInfoList = myTestService.getMyInfoList(myInfo);
		if(myInfoList != null){
			for (MyInfo myInfo2 : myInfoList) {
				logger.info("name:{};sex:{}",myInfo2.getName(),myInfo2.getSex());
			}
		}
	 
	}
	
	
	
	
}


啟動服務  傳送post請求  http://localhost:8090/mySpringmvcWeb/myTest

結果

[INFO ] 2017-06-17 19:03:35 com.mvc.mybatis.controller.MyBatisTestController name:xiazhang;sex:男
[INFO ] 2017-06-17 19:03:35 com.mvc.mybatis.controller.MyBatisTestController name:nihao;sex:女

success