2 yaoyy09 yaoyy09 于 2016.01.21 11:21 提问

Spring3整合Mybatis3+mybatis-spring1.x查询报错

我在进行Spring(V:3.2.3.RELEASE)+mybatis(V:3.2.4)+mybatis-spring(V:1.1.1)整合。有一个ClassRoom班级类:
//班级id
private int classId;
//班级编码
private String classCode;
//班级名字
private String className;
//所属学院
private College college;
//标记
private int flag;
学院类College属性是:
private int collegeId;
private String collegeCode;
private String collegeName;
private int flag;

ClassRoomMapper.xml配置如下:





<!-- -->






查询班级信息语句如下:

select t.class_id, t.class_code,t.class_name, t.flag as 'class_flag',t2.college_code, t2.college_id, t2.college_name, t2.flag as 'college_flag' from t_class t left join t_college t2 on t.college =t2.college_code where t.class_code = #{classCode}

beans.xml中配置ClassDao如下:



在进行查询(loadByCode) 的时候报错如下:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classRoomDao'
defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sqlSessionFactory'
while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init
method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource
[mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.
Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalStateException:
Type handler was null on parameter mapping for property 'college'. It was either not specified and/or could not be found
for the javaType / jdbcType combination specified.

请问这是什么原因呢?还需要重新写college与数据库字段对应的关系吗?我已经在association中增加了对应字段的配置。

9个回答

yaoyy09
yaoyy09   2016.01.21 11:25

刚才看配置的xml文件不显示,重新补充下。
ClassRoomMapper.xml配置如下:





<!-- -->







---------------------------------------------------------------------
beans.xml中配置ClassDao如下:



yaoyy09
yaoyy09   2016.01.21 11:26

ClassRoomMapper.xml配置如下:





<!-- -->






beans.xml中配置ClassDao如下:



missMeyo
missMeyo   2016.01.21 11:31

331221223


missMeyo
missMeyo   2016.01.21 11:31
 321311
yaoyy09
yaoyy09   2016.01.21 11:32

ClassRoomMapper.xml配置如下:

 <resultMap type="com.yao.model.ClassRoom" id="ClassRoomMap">
        <id column="class_id" property="classId"/>
        <result column="class_code" property="classCode"/>
        <result column="class_name" property="className"/>
        <result column="class_flag" property="flag"/>
        <!-- <association property="college" javaType="com.yao.model.College" column="college" select="com.yao.dao.CollegeDao.loadByCode" /> -->
        <association property="college" javaType="com.yao.model.College">
            <id column="college_id" property="collegeId"/>
            <result column="college_code" property="collegeCode"/>
            <result column="college_name" property="collegeName"/>
            <result column="college_flag" property="flag"/>
        </association>
    </resultMap>

    beans.xml中配置ClassDao如下:
    <bean id="classRoomDao" class="org.mybatis.spring.mapper.MapperScannerConfigurer" scope="prototype">
        <property name="mapperInterface" value="com.yao.dao.ClassRoomDao"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>


    查询班级信息语句如下:
    <select id="loadByCode" resultMap="ClassRoomMap">
    select t.class_id, t.class_code,t.class_name, t.flag as 'class_flag',t2.college_code, t2.college_id, t2.college_name, t2.flag as 'college_flag' from t_class t left join t_college t2 on t.college =t2.college_code where t.class_code = #{classCode}
    </select>
rui888
rui888   Ds   Rxr 2016.01.21 13:20

Type handler was null on parameter mapping fo**r property 'college'. **I t was either not specified and/or could not be found
for the javaType / jdbcType combination specified.

全局搜搜 college

yaoyy09
yaoyy09   2016.01.21 16:43

college是一个对象,根本原因找到了,在这里写下:
在ClassRoom.xml文件配置中,使用insert和update语句的时候,直接使用了属性college,而不是获取college中的额collegeCode导致college错误。
将原来的#{college}修改为#{college.collegeCode}。

 <insert id="add" parameterType="com.yao.model.ClassRoom">
        insert into t_class (class_code ,class_name , college,flag)
#       values (#{classCode},#{className},#{college.collegeCode},#{flag})
    </insert>
BestwishesTolover
BestwishesTolover   2016.01.21 18:04

现在spring都是注解,配置文件太麻烦了

Evankaka
Evankaka   Ds   Rxr 2016.01.21 22:31

mybatis-config.xml这个文件在报错。,XML解析失败
Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalStateException:
出现这种情况,一般都是XML格式不对,仔细检查下各种符号

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!