使用MyBatis调用Oracle存储过程

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一、Oracle存储过程

存储过程代码

create or replace procedure TEST_DEMO_PROC(param_name    IN VARCHAR2, return_result OUT VARCHAR2) is
begin
  return_result := 'Hello ' || param_name;
end TEST_DEMO_PROC;
/

测试存储过程

declare result varchar2(100);
begin
test_demo_proc('Kevin', result);
DBMS_OUTPUT.put_line(result);
end;
/

####二、ojdbc6的Maven Dependency配置


	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配置

 
     
     
     
     
 

四、MyBatis的Mapper和Mapper对应的xml文件

Mapper文件:

/**
 * 存储过程测试Demo
 * 
 * @author Kevin
 * @date 2017-01-03
 */
public interface ProcedureDemoMapper {

	/**
	 * 测试方法
	 *
	 * @param param
	 */
	void testMethod(Map param);

}

Mapper对应的xml文件




    
        
        
    

    


五、Mapper单元测试代码

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
    }

}

转载于:https://my.oschina.net/zhaokaiju/blog/818157

你可能感兴趣的:(使用MyBatis调用Oracle存储过程)