初学者第一次调试有如 下异常 不理解 ,望讲解,用xml配置可以实现

robinzyx 发布于 2012/05/05 12:19
阅读 14K+
收藏 0
<?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>
<properties resource="com/ibatis/SqlMap.properties"></properties>
 
<typeAliases >
<typeAlias  alias = "Student" type="com.ibatis.Student"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/ibatis/Student.xml"/>
</mappers>
</configuration>
—————————————————————————————————————————————
package com.ibatis;

import org.apache.ibatis.annotations.Select;

public interface IsStudent {
 @Select("SELECT * FROM student WHERE id = #{id}")
  Student selectStudent(int id);

}
——————————————————————————————————-——————————
package com.ibatis;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class TestStudent {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
String resource = "com/ibatis/configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
try{
IsStudent isstudent = session.getMapper(IsStudent.class);
Student student=isstudent.selectStudent(1);
System.out.print("student name is" +student.getName());
}finally{
session.close();
}

}

}

—————————————————————————————————————————————
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/ibatis/IsStudent.java (best guess)
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.ibatis.IsStudent.selectStudent
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:35)
at com.ibatis.TestStudent.main(TestStudent.java:20)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.ibatis.IsStudent.selectStudent
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:105)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:88)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:49)
... 2 more
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.ibatis.IsStudent.selectStudent
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:657)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:629)
at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:462)
at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:296)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parseStatement(MapperAnnotationBuilder.java:272)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:119)
at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:60)
at org.apache.ibatis.session.Configuration.addMapper(Configuration.java:532)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.bindMapperForNamespace(XMLMapperBuilder.java:365)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:90)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:311)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:103)
... 4 more

加载中
1
n
number_two
用了注解,应该就不需要<mapper resource="com/ibatis/Student.xml"/>这个了
r
robinzyx
谢谢你的解答,我自己摸索到了,现在搞清楚了,你算是唯一一个说道点子上的。谢谢。。
0
鉴客
鉴客
The error may exist in com/ibatis/IsStudent.java (best guess)
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper
 
配置有误,在 IsStudent 这块
0
一号男嘉宾
一号男嘉宾
初学者你还玩 ibatis
r
robinzyx
那个东东不是从不知道,不会开始的了。。。
一号男嘉宾
一号男嘉宾
回复 @伤离别丶 : http://www.oschina.net/question/12_52027
被风遗忘
被风遗忘
回复 @伤离别丶 : 初学应该是先弄懂JDBC的一些基本原理和来龙去脉.像ibatis,hibernate都是在Jdbc封装之上,弄得基础的,自然框架会了解透彻一些.
军区文工团
军区文工团
初学者玩什么?
0
梦想编织者
中间还用到了 IBatis的注解~  NB  还是全部用xml配置吧。 高人啊,我只会xml配置。
r
robinzyx
应为是学习使用,所以既然他有两种方案,那就都试试了。。
返回顶部
顶部