struts2 + spring 2.5 + ibatis

很久没有做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>

 

你可能感兴趣的:(spring,sql,bean,xml,ibatis)