I can't put a HashMap object in the MyBatis parameter

Issue

I’m trying use a HashMap object to look up ids and passwords in database, but I can’t. I can only get error message as follows:

Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 부적합한 열 유형: 1111

I set my MyBatis mapper as follows:

  <select id="selectOneByNTB" parameterType="hashMap" resultType="custDto">
    select * from Customer_info where cust_name = #{name} and cust_telephone=#{telephone} and cust_birthday=#{birthday}
  </select>

And, I think it’s ok with my controller and dao, but I’ll attach it.

@RequestMapping(value="/findId", method=RequestMethod.POST)
    public String findId(@ModelAttribute Customer_infoDto cuDto, HttpServletResponse response) throws Exception {
        
        HashMap<String, String> map = new HashMap<String, String>();    
        map.put("name", cuDto.getCust_name());
        map.put("telephone", cuDto.getCust_telephone());
        map.put("birthday", cuDto.getCust_birthday());
        
        
        System.out.println("name: "+map.get("name"));
        System.out.println("telephone: "+map.get("telephone"));
        System.out.println("birthday: "+map.get("birthday"));
                
        
        String cust_id = cudao.selectOneByNTB(map).getCust_id();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF");
        PrintWriter out = response.getWriter();
        out.println("<meta charset='UTF-8'><script>alert('Your ID is "+ cust_id +"'); history.go(-1);</script>");
        out.flush();        
        return "redirect:/findIdAndPw";
    }
public Customer_infoDto selectOneByNTB(HashMap<String, String> map) {
        System.out.println("name"+((map.get("cust_name"))));
        return ss.selectOne(NameSpace + "selectOneByNTB");
    }

I checked the HashMap variable in the controller and dao correctly print their items.

So, I think the mapper is the problem. Is there anything wrong in my mapper? or any other problems with my other codes?

FYI, I’ll put my pom.xml dependecies,

<dependencies>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.30.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.30.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.30.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency> 
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.6</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>   
    <dependency>
        <groupId>oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency> 
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.6</version>
    </dependency>
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.5.4</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.3</version>
    </dependency>   
  </dependencies>

Solution

In the selectOneByNTB() method, you need to pass the HashMap parameter as the second argument of selectOne().

public Customer_infoDto selectOneByNTB(HashMap<String, String> map) {
  System.out.println("name"+((map.get("cust_name"))));
  return ss.selectOne(NameSpace + "selectOneByNTB", map);
}

Answered By – ave

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published