简体   繁体   中英

How to pass argument from Java using HashMap to myBatis?

How to pass input parameter as array in myBatis stored Procedure using Java?

In my case I pass Long [] array

I want to pass value as array in my stored procedure input, and I am trying to put jdbcType=ARRAY but I got error,

ClassCastException java.lang.Long cannot convert java.sql.Array

Can you help in code

An error in my code...?

For your reference I added my code. This is my spring DAO layer for user. From here i need to call myBatis

UserDAO.java

@Autowired
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
    super.setSqlSessionTemplate(SqlSessionTemplate)
}


public List<User> getUserList(List<Long> userId) {
    Long[] arr = userId.toArray(new Long[userId.size()]);
    Map<String,Object> queryParameter = new HashMap<String,Object>();
    queryParameter.put("P_PARAM_ID_ARR",arr);// Here i got error how to convert Long[] to  java.sql.Array.
    this.sqlSession().selectList("getUserDatas",queryParameter);
    List<User> userList = (List<User>)queryParameter.get("P_RST");

}

This is my myBatis Queries xml file userqueries.xml

<mapper namespace = "userDAO">
    <resultMap id= "userMapping" type="com.User">
        <result property="id" column="USER_ID" javaType="java.lang.Long"></result>
        <result property="name" column="USER_NAME" javaType="java.lang.String"></result>
    </resultMap>

    <select id="getUserDatas" parameterType="java.util.Map" statementType="CALLABLE" resultMap="userMapping">

    { call PROC_USER(#{P_PARAM_ID_ARR, jdbcType=ARRAY, mode=IN}, #{P_RST, javaType=java.sql.ResultSet, jdbcType=CURSOR, mode=OUT, resultMap="userMapping"})}

    </select>

</mapper>

When I am trying to rum I always get 500 error.

How to pass array as argument in procedure?

Thankyou Please help me.....

Arrays in JDBC need to be created through the factory method on java.sql.Connection. Something like this:

Long[] arr = userId.toArray(new Long[userId.size()]);
Array sqlArray = sqlSession().getConnection().createArrayOf("int", arr);
Map<String,Object> queryParameter = new HashMap<String,Object>();
queryParameter.put("P_PARAM_ID_ARR",sqlArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM