mybatis spring配置SqlSessionTemplate使用

[size=large][color=red]1.application.xml配置[/color][/size]


<?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>


[color=red][size=large]2.MyBatisDAO :[/size][/color]

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;

@Repository
public 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);
}
}



[size=large][color=red]3.mybatis配置文件[/color][/size]


<?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>



[size=large][color=red]4.具体DAO配置[/color][/size]


package com.dwr;

import org.springframework.stereotype.Repository;

import com.mybatis.Test;

@Repository
public class TestDAO extends MyBatisDAO {

public int getCount(Test test) {
return (Integer) this.getObject("Test.queryDataCount", test);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值