mybatis获取mysql存储过程out参数的值_mybatis接受mysql存储过程out的值
這是題目的實體類
privateInteger id;private Integer type;//題型 單選,多選
private String category;//類型 數據字典配置
private String problem;//問題
private String choose1;//選擇1
privateString choose2;privateString choose3;privateString choose4;privateString choose5;privateString choose6;privateString choose7;privateString choose8;private String answer;//答案
卷子的實體類
privateInteger id;privateString title;private String problemNo;//題目,逗號分割
private Integer extract;//抽題個數
需求:已經錄入若干提,其中每題的類型不完全相同,例如有的題目是語文,有的是數學之類的。
抽取其中若干題,必然是小于錄入題的,必須保證每種類型的題都被抽取到,且希望能根據不同類型的題目均勻抽取;
例如錄入40題,抽取10題。
假如語文,數學,物理,化學各是16,12,8,4。那么抽取語文,數學,物理,化學應當各是4,3,2,1道題。
假如語文,數學,物理,化學各是19,19,1,1。那么抽取語文,數學,物理,化學應當各是4,4,1,1道題。
作出最優解。
表結構:
CREATE TABLE`tb_multi_question_problem` (
`id`int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`type`tinyint(4) DEFAULT NULL COMMENT '題型',
`category`varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '類型',
`problem`varchar(512) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '問題',
`choose1`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇1',
`choose2`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇2',
`choose3`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇3',
`choose4`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇4',
`choose5`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇5',
`choose6`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇6',
`choose7`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇7',
`choose8`varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '選擇8',
`answer`varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`update_time`datetime DEFAULT NULL COMMENT '更新時間',
`create_time`datetime DEFAULT NULL COMMENT '創建時間',
`update_user`varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`create_user`varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`del_flag`char(1) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',PRIMARY KEY(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=58 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE`tb_multi_questionnaire` (
`id`int(10) NOT NULLAUTO_INCREMENT,
`title`varchar(128) DEFAULT NULL,
`description`text,
`imgurl`varchar(128) DEFAULT NULL,
`problem_no`varchar(512) DEFAULT NULL,
`parent_area`varchar(32) DEFAULT NULL,
`area`varchar(32) DEFAULT NULL,
`committee`varchar(32) DEFAULT NULL,
`start_time`datetime DEFAULT NULL,
`end_time`datetime DEFAULT NULL,
`duration`int(10) DEFAULT NULL COMMENT '時長',
`number` tinyint(4) DEFAULT NULL,
`extract`tinyint(4) DEFAULT NULL,
`url1`varchar(256) DEFAULT NULL,
`url2`varchar(256) DEFAULT NULL,
`url3`varchar(256) DEFAULT NULL,
`update_time`datetime DEFAULT NULL COMMENT '更新時間',
`create_time`datetime DEFAULT NULL COMMENT '創建時間',
`update_user`varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`create_user`varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`del_flag`char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',PRIMARY KEY(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
解決方案,因為涉及到類型,且類型未均勻分布。避免多次訪問數據庫,使用存儲過程;
CREATE DEFINER=`root`@`localhost` PROCEDURE `rand_question`(in pid int, out idstr VARCHAR(128))begin
declare pno VARCHAR(128);declare qp VARCHAR(32);DECLARE ques_ext int DEFAULT 1;DECLARE ques_sum int DEFAULT 0;DECLARE qpid VARCHAR(32);DECLARE qpCategory VARCHAR(32) CHARACTER SETutf8mb4 COLLATE utf8mb4_unicode_ci;DECLARE qpSum INT(10);DECLARE q_sum INT(10) DEFAULT 0;DECLARE rem INT(10) DEFAULT 0;DECLARE lim INT(10) DEFAULT 0;declare done int default -1;DECLARE qp_cursor CURSOR FOR SELECT qp.category,COUNT(1) FROM tb_multi_question_problem qp WHERE del_flag = '0' and FIND_IN_SET(qp.id,(SELECT q.problem_no FROM tb_multi_questionnaire q WHERE del_flag = '0' and q.id = pid)) GROUP BYqp.category;DECLARE CONTINUE HANDLER FOR NOT found SET done = 1;SELECT q.problem_no INTO pno FROM tb_multi_questionnaire q WHERE del_flag = '0' and q.id =pid;SELECT q.extract into ques_ext FROM tb_multi_questionnaire q WHERE del_flag = '0' and q.id =pid;SELECT COUNT(1) into ques_sum FROM tb_multi_question_problem qp WHERE del_flag = '0' and FIND_IN_SET(qp.id,(SELECT q.problem_no FROM tb_multi_questionnaire q WHERE del_flag = '0' and q.id =pid));set idstr = '';OPENqp_cursor;
qpLoop : LOOPFETCH qp_cursor INTOqpCategory,qpSum;IF done = 1 THENLEAVE qpLoop;END IF;set rem = floor(qpSum*ques_ext/ques_sum);IF rem < 1 THEN
SET rem = 1;END IF;SELECT group_concat(id) INTO qpid FROM (SELECT id FROM tb_multi_question_problem WHERE del_flag = '0' and category = qpCategory and FIND_IN_SET(id,pno) and id >= (SELECT FLOOR(RAND()*(SELECT MAX(id) FROM tb_multi_question_problem WHERE del_flag = '0' and category = qpCategory))) order by rand() LIMIT rem) asa;set idstr = CONCAT(idstr,qpid,',');ENDLOOP qpLoop;CLOSEqp_cursor;select length(idstr)-length(replace(idstr,',','')) INTOq_sum;WHILE q_sum = (SELECT FLOOR(RAND()*(SELECT MAX(id) FROM tb_multi_question_problem WHERE del_flag = '0'))) order by rand() LIMIT lim) as a WHEREFIND_IN_SET(a.id,pno);set idstr = CONCAT(idstr,qpid,',');select length(idstr)-length(replace(idstr,',','')) INTOq_sum;END WHILE;end
得到所得的值有兩種方法,第一種:
使用map傳遞接受參數
mapper.xml
{
call rand_question(
#{id,jdbcType=INTEGER,mode=IN},
#{idstr, jdbcType=VARCHAR,mode=OUT})
}
service
public List randomList(Mapparam) {
questionProblemDao.randomList(param);return questionProblemDao.getListByIds((String)param.get("idstr"));
}
如上,通過param傳參,也通過param接受參數;
第二種通過javabean,有時間再補充
總結
以上是生活随笔為你收集整理的mybatis获取mysql存储过程out参数的值_mybatis接受mysql存储过程out的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中super关键字_java中s
- 下一篇: linux cmake编译源码,linu