0

使用“mybatis 3.2.7.jar”时,我的项目中遇到映射错误,但它使用 3.0 版时有效。


(试用基于loaine sample hello world mybatis)这是整个示例项目文件:

package test.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import test.model.Customer;
import test.model.CustomerExample;

public class CustomerDAO implements CustomerMapper{

    private SqlSessionFactory sqlSessionFactory; 

    public CustomerDAO(){
        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    }

    @Override
    public int countByExample(CustomerExample example) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int deleteByExample(CustomerExample example) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Integer CONTACT_ID) {
        SqlSession session = sqlSessionFactory.openSession();

        try {
            session.insert("Customer.deleteByPrimaryKey", CONTACT_ID);
            session.commit();
        } finally {
            session.close();
        }

        return 0;
    }

    @Override
    public int insert(Customer record) {
        SqlSession session = sqlSessionFactory.openSession();

        try {
            session.insert("Customer.insert", record);
            session.commit();
        } finally {
            session.close();
        }
        return 0;
    }

    @Override
    public int insertSelective(Customer record) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<Customer> selectByExample(CustomerExample example) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Customer selectByPrimaryKey(Integer CONTACT_ID) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int updateByExampleSelective(Customer record, CustomerExample example) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int updateByExample(Customer record, CustomerExample example) {
        // TODO Auto-generated method stub
        return 0;
    }

    public int updateByPrimaryKeySelective(Customer record) {
        int successStatus = 0;
        SqlSession session = sqlSessionFactory.openSession();
        try {
            successStatus = session.update("Customer.updateByPrimaryKeySelective", record);
            session.commit();
        } finally {
            session.close();
        }
        return successStatus;
    }

    @Override
    public int updateByPrimaryKey(Customer record) {
        // TODO Auto-generated method stub
        return 0;
    }

}


*******************************************************************************************
package test.trial;

import test.dao.CustomerDAO;
import test.model.Customer;
import test.model.CustomerExample;



public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //insert
        Customer cus = new Customer();
        CustomerDAO cdao= new CustomerDAO();
        CustomerExample ce = new CustomerExample();

        cus.setCONTACT_ID(201);
        cus.setCONTACT_NAME("**** 2");
        cus.setCONTACT_EMAIL("xtian");
        cus.setCONTACT_PHONE("12345");


        //count
//      cdao.deleteByPrimaryKey(105);
        cdao.updateByPrimaryKeySelective(cus);
        cdao.insert(cus);
        ce.isDistinct();
        System.out.println(cdao.countByExample(ce));

//      cdao.deleteByPrimaryKey(100);


    }

}

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="test.dao.CustomerMapper" >
  <resultMap id="BaseResultMap" type="test.model.Customer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    <id column="CONTACT_ID" property="CONTACT_ID" jdbcType="INTEGER" />
    <result column="CONTACT_EMAIL" property="CONTACT_EMAIL" jdbcType="VARCHAR" />
    <result column="CONTACT_NAME" property="CONTACT_NAME" jdbcType="VARCHAR" />
    <result column="CONTACT_PHONE" property="CONTACT_PHONE" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    CONTACT_ID, CONTACT_EMAIL, CONTACT_NAME, CONTACT_PHONE
  </sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="test.model.CustomerExample" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from contact
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    select 
    <include refid="Base_Column_List" />
    from contact
    where CONTACT_ID = #{CONTACT_ID,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    delete from contact
    where CONTACT_ID = #{CONTACT_ID,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="test.model.CustomerExample" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    delete from contact
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="test.model.Customer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    insert into contact (CONTACT_ID, CONTACT_EMAIL, CONTACT_NAME, 
      CONTACT_PHONE)
    values (#{CONTACT_ID,jdbcType=INTEGER}, #{CONTACT_EMAIL,jdbcType=VARCHAR}, #{CONTACT_NAME,jdbcType=VARCHAR}, 
      #{CONTACT_PHONE,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="test.model.Customer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    insert into contact
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="CONTACT_ID != null" >
        CONTACT_ID,
      </if>
      <if test="CONTACT_EMAIL != null" >
        CONTACT_EMAIL,
      </if>
      <if test="CONTACT_NAME != null" >
        CONTACT_NAME,
      </if>
      <if test="CONTACT_PHONE != null" >
        CONTACT_PHONE,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="CONTACT_ID != null" >
        #{CONTACT_ID,jdbcType=INTEGER},
      </if>
      <if test="CONTACT_EMAIL != null" >
        #{CONTACT_EMAIL,jdbcType=VARCHAR},
      </if>
      <if test="CONTACT_NAME != null" >
        #{CONTACT_NAME,jdbcType=VARCHAR},
      </if>
      <if test="CONTACT_PHONE != null" >
        #{CONTACT_PHONE,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="test.model.CustomerExample" resultType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    select count(*) from contact
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    update contact
    <set >
      <if test="record.CONTACT_ID != null" >
        CONTACT_ID = #{record.CONTACT_ID,jdbcType=INTEGER},
      </if>
      <if test="record.CONTACT_EMAIL != null" >
        CONTACT_EMAIL = #{record.CONTACT_EMAIL,jdbcType=VARCHAR},
      </if>
      <if test="record.CONTACT_NAME != null" >
        CONTACT_NAME = #{record.CONTACT_NAME,jdbcType=VARCHAR},
      </if>
      <if test="record.CONTACT_PHONE != null" >
        CONTACT_PHONE = #{record.CONTACT_PHONE,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    update contact
    set CONTACT_ID = #{record.CONTACT_ID,jdbcType=INTEGER},
      CONTACT_EMAIL = #{record.CONTACT_EMAIL,jdbcType=VARCHAR},
      CONTACT_NAME = #{record.CONTACT_NAME,jdbcType=VARCHAR},
      CONTACT_PHONE = #{record.CONTACT_PHONE,jdbcType=VARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="test.model.Customer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    update contact
    <set >
      <if test="CONTACT_EMAIL != null" >
        CONTACT_EMAIL = #{CONTACT_EMAIL,jdbcType=VARCHAR},
      </if>
      <if test="CONTACT_NAME != null" >
        CONTACT_NAME = #{CONTACT_NAME,jdbcType=VARCHAR},
      </if>
      <if test="CONTACT_PHONE != null" >
        CONTACT_PHONE = #{CONTACT_PHONE,jdbcType=VARCHAR},
      </if>
    </set>
    where CONTACT_ID = #{CONTACT_ID,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="test.model.Customer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Jun 17 09:23:52 CEST 2014.
    -->
    update contact
    set CONTACT_EMAIL = #{CONTACT_EMAIL,jdbcType=VARCHAR},
      CONTACT_NAME = #{CONTACT_NAME,jdbcType=VARCHAR},
      CONTACT_PHONE = #{CONTACT_PHONE,jdbcType=VARCHAR}
    where CONTACT_ID = #{CONTACT_ID,jdbcType=INTEGER}
  </update>
</mapper>

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for Customer.updateByPrimaryKeySelective
    ### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for Customer.updateByPrimaryKeySelective
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:154)
at test.dao.CustomerDAO.updateByPrimaryKeySelective(CustomerDAO.java:91)
at test.trial.main.main(main.java:27)
    Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for Customer.updateByPrimaryKeySelective
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:797)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:631)
at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:624)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:151)
... 2 more
4

1 回答 1

0

您可以通过其简单名称“updateByPrimaryKeySelective”或通过其全限定名称“nampespace”+updateByPrimaryKeySelective 来引用语句。

在您的情况下,正确的完全限定名称是 test.dao.CustomerMapper.updateByPrimaryKeySelective

于 2014-06-20T15:12:54.447 回答