Kim VamPa

스프링 에러 : TooManyResultsException 본문

개발노트/에러

스프링 에러 : TooManyResultsException

Kim VamPa 2020. 2. 4. 13:02
728x90
반응형

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
at com.sun.proxy.$Proxy18.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:87)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:93)
at com.sun.proxy.$Proxy23.cartList(Unknown Source)
at com.sjb.controller.CartMapperTest.cartList(CartMapperTest.java:38)
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.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
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:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:80)
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:426)
... 37 more

 

상황 : 장바구니 리스트를 가져오는 쿼리문 테스트중 이와같은 에러발생

1. xml

1
2
3
4
5
6
7
8
9
10
11
12
<select id="cartList" resultType="com.sjb.model.CartListVO">
        select @rownum:=@rownum+1 as rownum, 
        sjb_cart.productId, memberId, cartStock, addDate , title, 
            authorID, (select authorName from sjb_author where sjb_book.authorID=sjb_author.authorID) as authorName, 
            publisher, cateCode, (select cateName from sjb_bcate where sjb_book.cateCode=sjb_bcate.cateCode) as cateName,
            bookCover, publeYear, likeStar, bookReply, bookPrice, discountRate, bookInfo, contents,
            introImage,regdate,boardRegdate,modDate,bookStock
        from (select @rownum:=0) as rownum, sjb_cart 
        join sjb_book
        on sjb_cart.productId = sjb_book.productID
        where sjb_cart.productId = #{productId};      
</select>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

2. mapper.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
 
public interface CartMapper {
 
 
    
    public void cartList(int productId) throws Exception;
    
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

3. test문

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
 
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
public class CartMapperTest {
    
    @Autowired
        private CartMapper cartmapper;
 
    @Test
    public void cartList() throws Exception{
        //CartListVO cart = new CartListVO();
        
        
        cartmapper.cartList(4);
        
        
        
        
    }
    
    
    
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">csz

 

 

 

원인 : 쿼리문의 결과로 나와야 되는 반환값 갯수가 맞지 않아서

해결법 : 반환값 갯수를 일치시켜준다.

=> 쿼리의 결과가 장바구니에 담긴 상품들의 리스트를 보여 줘야 하므로 VO에 담은후 반환값을 LIST<VO>로 변환

2번 mapper 문에서 void 를 List<CartListVO>로 수정

 

 

 

 

수정된 mapper문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
 
public interface CartMapper {
 
 
    
    public List<CartListVO> cartList(int productId) throws Exception;
    
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

결과물 담은 VO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
 
 
public class CartListVO {
 
    private int productId;
    
    private String memberId;
    
    private int cartStock;
    
    private String addDate;
    
    private String title;
    
    private int authorID;
    
    private String authorName;
        
    private String publisher;
    
    private String cateCode;
    
    private String cateName;
    
    private String bookCover;
    
    private List<BookCoverVO> bCover;
    
    private String publeYear;
    
    private int likeStar;
    
    private int bookReply;
    
    private int bookPrice;
    
    private int discountRate;
    
    private int sellprice;
    
    private String bookInfo;
    
    private String contents;
    
    private String introImage;
    
    private String regdate;
    
    private String boardRegdate;
    
    private String modDate;
    
    private int bookStock;
 
    public String getMemberId() {
        return memberId;
    }
 
    public void setMemberId(String memberId) {
        this.memberId = memberId;
    }
 
    public int getCartStock() {
        return cartStock;
    }
 
    public void setCartStock(int cartStock) {
        this.cartStock = cartStock;
    }
 
    public String getAddDate() {
        return addDate;
    }
 
    public void setAddDate(String addDate) {
        this.addDate = addDate;
    }
 
    public int getProductId() {
        return productId;
    }
 
    public void setProductId(int productId) {
        this.productId = productId;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public int getAuthorID() {
        return authorID;
    }
 
    public void setAuthorID(int authorID) {
        this.authorID = authorID;
    }
 
    public String getAuthorName() {
        return authorName;
    }
 
    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }
 
    public String getPublisher() {
        return publisher;
    }
 
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }
 
    public String getCateCode() {
        return cateCode;
    }
 
    public void setCateCode(String cateCode) {
        this.cateCode = cateCode;
    }
 
    public String getCateName() {
        return cateName;
    }
 
    public void setCateName(String cateName) {
        this.cateName = cateName;
    }
 
    public String getBookCover() {
        return bookCover;
    }
 
    public void setBookCover(String bookCover) {
        this.bookCover = bookCover;
    }
 
    public List<BookCoverVO> getbCover() {
        return bCover;
    }
 
    public void setbCover(List<BookCoverVO> bCover) {
        this.bCover = bCover;
    }
 
    public String getPubleYear() {
        return publeYear;
    }
 
    public void setPubleYear(String publeYear) {
        this.publeYear = publeYear;
    }
 
    public int getLikeStar() {
        return likeStar;
    }
 
    public void setLikeStar(int likeStar) {
        this.likeStar = likeStar;
    }
 
    public int getBookReply() {
        return bookReply;
    }
 
    public void setBookReply(int bookReply) {
        this.bookReply = bookReply;
    }
 
    public int getBookPrice() {
        return bookPrice;
    }
 
    public void setBookPrice(int bookPrice) {
        this.bookPrice = bookPrice;
    }
 
    public int getDiscountRate() {
        return discountRate;
    }
 
    public void setDiscountRate(int discountRate) {
        this.discountRate = discountRate;
    }
 
    public int getSellprice() {
        return sellprice;
    }
 
    public void setSellprice(int sellprice) {
        this.sellprice = sellprice;
    }
 
    public String getBookInfo() {
        return bookInfo;
    }
 
    public void setBookInfo(String bookInfo) {
        this.bookInfo = bookInfo;
    }
 
    public String getContents() {
        return contents;
    }
 
    public void setContents(String contents) {
        this.contents = contents;
    }
 
    public String getIntroImage() {
        return introImage;
    }
 
    public void setIntroImage(String introImage) {
        this.introImage = introImage;
    }
 
    public String getRegdate() {
        return regdate;
    }
 
    public void setRegdate(String regdate) {
        this.regdate = regdate;
    }
 
    public String getBoardRegdate() {
        return boardRegdate;
    }
 
    public void setBoardRegdate(String boardRegdate) {
        this.boardRegdate = boardRegdate;
    }
 
    public String getModDate() {
        return modDate;
    }
 
    public void setModDate(String modDate) {
        this.modDate = modDate;
    }
 
    public int getBookStock() {
        return bookStock;
    }
 
    public void setBookStock(int bookStock) {
        this.bookStock = bookStock;
    }
 
    @Override
    public String toString() {
        return "cartListVO [memberId=" + memberId + ", cartStock=" + cartStock + ", addDate=" + addDate + ", productId="
                + productId + ", title=" + title + ", authorID=" + authorID + ", authorName=" + authorName
                + ", publisher=" + publisher + ", cateCode=" + cateCode + ", cateName=" + cateName + ", bookCover="
                + bookCover + ", bCover=" + bCover + ", publeYear=" + publeYear + ", likeStar=" + likeStar
                + ", bookReply=" + bookReply + ", bookPrice=" + bookPrice + ", discountRate=" + discountRate
                + ", sellprice=" + sellprice + ", bookInfo=" + bookInfo + ", contents=" + contents + ", introImage="
                + introImage + ", regdate=" + regdate + ", boardRegdate=" + boardRegdate + ", modDate=" + modDate
                + ", bookStock=" + bookStock + "]";
    }
    
    
    
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

결과

 

 

 

 

 

 

728x90
반응형
Comments