In this article, we will learn about the use and running process of Mybatis calls stored procedures. First, we create a simple stored procedure
DELIMITER $ CREATE PROCEDURE mybatis.ges_user_count(IN age INT, OUT user_count INT) BEGIN SELECT COUNT(*) FROM users WHERE users.age=age INTO user_count; END $
The meaning of this stored procedure is actually relatively simple, which is to enter age and then execute select count(*) from users where users.age = age into user_count; it is relatively simple to obtain the number of people with age equal to age to assign value to user_count.
Next is the call of the stored procedure. Execute the following command to complete the call of the stored procedure.
Next, let's take a look at how stored procedures are called using Mybatis.
userMapper.xml adds stored procedure call configuration:
<select id="count" statementType="CALLABLE" parameterMap="getUserCountMap"> CALL mybatis.ges_user_count(?,?) </select>
Main function:
public class Learn1Main { public static void main(String [] args){ //mybatis configuration file String resource = "learn/mybatis-config.xml"; //Use class loader to load mybatis configuration file (it also loads the associated mapping file) InputStream is = Learn1Main.class.getClassLoader().getResourceAsStream(resource); //Build the factory of sqlSessionSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession session = sessionFactory.openSession(); Map<String, Integer> parameterMap = new HashMap<String, Integer>(); parameterMap.put("age", 12); parameterMap.put("user_count", -1); session.selectOne("com.tianjunwei.learn.learn1.entity.User.count", parameterMap); Integer result = parameterMap.get("user_count"); System.out.println(result); } }Running results:
The final execution process is in the DefaultResultSetHandler. There is still a difference between calling ordinary SQL and stored procedures. The execution of Sql statements uses CallableStatement.
// // HANDLE OUTPUT PARAMETER // // Call the stored procedure to return the result, put the result value in the parameter @Override public void handleOutputParameters(CallableStatement cs) throws SQLException { final Object parameterObject = parameterHandler.getParameterObject(); final MetaObject metaParam = configuration.newMetaObject(parameterObject); final List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); /Loop for each parameter for (int i = 0; i < parameterMappings.size(); i++) { final ParameterMapping parameterMapping = parameterMappings.get(i); //Judge the pattern of the parameter if (parameterMapping.getMode() == ParameterMode.OUT || parameterMapping.getMode() == ParameterMode.INOUT) { if (ResultSet.class.equals(parameterMapping.getJavaType())) { handleRefCursorOutputParameter((ResultSet) cs.getObject(i + 1), parameterMapping, metaParam); } else { final TypeHandler<?> typeHandler = parameterMapping.getTypeHandler(); metaParam.setValue(parameterMapping.getProperty(), typeHandler.getResult(cs, i + 1)); } } } } private void handleRefCursorOutputParameter(ResultSet rs, ParameterMapping parameterMapping, MetaObject metaParam) throws SQLException { try { final String resultMapId = parameterMapping.getResultMapId(); final ResultMap resultMap = configuration.getResultMap(resultMapId); final DefaultResultHandler resultHandler = new DefaultResultHandler(objectFactory); final ResultSetWrapper rsw = new ResultSetWrapper(rs, configuration); handleRowValues(rsw, resultMap, resultHandler, new RowBounds(), null); metaParam.setValue(parameterMapping.getProperty(), resultHandler.getResultList()); } finally { // issue #228 (close resultssets) closeResultSet(rs); } }The above is the stored procedure call and operation process of Mybatis source code analysis introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!