1. 程式人生 > >c3p0+spring3+mybatis3的整合配置檔案示例

c3p0+spring3+mybatis3的整合配置檔案示例

環境描述:  

  mybatis的jar包:mybatis-3.1.1.jar(mybatis自身功能包),mybatis-spring-1.1.1.jar(mybatis和spring的整合包)

  c3p0的jar包:c3p0-0.9.1.jar。

首先配置好mybatis的*Mapper.xml檔案:

複製程式碼
<?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="org.jston.sql.interfaces.EasyMapper"> <resultMap id="Easymap" type="org.jston.entity.Easy"> <!--id對映 --> <id property="eid" column="eid" /> <!--普通屬性對映 --> <result property="ename" column="ename" /> <
result property="epassword" column="epassword" /> </resultMap> <select id="getById" parameterType="int" resultMap="Easymap"> select * from Easy where eid = #{id} </select> </mapper>
複製程式碼

上述程式碼描述了一張表對映到mybatis中的基本結構,resultMap節點的type屬性是資料庫表所對應的實體類,<id>節點則是描述資料庫作為標識列對映到Java實體類對應的欄位。<result>則是一般欄位。<select />顧名思義,是配置一個查詢。

Java實體類如下:

複製程式碼
package org.jston.entity;

public class Easy {

    private Integer eid;
    private String ename;
    private String epassword;
    public Integer getEid() {
        return eid;
    }
    public void setEid(Integer eid) {
        this.eid = eid;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public String getEpassword() {
        return epassword;
    }
    public void setEpassword(String epassword) {
        this.epassword = epassword;
    }
}
複製程式碼

現在對mybatis的Mapper配置實體類應該Ok了,接下來就是Configration.xml了,這是mybatis的主配置檔案,由於現在要將spring和mybatis整合,所以mybatis的主配置檔案的意義只是用來管理mybatis的Mapper.xml檔案而已,具體內容如下:

複製程式碼
<?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>
    <mappers>
        <mapper resource="resouce/EasyMapper.xml" />
    </mappers>

</configuration>
複製程式碼

只需要配置一下Mapper即可,關於datasource的配置在spring中配置。

到此mybatis的簡單配置就Ok了。

下面就是配置spring的application.xml檔案了    

複製程式碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation=" 
       http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/tx  
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
       http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">



    <!-- dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
        <property name="jdbcUrl"
            value="jdbc:sqlserver://localhost:1433; DatabaseName=ey"></property>
        <property name="user" value="mybatis"></property>
        <property name="password" value="mybatis"></property>
    </bean>

    <!-- 使用spring的會話管理 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:Configration.xml" />
    </bean>
    
    <!-- 使用spring的事務管理 -->
    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置當出現Exception、RuntimeException、Exception時採用對應的事務操作 -->
    <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
            <tx:method name="insert*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.RuntimeException" />
            <tx:method name="update*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="select*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>

</beans>
複製程式碼

以上就是spring和mybatis的配置檔案整合示例。

接下來就是呼叫Mybatis上面配置好德select語句了~

先建立一個Dao介面:

複製程式碼
package org.jston.sql.interfaces;

import org.jston.entity.Easy;

public interface EasyMapper {

    public Easy getById(int id);
}
    
複製程式碼

回頭看看上面的mybatis的*Mapper.xml檔案就會發現,Mapper節點的type屬性和現在定義的Dao介面型別一致,並且<select>節點的id和這個介面的方法名也一致,意味著等下我們可以通過呼叫這個介面的getById方法來呼叫mybatis對應的Sql語句。

main方法測試程式碼:

複製程式碼
package org.jston.mybatis;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.jston.entity.Easy;
import org.jston.sql.interfaces.EasyMapper;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mybatis_Test {

    public static void main(String[] args) throws IOException {

        //載入spring的配置檔案
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        //得到mybatis的SqlSessionFactory
        SqlSessionFactory obj = (SqlSessionFactory) app.getBean(
                "sqlSessionFactory", DefaultSqlSessionFactory.class);
        //建立mybatis的SqlSession
        SqlSession ses = obj.openSession();
        //得到在mybatis的*Mapper.xml檔案中配置好的Mapper節點
        EasyMapper mapper = ses.getMapper(EasyMapper.class);
        //呼叫mybatis的Id為getById對應的Sql語句
        Easy easy = mapper.getById(1);
        //列印輸出
        System.out.println(easy.getEname());
    }
}
複製程式碼