MyBatisProcedure
来源:互联网 发布:八爪鱼采集软件免费版 编辑:程序博客网 时间:2024/06/07 05:53
注意:有两种传参数的方式,Map 或 bean
mybatis中 Mapper 文件
<!--learn procedure--> <!--for test procedure with map paramater--> <parameterMap type="map" id="testProcedure"> <parameter property="p1" jdbcType="INTEGER" mode="IN"/> <parameter property="p2" jdbcType="INTEGER" mode="IN"/> <parameter property="result" jdbcType="INTEGER" mode="OUT"/> <parameter property="report" jdbcType="VARCHAR" mode="OUT"/> </parameterMap> <select id="selectSummaryReportWithMap" parameterMap="testProcedure" statementType="CALLABLE"> {call micro_message.playMybatis( ?,?,?,? )} </select> <!--for test procedure with bean paramater--> <select id="selectSummaryReportWithBean" parameterType="com.imooc.bean.SummaryReport" statementType="CALLABLE"> {call micro_message.playMybatis( #{p1,jdbcType=INTEGER, mode=IN}, #{p2,jdbcType=INTEGER, mode=IN}, #{result, jdbcType=INTEGER,mode=OUT}, #{report, jdbcType=VARCHAR,mode=OUT} )} </select> <!-- end learn procedure-->
The Client
/** * Created by shan on 2016/2/17. */public class CommandDao { //使用 map 参数 public void ProcedureWithMapParamater(int p1,int p2) { DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); Map<String, Object> map = new HashMap<String, Object>(); map.put("p1", p1); map.put("p2", p2); sqlSession.selectOne("CommandContent.selectSummaryReportWithMap", map); Logger logger = Logger.getLogger(CommandDao.class); logger.info(map.get("result")); logger.info(map.get("report")); } catch (IOException e) { e.printStackTrace(); } } //使用 bean 参数 public void ProcedureWithBeanParamater(int p1,int p2) { DBAccess dbAccess = new DBAccess(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); SummaryReport summaryReport = new SummaryReport(); summaryReport.setP1(p1); summaryReport.setP2(p2); sqlSession.selectOne("CommandContent.selectSummaryReportWithBean", summaryReport); Logger logger = Logger.getLogger(CommandDao.class); logger.info(summaryReport.getResult()); logger.info(summaryReport.getReport()); } catch (IOException e) { e.printStackTrace(); } }}
测试代码:
/** * test call procedure in map paramater */ @Test public void testProcedure(){ commandDao.ProcedureWithMapParamater(23,22); } /** * test call procedure in bean paramater */ @Test public void testProcedureWithBean(){ commandDao.ProcedureWithBeanParamater(11,31); }
output1:
2016-02-24 16:14:24,036 [main] DEBUG [CommandContent.selectSummaryReportWithBean] - ==> Preparing: {call micro_message.playMybatis( ?, ?, ?, ? )}2016-02-24 16:14:24,113 [main] DEBUG [CommandContent.selectSummaryReportWithBean] - ==> Parameters: 23(Integer), 22(Integer)2016-02-24 16:14:24,139 [main] INFO [com.imooc.dao.CommandDao] - 452016-02-24 16:14:24,139 [main] INFO [com.imooc.dao.CommandDao] - 45 is the result at 2016-02-24 16:14:24
output2:
2016-02-24 16:54:37,184 [main] DEBUG [CommandContent.selectSummaryReportWithBean] - ==> Preparing: {call micro_message.playMybatis( ?, ?, ?, ? )}2016-02-24 16:54:37,266 [main] DEBUG [CommandContent.selectSummaryReportWithBean] - ==> Parameters: 11(Integer), 31(Integer)2016-02-24 16:54:37,272 [main] INFO [com.imooc.dao.CommandDao] - 422016-02-24 16:54:37,273 [main] INFO [com.imooc.dao.CommandDao] - 42 is the result at 2016-02-24 16:54:37
存储过程,这就是做个加法
DELIMITER $$USE `micro_message`$$DROP PROCEDURE IF EXISTS `playMybatis`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `playMybatis`(IN p1 INT,IN p2 INT,OUT result INT,OUT report VARCHAR(50))BEGIN SELECT p1+p2 INTO @a; SET result = @a; SELECT CONCAT(@a,' is the result at ',NOW()) INTO @b; SET report = @b; END$$DELIMITER ;
0 0
- MyBatisProcedure
- Android java.lang.reflect.InvocationTargetException 错误
- java中常用的字符串处理类
- Spark TopK 问题解决-使用最小堆
- ASP.NET 系统支持数据库访问设计
- iOS NSData转换成NSDictionary
- MyBatisProcedure
- [技术讨论]程序代码是否可能发生自我变异
- eclipse plugin 导出插件包
- json封装 与 解析
- Jrebel集成到 tomcat中
- UVa 12563 Jin Ge Jin Qu hao dp : 0-1背包问题
- ansible 在python中的使用
- Xcode项目调试-1.去掉多余的证书
- 组合,关联,聚合的区别