使用MyBatis调用Oracle存储过程

2017-01-05 11:09:52来源:oschina作者:月光-云海人点击



#### 一、Oracle存储过程
**存储过程代码**:
``` SQL
create or replace procedure TEST_DEMO_PROC(param_nameIN VARCHAR2, return_result OUT VARCHAR2) is
begin
return_result := 'Hello ' || param_name;
end TEST_DEMO_PROC;
/
```
**测试存储过程**:
``` SQL
declare result varchar2(100);
begin
test_demo_proc('Kevin', result);
DBMS_OUTPUT.put_line(result);
end;
/
```
####二、ojdbc6的Maven Dependency配置
``` xml

com.oracle
ojdbc6
12.1.0.1-atlassian-hosted

```
#### 三、Oracle连接配置
**Oracle数据库连接属性**(需要根据自己的实际情况进行修改):
```
db.driverClass=oracle.jdbc.OracleDriver
db.url=jdbc:oracle:thin:@localhost:1521/xe
db.username=system
db.password=password
```
**数据源dataSource配置**:
``` xml






```
#### 四、MyBatis的Mapper和Mapper对应的xml文件
**Mapper文件**:
``` java
/**
* 存储过程测试Demo
*
* @author Kevin
* @date 2017-01-03
*/
public interface ProcedureDemoMapper {
/**
* 测试方法
*
* @param param
*/
void testMethod(Map param);
}
```
**Mapper对应的xml文件**:
``` xml
<?xml version="1.0" encoding="UTF-8" ?>








```
#### 五、Mapper单元测试代码
``` java
package mybatis;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zkj.dal.mybatis.model.mapper.ProcedureDemoMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/mybatis.xml"})
public class ProcedureDemoMapperTest extends AbstractJUnit4SpringContextTests {
private ProcedureDemoMapper mapper;
@Before
public void setUp() {
mapper = applicationContext.getBean(ProcedureDemoMapper.class);
}
@Test
public void testMethod() {
Map paramMap = new HashMap();
paramMap.put("param_name", "Kevin");
mapper.testMethod(paramMap);
System.out.println(paramMap.get("return_result"));
// 输出结果为:Hello Kevin
}
}
```

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台