当前位置: 代码迷 >> Java Web开发 >> spring集成ibatis问题
  详细解决方案

spring集成ibatis问题

热度:397   发布时间:2008-08-27 19:47:22.0
spring集成ibatis问题
applicationContext.xml
<!-- 配置事务特性 -->

    <tx:advice id="txAdvice" transaction-manager="txManager">

        <tx:attributes>

          <tx:method name="add*" propagation="REQUIRED"/>

          <tx:method name="del*" propagation="REQUIRED"/>

          <tx:method name="update*" propagation="REQUIRED"/>

          <tx:method name="*" read-only="true"/>

      </tx:attributes>

    </tx:advice>

   

    <!-- 配置哪些类的方法需要进行事务管理 -->

    <aop:config>

      <aop:pointcut id="allManagerMethod" expression="execution(* add*(..))"/>

      <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>

    </aop:config>


    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

    <property name="dataSource" ref="dataSource"/>

    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

        <property name="url" value="jdbc:sqlserver://localhost:1433;databasename=test"/>

        <property name="username" value="sa"/>

        <property name="password" value=""/>

    </bean>

<bean id="userDAO" class="dao.imp.BeanDaoImp">

    <property name="sqlMapClient" ref="sqlMapClient"/>
      
</bean>


<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

    <property name="configLocation"> <value>classpath:SqlMapConfig.xml </value> </property>

    <property name="dataSource" ref="dataSource"> </property>

</bean>


SqlMapConfig.xml
<sqlMapConfig>

    <settings

      lazyLoadingEnabled="true"

        useStatementNamespaces="true" />
  

  <sqlMap resource="com/tx/vo/Users.xml"/>

</sqlMapConfig>


Users.xml
<sqlMap namespace="Users">

    <typeAlias alias="users" type="com.tx.vo.Users"/>

    <cacheModel id="usersCache" type="MEMORY" readOnly="false" serialize="true">
        <flushInterval hours="24"/>
        <flushOnExecute statement="Users.update"/>
        <flushOnExecute statement="Users.insert"/>
        <flushOnExecute statement="Users.delete"/>
    </cacheModel>

    <resultMap id="usersResult" class="users">
    <result property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="age" column="age"/>
    </resultMap>

    <!-- hsql db used for this example capitalizes col names
        so Map will have them as all caps -->
    <select id="getAll" resultClass="users"> <!--  cacheModel="usersCache" 这个加了报错 -->
        SELECT
        id,
            username,        
            age      
        FROM users
    </select>

    <select id="getById" resultMap="usersResult" parameterClass="java.lang.Integer">
        SELECT id,username, age
        FROM users
        WHERE id = #value#            
    </select>

    <update id="update" parameterClass="users">
        UPDATE users
        SET
            username = #username#,         
            age = #age#         
        WHERE id = #id#
    </update>

    <insert id="insert" parameterClass="users" >
        INSERT INTO users (username, age)
        VALUES (#username#, #age#)
    </insert>

    <delete id="delete" parameterClass="java.lang.Integer">
        DELETE FROM users WHERE id = #value#
    </delete>
</sqlMap>


dao.imp.BeanDaoImp.java
public class BeanDaoImp extends SqlMapClientDaoSupport implements BeanDao {

    public void addBean(Users u) {
this.getSqlMapClientTemplate().insert("Users.insert", u);
    }
}


测试类
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BeanDao dao = (BeanDao) ctx.getBean("userDAO");
        Users u = new Users();
        u.setId(22);
        u.setUsername("tx");
        dao.addBean(u);


我现在程序执行到
this.getSqlMapClientTemplate()都没事,一调this.getSqlMapClientTemplate().insert("Users.insert", u);
就出错
java.lang.NoSuchMethodError: com.ibatis.sqlmap.client.SqlMapSession.getCurrentConnection()Ljava/sql/Connection;
搜索更多相关主题的帖子: ibatis  spring  

----------------解决方案--------------------------------------------------------
  相关解决方案