• spring3.0+struts2+ibatis整合


    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">
    
      <!-- Use type aliases to avoid typing the full classname every time. -->
      <typeAlias alias="User" type="******.beans.User"/>
    
      <!-- Result maps describe the mapping between the columns returned
           from a query, and the class properties.  A result map isn't
           necessary if the columns (or aliases) match to the properties 
           exactly. -->
      <resultMap id="UserResult" class="User">
        <result property="userName" column="name"/>
        <result property="password" column="password"/>
      </resultMap>
    
      <!-- Select with no parameters using the result map for Account class. -->
      <select id="selectAllUser" resultMap="UserResult">
        select * from User
      </select>
    
      <!-- A simpler select example without the result map.  Note the 
           aliases to match the properties of the target result class. -->
      <select id="selectUserByName" parameterClass="java.lang.String" resultClass="User">
        select
          name as userName,
          password as password
        from user
        where name = #userName#
      </select>
       
      <!-- Insert example, using the User parameter class-->
      <insert id="insertUser" parameterClass="User">
        insert into User (
          name,
          password ) 
        values (#userName#,#password#)
      </insert>
     
     
      <!-- Update example, using the Account parameter class 
      <update id="updateAccount" parameterClass="Account">
        update ACCOUNT set
          ACC_FIRST_NAME = #firstName#,
          ACC_LAST_NAME = #lastName#,
          ACC_EMAIL = #emailAddress#
        where
          ACC_ID = #id#
      </update>
    -->
      <!-- Delete example, using an integer as the parameter class 
      <delete id="deleteAccountById" parameterClass="int">
        delete from ACCOUNT where ACC_ID = #id#
      </delete>
    -->
    </sqlMap>


    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>
    
    
       <!-- The properties (name=value) in the file specified here can be used 
            placeholders in this config file (e.g. “${driver}”. The file is relative 
            to the classpath and is completely optional. 
       <properties resource="db.properties" /> -->
    
       <!-- These settings control SqlMapClient configuration details, primarily 
            to do with transaction management. They are all optional (more detail 
            later in this document). 
      <settings
        cacheModelsEnabled="true"
        enhancementEnabled="true"
        lazyLoadingEnabled="true"
        maxRequests="32"
        maxSessions="10"
        maxTransactions="5"
        useStatementNamespaces="false"/> -->
    
      <!-- Configure a built-in transaction manager.  If you're using an 
           app server, you probably want to use its transaction manager 
           and a managed datasource 
       
      <transactionManager type="JDBC" commitRequired="false">
        <dataSource type="SIMPLE">
          <property name="JDBC.Driver" value="${driver}"/>
          <property name="JDBC.ConnectionURL" value="${url}"/>
          <property name="JDBC.Username" value="${username}"/>
          <property name="JDBC.Password" value="${password}"/>      
          <property name="JDBC.DefaultAutoCommit" value="true" />
          <property name="Pool.MaximumActiveConnections" value="10"/>
          <property name="Pool.MaximumIdleConnections" value="5"/>
          <property name="Pool.MaximumCheckoutTime" value="120000"/>
          <property name="Pool.TimeToWait" value="500"/>
          <property name="Pool.PingQuery" value="select 1 from User"/>
          <property name="Pool.PingEnabled" value="false"/>
          <property name="Pool.PingConnectionsOlderThan" value="1"/>
          <property name="Pool.PingConnectionsNotUsedFor" value="1"/>
        </dataSource>
      </transactionManager> -->
      
      <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) -->
      <!-- List more here...
      <sqlMap resource="com/mydomain/data/Order.xml"/>
      <sqlMap resource="com/mydomain/data/Documents.xml"/>
      -->
      <sqlMap resource="*********/User.xml"/>
    
    </sqlMapConfig>

     beans.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-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">
               
             
     
    <context:property-placeholder location="classpath:db.properties"/>
    <context:annotation-config/>
    <context:component-scan base-package="com.****"/>
    <!--此bean用来告诉Spring去何处找数据库信息,有此Bean才会有下面dataSource中用${}标记来取变量的语句
    <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="location"> 
        <value>classpath:db.properties</value>  
        </property> 
    </bean> 
     -->  
    <!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息 --> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName"> 
        <value>${driver}</value> 
        </property> 
        <property name="url"> 
        <value>${url}</value> 
        </property> 
        <property name="username"> 
        <value>${username}</value> 
        </property> 
        <property name="password"> 
        <value>${password}</value> 
        </property> 
    </bean> 
    
    <!--根据dataSource和configLocation创建一个SqlMapClient --> 
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
        <property name="configLocations"> 
            <list> 
                <!-- sql语句配置xml文件 --> 
                <value>classpath:SqlMapConfig.xml</value> 
            </list> 
        </property> 
        <property name="dataSource"> 
            <ref bean="dataSource" /> 
        </property> 
    </bean> 
    
    <!--根据sqlMapClien创建一个sqlMapClientTemplate模版类 --> 
    <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> 
        <property name="sqlMapClient"> 
           <ref bean="sqlMapClient" /> 
        </property> 
    </bean> 
    </beans>

    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        <!-- 
                      以下指定了对象工厂为StrutsSpringObjectFactory,这样struts2的action就不再又
           struts2而是由spring负责产生了,另外action元素中的class属性不再指向其实际的class,
                      而是指向beans.xml中某个action bean的id    
         -->                                        
        <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
        <!-- 
        <constant name="struts.objectFactory" value="spring" /> 
         -->   
        <constant name="struts.devMode" value="true" />
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        
       <package name="default" namespace="/" extends="struts-default">
           <action name="save" class="userAction" method="userAdd">
                <result name="sucess">/sucess.jsp</result>
                <result name="failure">/failure.jsp</result>
           </action>
      </package>
        <!--  
        <package name="default" namespace="/" extends="struts-default">
            <default-action-ref name="index" />
            <global-results>
                <result name="error">/error.jsp</result>
            </global-results>
            <global-exception-mappings>
                <exception-mapping exception="java.lang.Exception" result="error"/>
            </global-exception-mappings>
            <action name="index">
                <result type="redirectAction">
                    <param name="actionName">HelloWorld</param>
                    <param name="namespace">/example</param>
                </result>
            </action>
        </package>
        <include file="example.xml"/> 
        -->
    
        <!-- Add packages here -->
    
        
     
    </struts>

    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">
    
    <!-- 配置Spring监听器 -->
    <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 用来定位Spring XML文件的上下文配置 -->
    <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:beans.xml</param-value>
    </context-param>
    
    <!-- 配置struts2 -->
    <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>*.action</url-pattern>
    </filter-mapping>
     <!-- 记住加入这个配置,不然在使用struts-tag标签会报错误 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    
    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    //     ActionContext context = ActionContext.getContext();  
    //     Map request = (Map) context.get("request");  
    //     Map session = context.getSession();  
    //     Map application = context.getApplication();  
    //     // 在请求中放置欢迎信息。  
    //     request.put("greeting", "欢迎您来到程序员之家");  
    //     // 在session中保存password属性  
    //     session.put("password", "111111");  
            
    // 通过接口注入来获取request、session和application对象的LoginAction:
            //RequestAware
            //SessionAware
            //ApplicationAware
    
            HttpServletRequest request = ServletActionContext.getRequest();  
            HttpSession session = request.getSession();  
            ServletContext context = ServletActionContext.getServletContext();  
      
            // 在请求中放置欢迎信息。  
            request.setAttribute("greeting", "欢迎您来到程序员之家");  
            // 在session中保存user对象  
            session.setAttribute("password", "111111");  

     

  • 相关阅读:
    Android开发之动态设置字体的样式和粗细
    Android开发之炫酷MD风格
    Android开发之自定义Dialog简单实现
    Android开发之自定义Toast(带详细注释)
    【Android优化篇】提升Activity加载速度的方法
    android使用Pull解析来自服务器的xml文件时出现错误以及解决方案
    Image augmentation for machine learning experiments
    LibreCAD
    C++ library to read and write DXF/DWG files
    DXF-Viewer
  • 原文地址:https://www.cnblogs.com/heml/p/3501531.html
Copyright © 2020-2023  润新知