日期:2014-05-20  浏览次数:21162 次

MyBatis的parameterMap问题
我刚接触Mybatis,自己写了个例子测试,因为数据库字段跟实体类字段不一样,所以用了parameterMap和resultMap,但是在insert的时候,数据没有插入
配置文件如下

XML code

<resultMap id="studentResultMap" type="student">
        <id property="id" column="F_ID" />
        <result property="name" column="F_NAME" />
    </resultMap>
    
    <parameterMap id="studentParameterMap" type="student">
        <parameter property="id" resultMap="F_ID"/>
        <parameter property="name" resultMap="F_NAME"/>
    </parameterMap>
    
    <select id="selectAllStudent" resultMap="studentResultMap">
        <![CDATA[
            select * from A_TEMP_STUDENT
        ]]>
    </select>
    
    <insert id="insertStudent" parameterMap="studentParameterMap">
        <![CDATA[
            insert into A_TEMP_STUDENT (f_id, f_name) values (#{id}, #{name})
        ]]>
    </insert>



java代码:

Java code

/* (non-Javadoc)
     * @see com.lmiky.database.BaseDAO#list()
     */
    @SuppressWarnings("unchecked")
    public List<T> list() {
        SqlSession session = openSession(getSqlMapper());
        List<T> students = session.selectList("com.lmiky.student.selectAllStudent");
        closeSession(session);
        return students;
    }
    
    /* (non-Javadoc)
     * @see com.lmiky.database.BaseDAO#add(com.lmiky.database.pojo.BasePojo)
     */
    public T add(T t) {
        SqlSession session = openSession(getSqlMapper());
        session.insert("insertStudent", t);
        closeSession(session);
        return t;
    }
    
    public static void main(String[] args) {
        StudentDAO<Student> studentDAO = new StudentDAOImpl<Student>();
        
        //添加
        Student student = new Student();
        student.setId(101l);
        student.setName("test101");
        studentDAO.add(student);
        
        //查询
        List<Student> students = new StudentDAOImpl<Student>().list();
        for(Student s : students) {
            System.out.println(s);
        }
        
    }



查询没问题,就是插入数据插不进去,也没报错,Mybatis官方文档实在是介绍太简单了,竟然也没提到parameterMap。
google上输入mybatis,查询出来的都是ibatis,是不是现在没什么人用mybatis,都还在用ibatis


------解决方案--------------------
insert into A_TEMP_STUDENT (f_id, f_name) values (?, ?)
或者是 <parameter property="id" resultMap="F_ID"/>加上jdbcType看看