0 0

mybatis注解调用报错5

package com.cdg.iservice;

import org.apache.ibatis.annotations.Select;

import com.cdg.bean.Person;

public interface IPersonMapper {
	
	@Select("select * from Person where pid = #{pid}")
	Person queryPersonById(Integer pid);
}


package com.cdg.iservice.impl;

import org.apache.ibatis.session.SqlSession;

import com.cdg.bean.Person;
import com.cdg.iservice.IPersonMapper;
import com.cdg.utils.UtilSqlSessionFactory;

public class PersonMapperManager implements IPersonMapper {

	public Person queryPersonById(Integer pid) {
		SqlSession session = UtilSqlSessionFactory.getSqlSessionFactory().openSession();
		try {
			IPersonMapper ipm = session.getMapper(IPersonMapper.class);
			Person p = ipm.queryPersonById(pid);
			System.out.println(p.getPname());
			return p;
		} finally {
			session.close();
		}
	}
	
	public static void main(String[] args) {
		IPersonMapper ipm = new PersonMapperManager();
		Person p = ipm.queryPersonById(1);
		System.out.println(p.getPname());
	}

}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url"
					value="jdbc:mysql://localhost:3306/test" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/cdg/bean/Person.xml" />
	</mappers>
</configuration>


Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.cdg.iservice.IPersonMapper is not known to the MapperRegistry.
	at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:21)
	at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:375)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:164)
	at com.cdg.iservice.impl.PersonMapperManager.queryPersonById(PersonMapperManager.java:14)
	at com.cdg.iservice.impl.PersonMapperManager.main(PersonMapperManager.java:25)


这个错误怎么回事呢
请大侠们帮助

2010年10月09日 16:55

1个答案 按时间排序 按投票排序

0 0

我也遇到了这个问题,后来发现时要在mapper.xml文件中将resource指定成你Mapper接口。

<mapper namespace="com.cdg.bean.Person.IPersonMapper">

2013年4月02日 10:26

相关推荐

Global site tag (gtag.js) - Google Analytics