mybatis spring配置SqlSessionTemplate使用

来源:互联网 发布:显卡满载测试软件 编辑:程序博客网 时间:2024/06/05 09:09
1.application.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"xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"xmlns:lang="http://www.springframework.org/schema/lang"xmlns:top="http://www.comtop.org/schema/spring-top"xsi:schemaLocation="     http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd     http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd     http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.2.xsd     http://www.directwebremoting.org/schema/spring-dwr         http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd         http://www.springframework.org/schema/lang         http://www.springframework.org/schema/lang/spring-lang-3.2.xsd         http://www.comtop.org/schema/spring-top         http://www.comtop.org/schema/top/spring-top.xsd"><!-- spring 注解 -->      <context:component-scan base-package="com.dwr"/>        <!-- 这句的作用是表示允许DWR访问Spring的Context -->  <dwr:annotation-config  id="dwr_as"/>      <!-- 扫描加了注解@RemoteProxy & @RemoteMethod 的对象 -->      <dwr:annotation-scan scanRemoteProxy="false" base-package="com.dwr" />      <!-- dwr初始化配置 -->    <dwr:configuration></dwr:configuration>        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"         destroy-method="close">       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />         <property name="url" value="jdbc:oracle:thin:@10.10.15.29:1521:xxxx" />         <property name="username" value="edmp" />         <property name="password" value="edmp" />      </bean>    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">          <property name="dataSource" ref="dataSource" /><property name="mapperLocations" value="classpath:com/mybatis/*.xml" />     </bean>    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean><import resource="RESTful.xml" /></beans>


2.MyBatisDAO :
package com.dwr;import java.util.List;import javax.annotation.Resource;import org.apache.ibatis.session.RowBounds;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.stereotype.Repository;@Repositorypublic class MyBatisDAO{@Resource    private SqlSessionTemplate sqlSessionTemplate;        public MyBatisDAO() {      }        public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {          this.sqlSessionTemplate = sqlSessionTemplate;      }        public void delete(String statementId, Object deleteObject) {          try {              this.sqlSessionTemplate.delete(statementId, deleteObject);          } catch (Exception e) {          }      }        public void insert(String statementId, Object insertObject) {          try {              this.sqlSessionTemplate.insert(statementId, insertObject);          } catch (Exception e) {          }      }          public void update(String statementId, Object updateObject) {          try {              this.sqlSessionTemplate.update(statementId, updateObject);          } catch (Exception e) {          }      }          public Object getObject(String statementId, Object selectParamObject) {          return this.sqlSessionTemplate                  .selectOne(statementId, selectParamObject);      }          @SuppressWarnings("all")    public List queryList(String statementId, Object queryParamObject) {          return this.sqlSessionTemplate                  .selectList(statementId, queryParamObject);      }    @SuppressWarnings("all")    public List queryList(String statementId, Object queryParamObject,              int pageNo, int pageSize) {          RowBounds objRowBounds;          int iOffset = (pageNo - 1) * pageSize;          objRowBounds = new RowBounds(iOffset, pageSize);          return this.sqlSessionTemplate.selectList(statementId,                  queryParamObject, objRowBounds);      }  }  


3.mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper    PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"><mapper namespace="Test"><select id="queryDataCount"parameterType="com.mybatis.Test"resultType="int">SELECT COUNT(1) FROM INF_CONSUMER T WHERE T.UUID = #{uuid}</select></mapper>


4.具体DAO配置

package com.dwr;import org.springframework.stereotype.Repository;import com.mybatis.Test;@Repositorypublic class TestDAO extends MyBatisDAO {public int getCount(Test test) {return (Integer) this.getObject("Test.queryDataCount", test);}}


0 2