1. 儲存過程準備
CREATE PROCEDURE sp_task ( IN userId INT ) BEGIN SELECT * FROM task WHERE user_id = userId; END
2. MyBatis呼叫儲存過程
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.libing.helloworld.dao.ITaskDao"> <resultMap id="baseResultMap" type="com.libing.helloworld.model.Task"> <id property="id" column="id" /> <result property="userId" column="user_id" /> <result property="taskName" column="task_name" /> <result property="content" column="content" /> <result property="createdDate" column="created_date" javaType="java.util.Date" jdbcType="DATE" /> </resultMap> <select id="findTasksByUserId" statementType="CALLABLE" resultMap="baseResultMap"> <![CDATA[ { CALL sp_task(#{userId, jdbcType=INTEGER, mode=IN}) } ]]> </select> </mapper>