Spring2.5+struts2.0+ibatis整合

各個框架在本項目內的作用

spring2:主要利用ioc,以及對事物的管理,減少硬性編碼和脫離手動事務控制。

struts2:主要用於MVC以及數據校驗。struts2擺脫了struts1性能上的瓶頸,達到了新的高度,配置更靈活,全面支持ajax,freemark等等,採用ognl動態語言使得輸出也更加靈活。

iBatis:主要用於作orm。開發效率不如hibernate,但是由於是半自動映射,因此更加靈活,並且效率更好,維護更方便。

整合過程(使用工具MyEclipse8.5):

1.Struts2的整合(這裏採用手動整合的方式)     導入包:freemarker-2.3.15.jar,ognl-2.7.3.jar,struts2-core-2.1.8.1.jar,xwork-core-2.1.6.jar,struts2-spring-plugin-2.1.8.1.jar(置於爲什麼在此不解釋)然後再web.xml中加入 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/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>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>(到此Struts2就已經導入成功了Spring2.5+struts2.0+ibatis整合,並且跟spring整合的配置也都配好了額)

2.Spring的導入使用myeclipse自動導入

3.ibatis的導入:導入包:ibatis-2.3.4.726.jar 

4.下面的就是將3個框架整合的配置文件了,要看清楚了額,別暈哦Spring2.5+struts2.0+ibatis整合

Spring的核心配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:annotation-config></context:annotation-config> <!--想使用注入此行不可缺少-->

<bean id="dataSource"<!--配置c3p0連接池-->
       class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="minPoolSize" value="${jdbc.minPoolSize}" />
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
        <property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
        <property name="acquireIncrement"
            value="${jdbc.acquireIncrement}" />
        <property name="maxStatements" value="${jdbc.maxStatements}" />
        <property name="initialPoolSize"
            value="${jdbc.initialPoolSize}" />
        <property name="idleConnectionTestPeriod"
            value="${jdbc.idleConnectionTestPeriod}" />
        <property name="acquireRetryAttempts"
            value="${jdbc.acquireRetryAttempts}" />
    </bean>

<bean id="sqlMapClient"<!--引入ibatis的配置文件,下面會有SqlMapConfig.xml的配置-->
       class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>/WEB-INF/iBatis/SqlMapConfig.xml</value>
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
     </bean>
    

<bean id="propertyConfig"<!--將連接池中的配置信息引入,下面會有jdbc.properties的配置-->
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/WEB-INF/iBatis/jdbc.properties</value>
        </property>
    </bean>
    <!--下面是配置自己的service 和dao的聲明-->
    <bean id = "user_Service" class="com.unite.service.User_service"></bean>
    <bean id= "user_dao" class="com.unite.dao.User_dao">
        <property name="sqlMapClient">
            <ref bean="sqlMapClient"/>
        </property>
    </bean>
    </beans>

ibatis的核心配置文件:SqlMapConfig.xml:<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    <typeAlias alias="user" type="com.unite.bean.User" /><!--給User這個bean類起個別名方便下面的使用--><!--在此還可以添加更多的功能,具體配置找百度大神吧Spring2.5+struts2.0+ibatis整合-->
       <sqlMap resource="com/unite/bean/User.xml"></sqlMap><!--引入bean類的信息,不解釋-->
</sqlMapConfig>

連接池的配置文件jdbc.properties:我這裏使用的MySQL數據庫,記得吧mysql的包加到項目中哦

jdbc.driverClass=com.mysql.jdbc.Driver     //驅動
jdbc.url=jdbc:mysql://localhost:3306/unite     //鏈接字符串,unite是數據庫名
jdbc.user=root   //數據庫登錄用戶,下面是密碼,
jdbc.password=
jdbc.minPoolSize=5
jdbc.maxPoolSize=20
jdbc.maxIdleTime=1800
jdbc.acquireIncrement=5
jdbc.maxStatements=50
jdbc.initialPoolSize=10
jdbc.idleConnectionTestPeriod=1800
jdbc.acquireRetryAttempts=30

bean類的配置文件User.xml:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"    
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="user" >
    <typeAlias alias="users" type="com.unite.bean.User" /><!--還是別名-->
    <select id="checkLogin" parameterClass="users" resultClass="users">
        select * from User where user_name = #user_name# and user_password = #user_password#
    </select><!--這裏只有一個查詢的功能,sql語句中的表明是數據庫的表的名字,列名也同樣parameterClass:參數類型,resultClass:返回值類型-->
</sqlMap>

Struts2的配置文件struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default"  extends="struts-default">
    <!--這是的配置不解釋-->
    <action name="User_LogAction" class="com.unite.action.User_LogAction">
            <result name = "ok">/index.jsp</result>
            <result name = "no">/index.jsp</result>
        </action>
</package>
</struts>

5.到這裏就應經整合完成了,項目結構:Spring2.5+struts2.0+ibatis整合,jsp中的代碼這裏就拿過來了,跟普通的用法一樣<--DAO中的代碼-->

package com.unite.dao;

import java.sql.SQLException;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.unite.bean.User;
public class User_dao extends SqlMapClientDaoSupport{
    @SuppressWarnings("unchecked")
    public User checkLogin(User user){
        System.out.println(user.getUser_name()+":"+user.getUser_password());
        try {
            List<User>  list = this.getSqlMapClient().queryForList("checkLogin", user);
            if(list.size() > 0){
                return list.get(0);
            }
        } catch (SQLException e) { e.printStackTrace();}
        return null;
    }}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章