2015年10月8日 星期四

spring4 + mybatis3 sample (簡易版) - 系統設定檔

想說 spring4 之前寫過,然後 ibatis 也寫了幾個~
那 spring4 + mybatis3 應該沒啥問題 (mybatis 以前是 ibatis)...
然後~就發現被表了Orz...

總之~還是湊了一個超簡易的版本~

jar lib,就是 spring4 的那一拖拉庫,
加上 mybatis-3-xx.jar(要找一下,他github是放 source,直接 build 好 release 的 jar 後來是在別的地方找到的)。
還有一個  mybatis-spring-xx.jar...
另外因為後端我習慣接 postgresql...但早期的 jdbc driver(8.4) 在此會發生 setQueryTimeOut method not implemented 的問題,總之~換成新的 driver(9.4) 就可以解掉(但是在那版才加的就不清楚)

首先講一下我的檔案位置

webapps/demo/WEB-INF/web.xml
                                         /classes
                                         /classes/mybatis/mybatis-config.xml     <-- mybatis 主要設定檔
                                         /classes/**/*Dao.xml        <-- mapper 內容的檔案
                                         /conf             <--  spring 設定檔區
                                         /lib

設定檔內容:
/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>anping</display-name>
    <!-- The definition of the Root Spring Container shared by all Servlets
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/conf/root-context.xml</param-value>
    </context-param>

    <!-- Sets the default profile to use in the absence of any profiles set
        at deployment time. -->
    <context-param>
        <param-name>spring.profile.default</param-name>
        <param-value>dev</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

   
    <!-- Handles requests into the application -->   
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/conf/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
   
       
    <security-constraint>
        <web-resource-collection>
            <url-pattern>/*</url-pattern>
            <http-method>PUT</http-method>
            <http-method>DELETE</http-method>
            <http-method>HEAD</http-method>
            <http-method>OPTIONS</http-method>
            <http-method>TRACE</http-method>
        </web-resource-collection>
        <auth-constraint></auth-constraint>
    </security-constraint>
</web-app>


Spring 主檔(application.xml)
/WEB-INF/root-context.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- Root Context: defines shared resources accessible to all other web
        components -->

    <!-- Turn on support for @Annotation-based configuration e.g. @Inject -->
    <context:annotation-config />

    <!-- Loads application properties -->
    <import resource="properties.xml" />
   
    <!-- Loads controller/component -->
    <import resource="servlet-context.xml" />
   
    <!-- Loads model(db) -->
    <import resource="data.xml" />
       
</beans>



servlet-context.xml、properties.xml 就spring 用的一般般沒啥問題~
主要差在 與 DB model 的設定
/WEB-INF/conf/data.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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:ctx="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

   
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>org.postgresql.Driver</value>           
        </property>
        <property name="url">           
            <value>jdbc:postgresql://127.0.0.1:5432/db1?charSet=utf-8</value>
        </property>
        <property name="username">
            <value>xxxxxxx</value>
        </property>
        <property name="password">
            <value>xxxxxxx</value>           
        </property>
        <property name="timeBetweenEvictionRunsMillis" value="300000" />
        <property name="numTestsPerEvictionRun" value="6" />
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <property name="initialSize" value="3" />
        <property name="maxActive" value="100" />
        <property name="maxIdle" value="10" />
        <property name="minIdle" value="1"/>
        <property name="maxWait" value="5000" />       
        <property name="poolPreparedStatements" value="true" />
        <property name="maxOpenPreparedStatements" value="100" />
    </bean>
   
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    </bean>
   
    <bean id="myBatisMapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="xxx.xxx.xxx.xxx"/>
       <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>
   
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean> 
   
   
</beans>


重點在  sqlSessionFactory 要指向 dataSource 和  configLocation
然後因為我要利用 annoation 的寫法~所以有加 myBatisMapperScanner
basePackage 必填, 但他不同於 spring 可以 ** 的用法,好像~只要寫入 上幾層的 pacakge就行了(跟 log4j 的 package 制定法較像)
annotationClass ,一般通用就是 spring 的 Repository
總合來說,就是~ class 有  @Repository 且 在  basePackage下 的就會被自動掃進來~

mybatis 的主設定檔
是說原本想把他搬去跟 conf 同一層,但是google了一下,有人說~把這個檔放在外面是有什麼理由嗎!?
後來我想想也是~因為他裡面還是會因需要一直加 mapper resourse 進來~那些內容也得跟程式對應。在 src 底下是比較好維護~(conf一般都是指系統架構整體用的 config,的確是不要去搞一個客制的比較好)
/WEB-INF/classes/mybatis/mybatis-config.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>
    <settings>
        <setting name="cacheEnabled" value="true" />
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="multipleResultSetsEnabled" value="true" />
        <setting name="useColumnLabel" value="true" />
        <setting name="defaultExecutorType" value="REUSE" />
        <setting name="defaultStatementTimeout" value="25000" />
    </settings>

    <mappers>
          <mapper resource="xxx/xxx/xxx/xxx/sample/SampleConfigDao.xml"/>
      </mappers>
</configuration>


後續~


沒有留言: