Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.baosight.webapp.sqlmap.addPro
Namespace为映射别名
production.xml
<?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">
<resultMap type="com.baosight.webapp.bean.Production" id="production">
<result property="pNumber" column="pNumber" javaType="java.lang.Integer" jdbcType="INTEGER" />
<result property="picture" column="picture" />
<result property="category" column="category" />
<result property="brand" column="brand" />
<result property="pName" column="pName" />
<result property="barcode" column="barcode" javaType="java.lang.Integer"/>
<result property="bPrice" column="bPrice" javaType="java.lang.Double"/>
<result property="sPrice" column="sPrice" javaType="java.lang.Double"/>
</resultMap>
<select id="selectProBypNumber" resultMap="production">
select * from b_production where pNumber = #{pNumber}
</select>
<select id="selectAll" resultMap="production">
select * from b_production
</select>
<insert id="addPro" parameterType="production" >
insert into
b_production(pNumber,category ,brand, pName, barcode, bPrice, sPrice)
values(#{pNumber},#{category},#{brand},#{pName},#{barcode},#{bPrice},#{sPrice})
</insert>
mybatisTest.java
package com.baosight.webapp.common;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.baosight.webapp.bean.Production;
public class mybatisTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static{
try{
reader = Resources.getResourceAsReader("com/baosight/webapp/Configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e){
e.printStackTrace();
}
}
public static SqlSessionFactory getSession(){
return sqlSessionFactory;
}
public static void main(String[] args) {
SqlSession session = sqlSessionFactory.openSession();
try {
// Production production = session.selectOne("com.baosight.webapp.bean.Production.selectProBypNumber",999);
// List list = session.selectList("com.baosight.webapp.bean.Production.selectAll");
// System.out.println(list.size());
Production p=new Production();
double price=12.0;
int barcode=11;
int brand=100;
p.setbPrice(88.0);
p.setBrand("h");
p.setCategory("特步");
p.setpName("运动鞋");
p.setBarcode(77);
p.setpNumber(854);
p.setsPrice(4.6);
System.out.println(p.toString());
session.insert("com.baosight.webapp.sqlmap.addPro",p);
System.out.println(p.toString());
session.commit();
}catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.close();
}
}
}
转一个我搜到的答案:
Mapped Statements collection does not contain value for后面是什么类什么方法之类的:
错误原因有几种:
1、mapper.xml中没有加入namespace
2、mapper.xml中的方法和接口mapper的方法不对应
3、mapper.xml没有加入到mybatis-config.xml中(即总的配置文件),例外:配置了mapper文件的包路径的除外
4、mapper.xml文件名和所写的mapper名称不相同。
原答案链接:http://your233.iteye.com/blog/1563240