springmvc+mybatis使用存储过程查询多结果集
2016-10-17 14:08
411 查看
<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif;">有时候查询一些数据需要存储过程,使用存储过程能大大的减少查询次数,增强性能。如下是使用存储过程完成一段复杂的查询。查询内容是根据课程的ID,难易程度和题目数量随机查出来题目。数据库如下</span>DROP TABLE IF EXISTS `exam_examquestions`;
CREATE TABLE `exam_examquestions` (
`examQuestion_ID` int(11) NOT NULL AUTO_INCREMENT,
`examQuestion_UseType` int(11) NOT NULL DEFAULT '1' COMMENT '1:作业;2:模拟试题;3:正式考试;4:项目',
`examQuestion_Type` int(100) NOT NULL COMMENT '0:单选题;1:多选题;2:判断题;3:填空题;4:问答题',
`examQuestion_Stem` varchar(4000) NOT NULL,
`examQuestion_Score` int(11) NOT NULL,
`examQuestion_Answer` varchar(4000) NOT NULL DEFAULT '<p></p>' COMMENT '判断题中(0:错;1:对)',
`examQuestion_Analysis` varchar(500) DEFAULT NULL,
`examQuestion_Metas` varchar(4000) DEFAULT NULL,
`examQuestion_Difficulty` int(11) NOT NULL,
`examQuestion_TargetClassIDs` varchar(100) DEFAULT NULL,
`examQuestion_StatusID` int(11) DEFAULT NULL,
`examCategory_ID` int(11) NOT NULL,
PRIMARY KEY (`examQuestion_ID`),
KEY `Refexam_category1` (`examCategory_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=145 DEFAULT CHARSET=utf8;存储过程如下
<result column="@min" property="minid" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="@max" property="maxid" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="@gap" property="gap" javaType="java.lang.Integer" jdbcType="INTEGER" />
</resultMap>
<resultMap type="QuestionResult" id="questionList">
<result column="examQuestion_ID" property="examQuestion_ID" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestion_Stem" property="examQuestion_Stem" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="examQuestion_Type" property="examQuestion_Type" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestion_Score" property="examQuestion_Score" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestion_Analysis" property="examQuestion_Analysis" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="examQuestion_UseType" property="examQuestion_UseType" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestionItem_ID" property="examQuestionItem_ID" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestionItem_Title" property="examQuestionItem_Title" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="examQuestionItem_StatusID" property="examQuestionItem_StatusID" javaType="java.lang.Integer" jdbcType="INTEGER" />
</resultMap>
<!-- @classIDs @difficultyID @singleCount @useType -->
<!-- 根据 classIDs 查找随机查找单选题 -->
<select id="createTestpaperForSingle" parameterType="java.util.Map" statementType="CALLABLE" resultMap="examIDResult,questionList">
{call sel_exam_questionsanditem_classids_single(#{_classIDs,mode=IN,jdbcType=VARCHAR},#{_difficultyID,mode=IN,jdbcType=INTEGER},#{_testPaperItem_Count,mode=IN,jdbcType=INTEGER},#{_useType,mode=IN,jdbcType=INTEGER})}
</select>
public List<List<?>> createTestpaperForSingle(String mapper, Map<String, Object> map);
}
CREATE TABLE `exam_examquestions` (
`examQuestion_ID` int(11) NOT NULL AUTO_INCREMENT,
`examQuestion_UseType` int(11) NOT NULL DEFAULT '1' COMMENT '1:作业;2:模拟试题;3:正式考试;4:项目',
`examQuestion_Type` int(100) NOT NULL COMMENT '0:单选题;1:多选题;2:判断题;3:填空题;4:问答题',
`examQuestion_Stem` varchar(4000) NOT NULL,
`examQuestion_Score` int(11) NOT NULL,
`examQuestion_Answer` varchar(4000) NOT NULL DEFAULT '<p></p>' COMMENT '判断题中(0:错;1:对)',
`examQuestion_Analysis` varchar(500) DEFAULT NULL,
`examQuestion_Metas` varchar(4000) DEFAULT NULL,
`examQuestion_Difficulty` int(11) NOT NULL,
`examQuestion_TargetClassIDs` varchar(100) DEFAULT NULL,
`examQuestion_StatusID` int(11) DEFAULT NULL,
`examCategory_ID` int(11) NOT NULL,
PRIMARY KEY (`examQuestion_ID`),
KEY `Refexam_category1` (`examCategory_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=145 DEFAULT CHARSET=utf8;存储过程如下
BEGIN SELECT @min := MIN(examQuestion_ID), @max := MAX(examQuestion_ID), @gap := MAX(examQuestion_ID) - MIN(examQuestion_ID) FROM exam_examquestions WHERE find_in_set(examQuestion_TargetClassIDs,_classIDs) AND examQuestion_UseType =_useType AND examQuestion_Type = 0; PREPARE q1 FROM 'SELECT a.examQuestion_ID, a.examQuestion_Answer, a.examQuestion_Stem, a.examQuestion_Type, a.examQuestion_Score, a.examQuestion_Analysis, a.examQuestion_UseType, b.examQuestionItem_ID, b.examQuestionItem_Title, b.examQuestionItem_StatusID FROM exam_examquestions a JOIN ( SELECT examQuestion_ID FROM ( SELECT examQuestion_ID FROM ( SELECT ? + (? - ? + 1 - ?) * RAND() AS START FROM DUAL ) AS init JOIN exam_examquestions y WHERE y.examQuestion_ID > init. START ORDER BY y.examQuestion_ID LIMIT ? ) z ORDER BY RAND() LIMIT ? ) r ON a.examQuestion_ID = r.examQuestion_ID LEFT JOIN exam_examquestionitems b ON a.examQuestion_ID = b.examQuestion_ID WHERE find_in_set(a.examQuestion_TargetClassIDs,?) AND a.examQuestion_UseType = ? AND a.examQuestion_Difficulty = ? AND a.examQuestion_Type = 2 Order By a.examQuestion_ID;'; SET @classIDs = _classIDs; SET @difficultyID = _difficultyID; SET @singleCount = _singleCount; SET @useType = _useType; EXECUTE q1 USING @min ,@max, @min, @gap, @gap, @singleCount, @classIDs, @useType, @difficultyID; DEALLOCATE PREPARE q1; END
写入mapper
<resultMap type="ExamIDResult" id="examIDResult"><result column="@min" property="minid" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="@max" property="maxid" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="@gap" property="gap" javaType="java.lang.Integer" jdbcType="INTEGER" />
</resultMap>
<resultMap type="QuestionResult" id="questionList">
<result column="examQuestion_ID" property="examQuestion_ID" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestion_Stem" property="examQuestion_Stem" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="examQuestion_Type" property="examQuestion_Type" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestion_Score" property="examQuestion_Score" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestion_Analysis" property="examQuestion_Analysis" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="examQuestion_UseType" property="examQuestion_UseType" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestionItem_ID" property="examQuestionItem_ID" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result column="examQuestionItem_Title" property="examQuestionItem_Title" javaType="java.lang.String" jdbcType="VARCHAR" />
<result column="examQuestionItem_StatusID" property="examQuestionItem_StatusID" javaType="java.lang.Integer" jdbcType="INTEGER" />
</resultMap>
<!-- @classIDs @difficultyID @singleCount @useType -->
<!-- 根据 classIDs 查找随机查找单选题 -->
<select id="createTestpaperForSingle" parameterType="java.util.Map" statementType="CALLABLE" resultMap="examIDResult,questionList">
{call sel_exam_questionsanditem_classids_single(#{_classIDs,mode=IN,jdbcType=VARCHAR},#{_difficultyID,mode=IN,jdbcType=INTEGER},#{_testPaperItem_Count,mode=IN,jdbcType=INTEGER},#{_useType,mode=IN,jdbcType=INTEGER})}
</select>
写dao接口
public interface DaoForCreateTestpaper {public List<List<?>> createTestpaperForSingle(String mapper, Map<String, Object> map);
}
dao实现
@Repository("daoForCreateTestpaperSupport") public class DaoForCreateTestpaperSupport implements DaoForCreateTestpaper{ @Resource(name = "sqlSessionTemplate") private SqlSessionTemplate sqlSessionTemplate; @Override public List<List<?>> createTestpaperForSingle(String mapper, Map<String, Object> map) { // TODO Auto-generated method stub return sqlSessionTemplate.selectList(mapper, map); } }
实现service
@Override public List<List<?>> createTestpaperForSingle(Map<String, Object> map) throws Exception { return createDao.createTestpaperForSingle("DaoForCreateTestpaper.createTestpaperForSingle", map); }
controller调用
parametersToSqlSelect.put("_useType", tp.get("testPaper_Type")); parametersToSqlSelect.put("_difficultyID", tp.get("testPaper_DifficultType")); parametersToSqlSelect.put("_classIDs", tp.get("testPaper_ClassIDs")); parametersToSqlSelect.put("_testPaperItem_Count", tp.get("testPaperItem_Count")); parametersToSqlSelect.put("_isBasedOnCourse", tp.get("testPaper_IsBaseOnCourse")); parametersToSqlSelect.put("_testPaper_CourseID", tp.get("testPaper_CourseID")); if ((Integer)parametersToSqlSelect.get("_isBasedOnCourse") == 0) { List<List<?>> singleListresults = TestpaperService.createTestpaperForSingle(parametersToSqlSelect); System.out.println(".................."+singleListresults); System.out.println(".....----......."+ (List<QuestionResult>)singleListresults.get(1));}
相关文章推荐
- SpringMVC +mybatis in 查询不报错但是没有结果
- Spring MVC+Mybatis 执行存储过程,使用Map进行参数的传递
- 阶梯式使用SpringMVC+MyBatis
- ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)
- springMVC+mybatis+spring security<三>:使用数据库管理资源
- Spring+SpringMVC+mybatis+easyui整合实例----使用mybatis拦截器分页
- 菜鸟使用SSM框架搭建web服务器实现登录功能(Spring+SpringMVC+Mybatis)
- 在存储过程中使用另一个存储过程返回的查询结果集
- 关于mysql 数据库使用spring 2.0低版本 JdbcTemplate.queryForList 查询结果别名不起作用的解决方法
- Springmvc+mybatis+maven查询分页完美封装版
- (菜鸟来动手)SpringMVC+Spring+Mybatis整合(第二天) 使用maven来构建项目
- Spring+SpringMVC+mybatis+easyui整合实例(五)使用mybatis拦截器分页
- SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
- 在使用mybatis时遇到查询结果返回为空的情况,但是查数据库能查到
- mybatis 使用oracle查询返回结果总是null
- springMVC+mybatis+dwr中如何使用dwr
- 使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB
- 使用MVC,实现多条件查询,排序与分页 的 存储过程
- Spring+SpringMVC+mybatis+easyui整合实例(二)注解方式的mybatis的使用
- Mybatis使用MySQL进行模糊查询时输入中文检索不到结果