seungje89
142
2017-07-17 17:43:06.0 작성 2017-07-17 17:44:00.0 수정됨
7
178

spring mybats 에러를 도저히 못잡겟어요 ㅠ


마이바티스 에러는 확인했습니다. 

테스트는 mapper에 select now(); 쿼리를 던져서 db에서 시간만 가져오는 것입니다. 


1. 인터페이스 구현

package com.seungje.persistence;

public interface PersonDAO {

public String getTime();

public void insertPerson(PersonVO vo);

}



2. mapper 구현
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.seungje.mapper.PersonMapper.xml">
<select id="getTime" resultType="string">
select now()
</select>
</mapper>

3. root-context 수정
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"></property>
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>


4. 구현 클래스

package com.seungje.persistence;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSession;

import org.springframework.stereotype.Repository;

@Repository

public class PersonDAOIm implements PersonDAO{

@Inject

private SqlSession sqlSession;

private static final String namespace = "com.seungje.mapper.PersonMapper";

@Override

public String getTime() {

return sqlSession.selectOne(namespace+".getTime");

}

@Override

public void insertPerson(PersonVO vo) {

// TODO Auto-generated method stub

}

}


5. 빈 추가

<context:component-scan base-package="com.seungje.persistence">

</context:component-scan>


6. 테스트

package com.seungje.mybatis;


import javax.inject.Inject;


import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import com.seungje.persistence.PersonDAO;


@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/root-context.xml" })

public class PersonDAOTest {


@Inject

private PersonDAO dao;

@Test

public void testTime()throws Exception{

System.out.println(dao.getTime());

}

 

}



시간도 못 불러오는 상황이라 insert는 꿈도 못꾸고 있습니다 (__).... 부탁드려요 
namespace도 맞고 mapper id도 맞고 .. 뭐가 문제일까요 ?ㅠ 
0
0
  • 답변 7

  • onimusha
    3k
    2017-07-17 17:48:15.0

    now() 결과가 string 으로 받아지던가요?

    0
  • seungje89
    142
    2017-07-17 17:50:34.0

    교재 내용을 따라하고 있는 것인데 시간값을 그냥 string으로 뿌리는 거 같아요 

    0
  • madlife
    139
    2017-07-17 17:51:15.0

    오류도 보여주세요

    0
  • seungje89
    142
    2017-07-17 17:52:27.0

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 

    ### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.seungje.mapper.PersonMapper.getTime

    ### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.seungje.mapper.PersonMapper.getTime

    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)

    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)

    at com.sun.proxy.$Proxy15.selectOne(Unknown Source)

    at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:158)

    at com.seungje.persistence.PersonDAOIm.getTime(PersonDAOIm.java:18)

    at com.seungje.mybatis.PersonDAOTest.testTime(PersonDAOTest.java:21)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)

    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)

    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)

    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

    Caused by: org.apache.ibatis.exceptions.PersistenceException: 

    ### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.seungje.mapper.PersonMapper.getTime

    ### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.seungje.mapper.PersonMapper.getTime

    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:71)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)

    ... 33 more

    Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.seungje.mapper.PersonMapper.getTime

    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:875)

    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:708)

    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:701)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)

    ... 41 more


    0
  • seungje89
    142
    2017-07-17 17:53:54.0

    datasource 테스트와 mybatis sqlFactory, sqlSession 객체 얻는 테스트도 잘 됩니다 

    0
  • ktse
    594
    2017-07-17 20:22:24.0

    <mapper namespace="com.seungje.mapper.PersonMapper.xml">



    여기에 .xml 빼고 해보셨나여

    이거 아니면  
    PersistenceException

    에러 구글검색해보시면 확인해야할 사항

    5가지정도 나와있는데 그대로 맞춰보세요

    1
  • seungje89
    142
    2017-07-18 10:14:04.0

    ㅜㅜ .xml 빼고 했는데도 안됩니다 ㅠ

    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.