我不能在 MyBatis 参数中放入一个 HashMap 对象
I can't put a HashMap object in the MyBatis parameter
我正在尝试使用 HashMap 对象在数据库中查找 ID 和密码,但我做不到。我只能得到如下错误信息:
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
我将我的 MyBatis 映射器设置如下:
<select id="selectOneByNTB" parameterType="hashMap" resultType="custDto">
select * from Customer_info where cust_name = #{name} and cust_telephone=#{telephone} and cust_birthday=#{birthday}
</select>
而且,我认为我的控制器和 dao 没问题,但我会附上它。
@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");
}
我检查了控制器中的 HashMap 变量并且 dao 正确地打印了它们的项目。
所以,我认为映射器是问题所在。我的映射器有什么问题吗?或者我的其他代码有任何其他问题吗?
仅供参考,我会把我的 pom.xml 亲属,
<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>
我没有收到错误..
字节尝试更改此代码:
HashMap<String, String> map = new HashMap<String, String>();
HashMap中的第二个参数为Object,可以输入String,int类型不仅是string类型
进入这段代码
HashMap<String, Object> map = new HashMap<>();
在selectOneByNTB()
方法中,需要传递HashMap
参数作为selectOne()
的第二个参数。
public Customer_infoDto selectOneByNTB(HashMap<String, String> map) {
System.out.println("name"+((map.get("cust_name"))));
return ss.selectOne(NameSpace + "selectOneByNTB", map);
}
我正在尝试使用 HashMap 对象在数据库中查找 ID 和密码,但我做不到。我只能得到如下错误信息:
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
我将我的 MyBatis 映射器设置如下:
<select id="selectOneByNTB" parameterType="hashMap" resultType="custDto">
select * from Customer_info where cust_name = #{name} and cust_telephone=#{telephone} and cust_birthday=#{birthday}
</select>
而且,我认为我的控制器和 dao 没问题,但我会附上它。
@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");
}
我检查了控制器中的 HashMap 变量并且 dao 正确地打印了它们的项目。
所以,我认为映射器是问题所在。我的映射器有什么问题吗?或者我的其他代码有任何其他问题吗?
仅供参考,我会把我的 pom.xml 亲属,
<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>
我没有收到错误.. 字节尝试更改此代码:
HashMap<String, String> map = new HashMap<String, String>();
HashMap中的第二个参数为Object,可以输入String,int类型不仅是string类型
进入这段代码
HashMap<String, Object> map = new HashMap<>();
在selectOneByNTB()
方法中,需要传递HashMap
参数作为selectOne()
的第二个参数。
public Customer_infoDto selectOneByNTB(HashMap<String, String> map) {
System.out.println("name"+((map.get("cust_name"))));
return ss.selectOne(NameSpace + "selectOneByNTB", map);
}