ibatis+struts+spring配置和登录实现

最近换了家公司,用的ibatis,也就是SSI.,感觉跟hibernate比起来麻烦了不止一点两点,所有sql语句都要自己手敲,.但是也有一些优点,比如ibatis查询数据可以指定一些返回的结果,响应速度更快.ibatis和hibernate各有千秋吧,小型项目开发最好还是ssh.开发速度很快,下面上代码,我这里配置的是多数据源,datasource.properties里面放的就是数据库地址账户密码之类的

spring.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
            http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd " default-autowire="byName">


    <context:property-placeholder location="classpath:datasource.properties" />
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="testOnBorrow" value="false"></property>
         <property name="testOnReturn" value="false"></property>
        <property name="testWhileIdle" value="true"></property>
        <property name="validationQuery" value="SELECT 1"></property>
        <property name="timeBetweenEvictionRunsMillis" value="300"></property>
           <property name="minEvictableIdleTimeMillis" value="18000000"></property>
    
    </bean>
    <bean id="sqlClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="configLocation">
            <value>classpath:config.xml</value>
        </property>
    </bean>
     
    <!--事务模板 -->  
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">  
           <property name="transactionManager">  
                    <ref local="transactionManager"/>  
           </property>  
   </bean>   
    <!--配置事务管理器-->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--需要回滚的方法-->
    <tx:advice id="serviceAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--当代理的service层中的方法抛出异常的时候才回滚,必须加rollback-for参数-->
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable" />
            <tx:method name="insert**" propagation="REQUIRED" rollback-for="Throwable" />
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Throwable" />
            <!--除了上面标识的方法,其他方法全是只读方法-->
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置哪些类的方法需要进行事务管理 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="servicePointcut"
            expression="execution(* com.inventory.service.*ServiceImpl.*(..))" />            
        <aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice" />
    </aop:config>
    <import resource="spring/cfg/applicationContext_acount.xml"/>

<beans>

config.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>
    <settings cacheModelsEnabled="true" enhancementEnabled="true"
        lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
        maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />   
<sqlMap resource="com/inventory/entity/dto/Acount_SqlMap.xml"/>实体类xml地址

</sqlMapConfig>


<?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="userAction">
    <resultMap class="com.cyw.website.inventory.entity.CrmUserAcount"
        id="userResult">

    <result column="ua_login" property="uaLogin" jdbcType="VARCHAR" />
    <result column="ua_pwd" property="uaPwd" jdbcType="VARCHAR" />

    </resultMap>

<select id="login" resultMap="userResult">
    select * from crm_user_acount c where c.ua_login=#uaLogin# and c.ua_pwd=#uaPwd#
    </select>

</sqlMap>

DaoImpl

    public void addLogin(CrmUserAcount c) {
        this.getSqlMapClientTemplate().insert("userAction.addLogin", c);
        
    }

其他的我就省掉了啊,如果有ssh基础的话,那么ssi对你来说其实没有太大区别



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值