mybatis調用存儲過程
輸入一個園的半徑傳回園的周長和面積
首先建立存儲過程
create or replace procedure pro_text(s out varchar2,r in out varchar2)
is
v_pi number(10):=3.14;
begin
s:=v_pi*r*r;
r:=2*v_pi*r;
end;
建立測試接口
package com.aaa.mybatis.dao;
import java.util.Map;
public interface TextDao {
public void returnSorC(Map map);
}
建立mapper檔案并調用存儲過程
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.mybatis.dao.TextDao">
<select id="returnSorC" statementType="CALLABLE">
<!--調用存儲過程有專門的标簽statementType="CALLABLE" 用{}括起來-->
{
call pro_text(
<!--#{第一個是參數名,第二個mode代表出參還是入參或者出入參必須大寫,
jdbcType參數類型jdvcType裡沒NUMBER,有VARCHAR ctrl+n輸入jdbcType可以看jdbcType都有哪些類型}-->
#{s,mode=OUT,jdbcType=INTEGER},
#{r,mode=INOUT,jdbcType=INTEGER}
)
}
</select>
</mapper>
建立工具類
package com.aaa.mybatis.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
public class SqlSessionFactoryUtil {
private static SqlSessionFactory sessionFactory;
static {
Reader reader =null;
try {
reader= Resources.getResourceAsReader("mybatis.xml");
sessionFactory =new SqlSessionFactoryBuilder().build(reader,"myOracle");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static SqlSession getSession(){
return sessionFactory.openSession();
}
}
建立mybatis主配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--列印日志可以看執行的sql語句-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="myOracle">
<environment id="myOracle">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/aaa/mybatis/dao/TextDaoMapper.xml"></mapper><!--指向mapper檔案的路徑-->
</mappers>
</configuration>
建立測試類
package com.aaa.mybatis.text;
import com.aaa.mybatis.dao.TextDao;
import com.aaa.mybatis.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Text {
public static void main(String[] args) {
//輸入一個園的半徑傳回這個圓的周長和面積 這裡我們通過傳Map的方式,穿過去兩個key,一個值,因為存儲過程
//有一個出參和一個出入參。
SqlSession session= SqlSessionFactoryUtil.getSession();
TextDao mapper = session.getMapper(TextDao.class);
Map map=new HashMap();
map.put("r",5);
map.put("s",null);
mapper.returnSorC(map);
System.out.println("這個圓的周長是:"+map.get("r")+"面積是:"+map.get("s"));
session.close();
}
}
測試結果
Logging initialized using \'class org.apache.ibatis.logging.stdout.StdOutImpl\' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 701141022.
Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@29ca901e]
==> Preparing: { call pro_text( ?, ? ) } ---執行的sql
==> Parameters: 5(Integer)
這個圓的周長是:30面積是:75 ---執行的結果
Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@29ca901e]
Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@29ca901e]
Returned connection 701141022 to pool.
Process finished with exit code 0
發表于
2019-05-19 16:35
一天兩天、
閱讀(10998)
評論(0)
編輯
收藏
舉報