1. 程式人生 > >spring整合Mybatis oracle資料庫連線配置(dbcp)

spring整合Mybatis oracle資料庫連線配置(dbcp)

Spring + struts + MyBatis 結合編碼demo

一.首先匯入相應的 Spring + struts + MyBatis jar包

 1.Spring + MyBatis :如下操作順序

1.1:首先建立資料庫連線:(oracle)

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="資料庫使用者名稱" />
<property name="password" value="資料庫使用者名稱密碼" />

</bean>

1.2:獲取sqlSession 物件:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定連線資源 -->
<property name="dataSource" ref="myDataSource" />
<!--指定對映檔案 -->
<property name="mapperLocations" value="classpath:org/great/sqlxml/SqlMapperUser.xml" />
</bean>

1.3: 獲取介面類:有兩種方法獲取 ,第一種手動新增獲取,第二種自動掃描獲取,(自動比較方便)

<!-- 手動新增介面的方式,通過ID 獲取每個sql對應的介面類 -->
 <bean id="stuMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property 
name="mapperInterface" value="org.great.interfaces.EmpetyInterface"></property> 
</bean>

<!-- 自動掃描各個Mapper介面,並註冊對應的MapperFactoryBean物件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.great.interfaces" />
</bean>

到這裡 Spring + MyBatis 的配置就都配置好了,接下來就是測試呼叫:

ApplicationContext apps = new ClassPathXmlApplicationContext("applicationcontext.xml");//獲取applicationcontext 物件並解析 applicationcontext.xml 

/*這種是自動掃描介面的方式獲取介面,getBean(“這裡寫介面類的類名 首字母改成小寫就ok”) 如果是手動新增的話:getBean(“bean 的ID”) 

如:getbean("stuMapper");*/
EmpetyInterface ei = (EmpetyInterface)apps.getBean("empetyInterface");

二:spring + Struts

1. 配置struts.xml 在Struts基礎上加上框架的核心配置: 伺服器啟動時,通過監聽器初始化Spring的配置環境 監聽器,預設載入檔案是 applicationContext.xml

<!-- spring+struts 要配置例項spring -->
  <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</context-param>

<!-- 過濾器 -->
<filter>
 <filter-name >struts2 </filter-name >  
 <filter-class>  
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
    </filter-class>  
</filter>   
<filter-mapping>  
 <filter-name >struts2 </filter-name >  
 <url-pattern >/*</url-pattern >  
</filter-mapping>

2.配置applicationContext.xml 

<!-- struts2與Spring 整合 -->
 <context:component-scan base-package="org.great.action" />
<!-- 利用依賴注入,class:對應action的地址,scope="prototype": 因為預設是單例的所以這裡要改成多例狀態-->
<bean id="loginAction" class="org.great.action.LoginAction" autowire="byName" scope="prototype">
</bean>

3.在struts.xml 中設定:其中 class:struts2時的寫法是:包名+類名,加入spring後:必須是applicationContext.xml 中bean 的 id,

<action name="loginaction" method="login" class="loginAction">

4.在action類中的寫法:

//通過掃描註解來例項物件
@Controller
public class LoginAction {
//前端頁面注入的物件
private Teachers teacher;
/*介面類的宣告,如果是手動新增的那麼屬性名要與applicationContext.xml中的id名一致
* 如果是自動掃描的話:屬性名只要是類名首字母小寫就可以
*/
@Resource
private EmpetyInterface empetyInterface;

public String login(){

//呼叫介面下的方法
List<Teachers> li = empetyInterface.selAllForTest();

}

到這來就實現了 Spring + struts + MyBatis 的結合做法了。