`
lzkyo
  • 浏览: 468224 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2 + spring 2.5 + ibatis

    博客分类:
  • SSH
阅读更多
很久没有做struts2 + spring 2.5 + ibatis这种组合,现在学习了一下,我把我的写的小例子共享给大家一起分享一下  
svn 下载地扯是:http://programming-ssi.unfuddle.com/svn/programming-ssi_ssi    
  用户名是 ssi  密码是: ssi 
我的开发环境是:jdk1.5  tomcat5.5 eclispe jee集成 版本 mysql 数据库 里面有建表语句 

    共同学习   共同进步 

还是把关键代码贴出来一下,要跑起来的就用svn下,只是看看,那就看以下的代码吧 

web.xml 的主要内容 
Java代码 
<?xml version="1.0" encoding="UTF-8"?>   
<web-app id="WebApp_ID" version="2.4"  
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
  
    <display-name>firstssh</display-name>   
    <description>this is a simple example</description>   
    <context-param>   
        <param-name>webAppRootKey</param-name>   
        <param-value>ssi.root</param-value>   
    </context-param>   
  
    <context-param>   
        <param-name>contextConfigLocation</param-name>   
        <param-value>classpath:applicationContext*.xml</param-value>   
    </context-param>   
    <context-param>   
        <param-name>log4jConfigLocation</param-name>   
        <param-value>classpath:log4j.properties</param-value>   
    </context-param>   
    <listener>   
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>   
    </listener>   
    <!-- 用于初始化Spring容器的Listener -->   
    <listener>   
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
    </listener>   
    <!-- 定义Struts2的FilterDispathcer的Filter -->   
    <filter>   
        <filter-name>struts2</filter-name>   
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
    </filter>   
  
    <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->   
    <filter-mapping>   
        <filter-name>struts2</filter-name>   
        <url-pattern>/*</url-pattern>   
    </filter-mapping>   
  
    <filter>   
        <filter-name>encodingFilter</filter-name>   
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
        <init-param>   
            <param-name>encoding</param-name>   
            <param-value>utf-8</param-value>   
        </init-param>   
    </filter>   
    <filter-mapping>   
        <filter-name>encodingFilter</filter-name>   
        <url-pattern>*.action</url-pattern>   
    </filter-mapping>   
    <filter-mapping>   
        <filter-name>encodingFilter</filter-name>   
        <url-pattern>*.jsp</url-pattern>   
    </filter-mapping>   
    <session-config>   
        <session-timeout>10</session-timeout>   
    </session-config>   
    <welcome-file-list>   
        <welcome-file>index.html</welcome-file>   
        <welcome-file>index.htm</welcome-file>   
        <welcome-file>index.jsp</welcome-file>   
        <welcome-file>default.html</welcome-file>   
        <welcome-file>default.htm</welcome-file>   
        <welcome-file>default.jsp</welcome-file>   
    </welcome-file-list>   
</web-app>  

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        <display-name>firstssh</display-name>
        <description>this is a simple example</description>
        <context-param>
                <param-name>webAppRootKey</param-name>
                <param-value>ssi.root</param-value>
        </context-param>

        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext*.xml</param-value>
        </context-param>
        <context-param>
                <param-name>log4jConfigLocation</param-name>
                <param-value>classpath:log4j.properties</param-value>
        </context-param>
        <listener>
                <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
        <!-- 用于初始化Spring容器的Listener -->
        <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- 定义Struts2的FilterDispathcer的Filter -->
        <filter>
                <filter-name>struts2</filter-name>
                <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        </filter>

        <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
        <filter-mapping>
                <filter-name>struts2</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>

        <filter>
                <filter-name>encodingFilter</filter-name>
                <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
                <init-param>
                        <param-name>encoding</param-name>
                        <param-value>utf-8</param-value>
                </init-param>
        </filter>
        <filter-mapping>
                <filter-name>encodingFilter</filter-name>
                <url-pattern>*.action</url-pattern>
        </filter-mapping>
        <filter-mapping>
                <filter-name>encodingFilter</filter-name>
                <url-pattern>*.jsp</url-pattern>
        </filter-mapping>
        <session-config>
                <session-timeout>10</session-timeout>
        </session-config>
        <welcome-file-list>
                <welcome-file>index.html</welcome-file>
                <welcome-file>index.htm</welcome-file>
                <welcome-file>index.jsp</welcome-file>
                <welcome-file>default.html</welcome-file>
                <welcome-file>default.htm</welcome-file>
                <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
</web-app>

applicationContext.xml 的主要内容 

Java代码 
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">   
<beans>   
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
        <property name="locations">   
            <list>   
                <!--  <value>WEB-INF/mail.properties</value>-->   
                <value>classpath:jdbc.properties</value>   
                <!--  <value>WEB-INF/oscache.properties</value>-->   
            </list>   
        </property>   
    </bean>   
    <!-- MailSender used by EmailAdvice -->   
    <!--   
        <bean id="mailSender"  
        class="org.springframework.mail.javamail.JavaMailSenderImpl">   
        <property name="host" value="${mail.host}"/> </bean>   
    -->   
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close" dependency-check="none">   
        <property name="driverClass">   
            <value>${datasource.driverClassName}</value>   
        </property>   
        <property name="jdbcUrl">   
            <value>${datasource.url}</value>   
        </property>   
        <property name="user">   
            <value>${datasource.username}</value>   
        </property>   
        <property name="password">   
            <value>${datasource.password}</value>   
        </property>   
        <property name="acquireIncrement">   
            <value>${c3p0.acquireIncrement}</value>   
        </property>   
        <property name="initialPoolSize">   
            <value>${c3p0.initialPoolSize}</value>   
        </property>   
        <property name="minPoolSize">   
            <value>${c3p0.minPoolSize}</value>   
        </property>   
        <property name="maxPoolSize">   
            <value>${c3p0.maxPoolSize}</value>   
        </property>   
        <property name="maxIdleTime">   
            <value>${c3p0.maxIdleTime}</value>   
        </property>   
        <property name="idleConnectionTestPeriod">   
            <value>${c3p0.idleConnectionTestPeriod}</value>   
        </property>   
        <property name="maxStatements">   
            <value>${c3p0.maxStatements}</value>   
        </property>   
        <property name="numHelperThreads">   
            <value>${c3p0.numHelperThreads}</value>   
        </property>   
    </bean>   
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   
        <property name="configLocation"  
            value="classpath:com/ssi/model/sql/ibatis/sql-map.xml" />   
        <property name="dataSource" ref="dataSource" />   
    </bean>   
  
    <!-- 数据连接事务 -->   
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
        <property name="dataSource" ref="dataSource" />   
    </bean>   
  
    <!-- 拦截事务的方法设置 -->   
    <bean id="transactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor">   
        <property name="transactionManager" ref="transactionManager" />   
        <property name="transactionAttributes">   
            <props>   
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>   
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>   
                <prop key="insert*">PROPAGATION_REQUIRED</prop>   
                <prop key="update*">PROPAGATION_REQUIRED</prop>   
                <prop key="delete*">PROPAGATION_REQUIRED</prop>   
                <prop key="add*">PROPAGATION_REQUIRED</prop>   
                <prop key="save*">PROPAGATION_REQUIRED</prop>   
                <prop key="remove*">PROPAGATION_REQUIRED</prop>   
            </props>   
        </property>   
    </bean>   
  
    <!-- 拦截事务Beans设置-->   
    <!--   
        <bean   
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
        <property name="beanNames"> <value>*DAO</value> </property> <property   
        name="interceptorNames"> <list> <value>transactionInterceptor</value>   
        此处增加新的Interceptor </list> </property> </bean>   
    -->   
  
    <!-- 通知器,用它来对属性值进行处理    -->   
    <bean   
        class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">   
        <property name="transactionInterceptor" ref="transactionInterceptor" />   
    </bean>   
</beans>  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
        <bean id="propertyConfigurer"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                        <list>
                                <!--  <value>WEB-INF/mail.properties</value>-->
                                <value>classpath:jdbc.properties</value>
                                <!--  <value>WEB-INF/oscache.properties</value>-->
                        </list>
                </property>
        </bean>
        <!-- MailSender used by EmailAdvice -->
        <!--
                <bean id="mailSender"
                class="org.springframework.mail.javamail.JavaMailSenderImpl">
                <property name="host" value="${mail.host}"/> </bean>
        -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close" dependency-check="none">
                <property name="driverClass">
                        <value>${datasource.driverClassName}</value>
                </property>
                <property name="jdbcUrl">
                        <value>${datasource.url}</value>
                </property>
                <property name="user">
                        <value>${datasource.username}</value>
                </property>
                <property name="password">
                        <value>${datasource.password}</value>
                </property>
                <property name="acquireIncrement">
                        <value>${c3p0.acquireIncrement}</value>
                </property>
                <property name="initialPoolSize">
                        <value>${c3p0.initialPoolSize}</value>
                </property>
                <property name="minPoolSize">
                        <value>${c3p0.minPoolSize}</value>
                </property>
                <property name="maxPoolSize">
                        <value>${c3p0.maxPoolSize}</value>
                </property>
                <property name="maxIdleTime">
                        <value>${c3p0.maxIdleTime}</value>
                </property>
                <property name="idleConnectionTestPeriod">
                        <value>${c3p0.idleConnectionTestPeriod}</value>
                </property>
                <property name="maxStatements">
                        <value>${c3p0.maxStatements}</value>
                </property>
                <property name="numHelperThreads">
                        <value>${c3p0.numHelperThreads}</value>
                </property>
        </bean>
        <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                <property name="configLocation"
                        value="classpath:com/ssi/model/sql/ibatis/sql-map.xml" />
                <property name="dataSource" ref="dataSource" />
        </bean>

        <!-- 数据连接事务 -->
        <bean id="transactionManager"
                class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource" />
        </bean>

        <!-- 拦截事务的方法设置 -->
        <bean id="transactionInterceptor"
                class="org.springframework.transaction.interceptor.TransactionInterceptor">
                <property name="transactionManager" ref="transactionManager" />
                <property name="transactionAttributes">
                        <props>
                                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                                <prop key="insert*">PROPAGATION_REQUIRED</prop>
                                <prop key="update*">PROPAGATION_REQUIRED</prop>
                                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                                <prop key="add*">PROPAGATION_REQUIRED</prop>
                                <prop key="save*">PROPAGATION_REQUIRED</prop>
                                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                        </props>
                </property>
        </bean>

        <!-- 拦截事务Beans设置-->
        <!--
                <bean
                class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
                <property name="beanNames"> <value>*DAO</value> </property> <property
                name="interceptorNames"> <list> <value>transactionInterceptor</value>
                此处增加新的Interceptor </list> </property> </bean>
        -->

        <!-- 通知器,用它来对属性值进行处理    -->
        <bean
                class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
                <property name="transactionInterceptor" ref="transactionInterceptor" />
        </bean>
</beans>

以下是applicationContext-dao.xml applicationContext-action.xml applicationContext-service.xml  的配置 
Java代码 
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">   
<beans>   
    <bean id="UserAction" class="com.ssi.action.UserAction"  
        scope="prototype">   
        <property name="userService" ref="userService" />   
    </bean>   
</beans>   
  
  
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">   
<beans>   
    <!-- 根DAO -->   
    <bean id="userDao" class="com.ssi.dao.impl.UserDao">   
        <property name="sqlMapClient" ref="sqlMapClient"></property>   
    </bean>   
</beans>   
  
  
<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">   
<beans>   
    <bean id="userService" class="com.ssi.service.impl.UserService">   
        <property name="userDao" ref="userDao" />   
    </bean>   
</beans>  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="UserAction" class="com.ssi.action.UserAction"
        scope="prototype">
        <property name="userService" ref="userService" />
    </bean>
</beans>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
        <!-- 根DAO -->
        <bean id="userDao" class="com.ssi.dao.impl.UserDao">
                <property name="sqlMapClient" ref="sqlMapClient"></property>
        </bean>
</beans>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
        <bean id="userService" class="com.ssi.service.impl.UserService">
                <property name="userDao" ref="userDao" />
        </bean>
</beans>

UserDao 代码  : 
Java代码 
package com.ssi.dao.impl;   
  
import java.util.List;   
  
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;   
  
import com.ssi.dao.IUserDao;   
import com.ssi.model.User;   
  
/**  
* 功能:  
*   
* @author programming  
* @version 1.0 2009-3-11下午04:48:22  
* @JDK 5  
*/  
@SuppressWarnings("unchecked")   
public class UserDao extends SqlMapClientDaoSupport implements IUserDao {   
  
  
    @Override  
    public List<User> findAllUser() {   
        return this.getSqlMapClientTemplate().queryForList("User.select");   
    }   
  
    @Override  
    public User findUserByNameAndPass(User user) {   
        List<User> list = this.getSqlMapClientTemplate().queryForList("User.query", user.transfer2Map());   
        if (list.size() == 1) {   
            return list.get(0);   
        }   
        return null;   
    }   
  
    @Override  
    public User getById(int id) {   
        return (User) this.getSqlMapClientTemplate().queryForObject("User.findByPrimarykey", id);   
    }   
  
    @Override  
    public boolean saveUser(User user) {   
        this.getSqlMapClientTemplate().insert("User.insert", user);   
        return true;   
    }   
  
    @Override  
    public void updateUser(User user) {   
        this.getSqlMapClientTemplate().update("User.update", user);   
    }   
  
}  

package com.ssi.dao.impl;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ssi.dao.IUserDao;
import com.ssi.model.User;

/**
* 功能:
* 
* @author programming
* @version 1.0 2009-3-11下午04:48:22
* @JDK 5
*/
@SuppressWarnings("unchecked")
public class UserDao extends SqlMapClientDaoSupport implements IUserDao {


        @Override
        public List<User> findAllUser() {
                return this.getSqlMapClientTemplate().queryForList("User.select");
        }

        @Override
        public User findUserByNameAndPass(User user) {
                List<User> list = this.getSqlMapClientTemplate().queryForList("User.query", user.transfer2Map());
                if (list.size() == 1) {
                        return list.get(0);
                }
                return null;
        }

        @Override
        public User getById(int id) {
                return (User) this.getSqlMapClientTemplate().queryForObject("User.findByPrimarykey", id);
        }

        @Override
        public boolean saveUser(User user) {
                this.getSqlMapClientTemplate().insert("User.insert", user);
                return true;
        }

        @Override
        public void updateUser(User user) {
                this.getSqlMapClientTemplate().update("User.update", user);
        }

}


UserAction 代码: 

Java代码 
package com.ssi.action;   
  
import java.util.List;   
  
import org.apache.struts2.ServletActionContext;   
  
import com.opensymphony.xwork2.ActionSupport;   
import com.ssi.model.User;   
import com.ssi.service.IUserService;   
import com.ssi.service.impl.UserService;   
  
/**  
* 功能:  
*   
* @author programming  
* @version 1.0 2009-3-10下午05:02:14  
* @JDK 5  
*/  
public class UserAction extends ActionSupport {   
    private User user;   
  
    private IUserService userService;   
  
    /**  
     * @return the user  
     */  
    public User getUser() {   
        return user;   
    }   
  
    /**  
     * @param user  
     *            the user to set  
     */  
    public void setUser(User user) {   
        this.user = user;   
    }   
  
    /**  
     * @param userService  
     *            the userService to set  
     */  
    public void setUserService(UserService userService) {   
        this.userService = userService;   
    }   
  
    public String login() throws Exception {   
        if (userService.login(user)) {   
            ServletActionContext.getContext().getSession().put("user", user);   
            return SUCCESS;   
        }   
        return LOGIN;   
    }   
  
    public String regist() throws Exception {   
        userService.register(user);   
        ServletActionContext.getContext().getSession().put("user", user);   
        return SUCCESS;   
  
    }   
  
    public String list() throws Exception {   
        List<User> userList = this.userService.query();   
        ServletActionContext.getContext().getSession().put("userList", userList);   
        return SUCCESS;   
    }   
  
}  

package com.ssi.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ssi.model.User;
import com.ssi.service.IUserService;
import com.ssi.service.impl.UserService;

/**
* 功能:
* 
* @author programming
* @version 1.0 2009-3-10下午05:02:14
* @JDK 5
*/
public class UserAction extends ActionSupport {
        private User user;

        private IUserService userService;

        /**
         * @return the user
         */
        public User getUser() {
                return user;
        }

        /**
         * @param user
         *            the user to set
         */
        public void setUser(User user) {
                this.user = user;
        }

        /**
         * @param userService
         *            the userService to set
         */
        public void setUserService(UserService userService) {
                this.userService = userService;
        }

        public String login() throws Exception {
                if (userService.login(user)) {
                        ServletActionContext.getContext().getSession().put("user", user);
                        return SUCCESS;
                }
                return LOGIN;
        }

        public String regist() throws Exception {
                userService.register(user);
                ServletActionContext.getContext().getSession().put("user", user);
                return SUCCESS;

        }

        public String list() throws Exception {
                List<User> userList = this.userService.query();
                ServletActionContext.getContext().getSession().put("userList", userList);
                return SUCCESS;
        }

}


UserService 代码 

Java代码 
package com.ssi.service.impl;   
  
import java.util.List;   
  
import com.ssi.dao.IUserDao;   
import com.ssi.model.User;   
import com.ssi.service.IUserService;   
  
/**  
* 功能:  
*   
* @author programming  
* @version 1.0 2009-3-11下午04:48:06  
* @JDK 5  
*/  
public class UserService implements IUserService {   
    private IUserDao userDao;   
  
    /**  
     * @param userDao  
     *            the userDao to set  
     */  
    public void setUserDao(IUserDao userDao) {   
        this.userDao = userDao;   
    }   
  
    @Override  
    public boolean login(User user) {   
        User u = this.userDao.findUserByNameAndPass(user);   
        if (u != null)   
            return true;   
        else  
            return false;   
    }   
  
    @Override  
    public void register(User user) {   
        userDao.saveUser(user);   
    }   
  
    @Override  
    public List<User> query() {   
        return this.userDao.findAllUser();   
    }   
}  

package com.ssi.service.impl;

import java.util.List;

import com.ssi.dao.IUserDao;
import com.ssi.model.User;
import com.ssi.service.IUserService;

/**
* 功能:
* 
* @author programming
* @version 1.0 2009-3-11下午04:48:06
* @JDK 5
*/
public class UserService implements IUserService {
        private IUserDao userDao;

        /**
         * @param userDao
         *            the userDao to set
         */
        public void setUserDao(IUserDao userDao) {
                this.userDao = userDao;
        }

        @Override
        public boolean login(User user) {
                User u = this.userDao.findUserByNameAndPass(user);
                if (u != null)
                        return true;
                else
                        return false;
        }

        @Override
        public void register(User user) {
                userDao.saveUser(user);
        }

        @Override
        public List<User> query() {
                return this.userDao.findAllUser();
        }
}

ssi-user-sqlmap.xml 内容: 
Java代码 
<?xml version="1.0" encoding="GB2312" ?>   
<!DOCTYPE sqlMap   
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"  
"http://www.ibatis.com/dtd/sql-map-2.dtd">   
<sqlMap namespace="User">   
    <typeAlias alias="user" type="com.ssi.model.User" />   
    <resultMap class="com.ssi.model.User" id="userResult">   
        <result property="id" column="userid" />   
        <result property="username" column="username" />   
        <result property="password" column="password" />   
        <result property="email" column="email" />   
    </resultMap>   
  
    <sql id="queryCondition">   
        <dynamic prepend="">   
            <isNotNull property="username" prepend="and">   
                <![CDATA[   
                username = #username#   
                ]]>   
            </isNotNull>   
            <isNotNull property="password" prepend="and">   
                <![CDATA[   
                password = #password#   
                ]]>   
            </isNotNull>   
        </dynamic>   
    </sql>   
    <insert id="User.insert" >   
        <![CDATA[   
         insert into user(username,password,email)   
         value(#username#,#password#,#email#)   
       ]]>   
    </insert>   
    <update id="User.update">   
        <![CDATA[   
          update user    
          set   
          username=#username#,password=#password#,email=#email#    
          where userid=#id#   
       ]]>   
    </update>   
  
    <delete id="User.deleteByPrimaryKey">   
        <![CDATA[   
        delete from user where userid = #value#    
       ]]>   
    </delete>   
       
    <select id="User.findByPrimarykey" resultClass="user"  
        parameterClass="long">   
        <![CDATA[   
        select * from user where userid=#value#   
    ]]>   
    </select>   
  
    <select id="User.query" resultMap="userResult">   
        <![CDATA[   
          select * from user where 1=1    
           ]]>   
        <include refid="queryCondition" />   
    </select>   
  
    <select id="User.select" resultMap="userResult">   
        <![CDATA[   
        select * from user   
       ]]>   
    </select>   
  
</sqlMap>  

<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="User">
        <typeAlias alias="user" type="com.ssi.model.User" />
        <resultMap class="com.ssi.model.User" id="userResult">
                <result property="id" column="userid" />
                <result property="username" column="username" />
                <result property="password" column="password" />
                <result property="email" column="email" />
        </resultMap>

        <sql id="queryCondition">
                <dynamic prepend="">
                        <isNotNull property="username" prepend="and">
                                <![CDATA[
                        username = #username#
                        ]]>
                        </isNotNull>
                        <isNotNull property="password" prepend="and">
                                <![CDATA[
                        password = #password#
                        ]]>
                        </isNotNull>
                </dynamic>
        </sql>
        <insert id="User.insert" >
                <![CDATA[
         insert into user(username,password,email)
         value(#username#,#password#,#email#)
       ]]>
        </insert>
        <update id="User.update">
                <![CDATA[
          update user 
          set
          username=#username#,password=#password#,email=#email# 
          where userid=#id#
       ]]>
        </update>

        <delete id="User.deleteByPrimaryKey">
                <![CDATA[
        delete from user where userid = #value# 
       ]]>
        </delete>
        
        <select id="User.findByPrimarykey" resultClass="user"
                parameterClass="long">
                <![CDATA[
        select * from user where userid=#value#
    ]]>
        </select>

        <select id="User.query" resultMap="userResult">
                <![CDATA[
              select * from user where 1=1 
               ]]>
                <include refid="queryCondition" />
        </select>

        <select id="User.select" resultMap="userResult">
                <![CDATA[
        select * from user
       ]]>
        </select>

</sqlMap>


sql-map.xml 同容 

Java代码 
<?xml version="1.0" encoding="GBK"?>   
<!DOCTYPE sqlMapConfig   
  PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"  
  "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">   
<sqlMapConfig>   
    <settings cacheModelsEnabled="false" enhancementEnabled="false"  
        lazyLoadingEnabled="false" maxRequests="3000" maxSessions="3000"  
        maxTransactions="3000" useStatementNamespaces="false" />   
    <sqlMap resource="com/ssi/model/sql/ssi-user-sqlmap.xml" />   
</sqlMapConfig>  

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE sqlMapConfig
  PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
  "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
        <settings cacheModelsEnabled="false" enhancementEnabled="false"
                lazyLoadingEnabled="false" maxRequests="3000" maxSessions="3000"
                maxTransactions="3000" useStatementNamespaces="false" />
        <sqlMap resource="com/ssi/model/sql/ssi-user-sqlmap.xml" />
</sqlMapConfig>

 

分享到:
评论

相关推荐

    Struts2+Spring2.5+Ibatis完整增删改查Demo(含全部jar包)

    这个"Struts2+Spring2.5+iBatis完整增删改查Demo"提供了一个完整的集成示例,包括所有必要的jar包,使得开发者可以快速在Mycelipse环境中搭建并运行项目。 **Struts2框架**: Struts2是基于MVC设计模式的Web应用...

    Struts2+Spring2.5+Ibatis2.3架构

    Struts2+Spring2.5+Ibatis2.3架构是一种经典的Java Web开发技术栈,广泛应用于企业级应用系统中。这个架构结合了Struts2的MVC框架、Spring的依赖注入(DI)和面向切面编程(AOP)以及Ibatis的持久层解决方案,为...

    Ext2.0+struts2+spring2.5+ibatis2

    在IT领域,构建高效、可扩展的企业级应用是至关重要的,而"Ext2.0+Struts2+Spring2.5+Ibatis2"的组合就是一种常见的技术栈,用于实现这样的目标。这个技术组合提供了从用户界面到数据访问的全方位解决方案。 **Ext...

    SSI2 Struts2+Spring2.5+IBatis2 配置

    Struts2、Spring2.5 和 iBatis2 是经典的Java Web开发框架组合,它们各自在应用程序的不同层面提供了强大的功能。下面将详细讲解这三大框架的集成配置以及log4j的相关知识。 首先,Struts2 是一个基于MVC(Model-...

    struts2+spring+Ibatis框架包

    Struts2、Spring和iBatis是Java Web开发中三个非常重要的开源框架,它们共同构建了一个灵活、可扩展且高效的应用程序开发环境。这个“struts2+spring+iBatis框架包”集成了这三个框架,使得开发者能够快速构建基于...

    struts2 + spring2.5 + ibatis2.3.4整合包文件

    这个"struts2 + spring2.5 + ibatis2.3.4整合包文件"包含了这三个框架的集成,用于搭建一个完整的Java Web应用程序。以下是对这些框架及其整合的详细说明: 1. Struts2:Struts2是基于Apache Struts 1的升级版,它...

    SSI--struts2+spring2.5+ibatis项目实例

    **SSI(Struts2 + Spring2.5 + iBatis)项目实例详解** SSI,即Struts2、Spring和iBatis的组合,是Java Web开发中常见的技术栈,用于构建高效、灵活的企业级应用程序。这个项目实例展示了如何将这三个框架集成到...

    SSI(struts2+spring2.5+ibatis2.3)项目实例

    **SSI(Struts2+Spring2.5+Ibatis2.3)项目实例详解** **一、Struts2框架** Struts2是一个基于MVC设计模式的Java Web开发框架,它继承了Struts1和WebWork的优点,提供了一种更灵活、更强大的控制层解决方案。在SSI...

    struts2+spring2.5+ibatis

    Struts2、Spring2.5和iBatis是经典的Java Web开发框架组合,它们共同构建了一个灵活、高效的企业级应用程序架构。在这个组合中,Struts2作为MVC(Model-View-Controller)框架负责处理用户请求,Spring2.5则提供依赖...

    整合struts2+spring2.5+ibatis

    Struts2、Spring2.5和iBatis是Java Web开发中的三大框架,它们的整合使用可以帮助开发者构建高效、灵活的MVC(Model-View-Controller)架构的应用程序。Struts2作为表现层框架,提供了丰富的动作调度和结果处理;...

    Ibatis+struts2.0+Spring2.5 示例

    本示例是"Ibatis+struts2.0+Spring2.5"的整合,这是一个经典的Java Web开发技术栈,用于构建高效、灵活的企业级应用。下面将详细阐述这三个框架的集成及其核心功能。 1. **Ibatis**:Ibatis 是一个轻量级的持久层...

    struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _02部分

    分为:struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _01部分 struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _02部分 只要将这两个包全下载下来,就可以搭建struts2.1.8 + spring 2.5 + ibatis2整合开发的...

    struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _01部分

    分为:struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _01部分 struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _02部分 只要将这两个包全下载下来,就可以搭建struts2.1.8 + spring 2.5 + ibatis2整合开发的...

    struts1.3+spring2.5+ibatis2.3.4整合

    struts1.3+spring2.5+ibatis2.3.4整合

    Ibatis2.3+Struts1.3+Spring2.5 整合实例代码

    本示例代码涉及的是一个经典的技术栈:Ibatis2.3、Struts1.3和Spring2.5的整合。这三个框架各自都有其独特的功能和优点,组合在一起能够提供灵活的数据访问、高效的MVC架构以及全面的应用管理。 Ibatis,作为一款轻...

    struts2+spring2.5+ibatis2.3框架整合开发.doc

    Struts2、Spring和iBatis是三个非常流行的开源Java框架,它们分别负责MVC模式中的表现层、业务层和数据访问层。将这三个框架整合在一起可以构建出强大的企业级应用,实现各层的解耦合,提高代码的可维护性和可扩展性...

    struts2+ibatis-2.3+spring2.5整合实例

    在"Struts2+ibatis-2.3+spring2.5整合实例"中,我们首先需要设置项目的环境,包括JDK、Maven或Gradle等构建工具,以及Tomcat服务器。然后,创建一个新的Maven或Gradle项目,并在`pom.xml`或`build.gradle`文件中添加...

    struts2_spring2.5_ibatis2.3_mysql架构

    *架构struts2_spring2.5_ibatis2.3 *mysql5.0 *jdk 1.6 *带有所有jar包,可直接运行 本实例实现了用户登陆,用户信息CRUD相关操作。让你感受到了ibatis做o/r mapping的方便快捷。 下次集成dwr进来 create ...

    Struts1.1+spring2.5+ibatis2.3+Ajax整合的源代码

    Struts1.1+Spring2.5+Ibatis2.3+Ajax整合是一个经典的Java Web开发框架组合,常用于构建企业级应用。这个源代码集合提供了如何将这四个技术有效地结合在一起的实例,以实现一个功能强大的、具有无刷新特性的用户界面...

    Structs2.0+Spring2.5+Ibatis整合例子

    本项目"Structs2.0+Spring2.5+Ibatis整合例子"就是这样一个示例,展示了如何将Struts2、Spring和iBatis这三个流行的技术栈整合在一起,以实现MVC(模型-视图-控制器)架构。以下将详细阐述这三个框架的核心功能以及...

Global site tag (gtag.js) - Google Analytics