Spring與mybatis的整合實踐 – JAVA編程語言程序開發技術文章

今天第一次整合spring和mybatis,可謂是歷程坎坷,前後歷時3個小時才整合完成,各種錯誤加起來恐怕有幾十個,尤其是導包的環節。

現在吧今天的成果貼出來,也算是對得起自己的這五個小時瞭,下面是整體的結構

 

 

這是spring的控制文件applicationContext.xml(悲劇的事情就是居然把有關數據的配置都放到瞭這裡,這不是搶mybatis的飯碗嗎,搞得我花瞭三個小時才想到!)

[html] 
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  
 <!–創建jdbc數據源 –> 
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
                <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
                <property name="url" value="jdbc:oracle:****:@***.***.**.**:1521:*****" />  
                <property name="username" value="******" />   
                <property name="password" value="******" />   
      </bean> 
  
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
  <property name="dataSource" ref="dataSource" /> 
  <property name="configLocation" value="classpath:configuration.xml" /> 
 </bean> 
  
  <bean id="sqlSession"     class="org.mybatis.spring.SqlSessionTemplate">  
       <constructor-arg index="0" ref="sqlSessionFactory" />  
 </bean> 
 
 <bean id="customerTest" class="com.test.Test"> 
  <!–註入SqlSessionTemplate實例 –> 
  <property name="sqlSessionTemplate" ref="sqlSession" /> 
 </bean> 
</beans> 
</span> 
 

這是mybatis的配置文件configuration.xml,沒辦法,飯碗被spring搶瞭,就隻剩這點瞭

[html] 
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE configuration PUBLIC    
    "-//mybatis.org//DTD Config 3.0//EN"   
    "http://mybatis.org/dtd/mybatis-3-config.dtd">  
<configuration>   
    <typeAliases>   
        <typeAlias alias="Customer" type="com.test.Customer" />   
    </typeAliases>   
    <mappers>   
        <mapper resource="com/test/Customer.xml" />   
    </mappers>   
</configuration>  
 
 </span> 
這是數據映射文件customer.xml

[html] view plaincopy
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC    
    "-//mybatis.org//DTD Mapper 3.0//EN"   
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
<mapper namespace="Customer">   
    <select id="selectCustomer" parameterType="int" resultType="Customer"> 
        select * from tcor_crm_customer where CUSTID = #{id}   
    </select>   
</mapper>  </span> 

 

customer的POJO類就省瞭,setter,getter方法,超簡單,記著重寫toString方法就可以瞭

這是測試方法

[java] 
<span style="font-size:14px;">package com.test; 
 
import org.mybatis.spring.support.SqlSessionDaoSupport; 
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class Test extends SqlSessionDaoSupport{ 
 public static void main(String[] args) { 
  // TODO Auto-generated method stub 
  new Test().firstTest(); 
 } 
 public void firstTest() { 
  BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 
  Test test = (Test)factory.getBean("customerTest"); 
  Customer cus = (Customer)test.getSqlSession().selectOne("selectCustomer",10696); 
  System.out.println(cus); 
 } 

</span> 

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。