本帖最后由 孤世星辰 于 2024-4-8 19:09 编辑 E:\JAVAJDK\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:E:\IDEA Community\IntelliJ IDEA Community Edition 2021.3.1\lib\idea_rt.jar=50939:E:\IDEA Community\IntelliJ IDEA Community Edition 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath "E:\IDEA Community\IntelliJ IDEA Community Edition 2021.3.1\lib\idea_rt.jar;E:\IDEA Community\IntelliJ IDEA Community Edition 2021.3.1\plugins\junit\lib\junit5-rt.jar;E:\IDEA Community\IntelliJ IDEA Community Edition 2021.3.1\plugins\junit\lib\junit-rt.jar;E:\JAVAJDK\jre\lib\charsets.jar;E:\JAVAJDK\jre\lib\deploy.jar;E:\JAVAJDK\jre\lib\ext\access-bridge-64.jar;E:\JAVAJDK\jre\lib\ext\cldrdata.jar;E:\JAVAJDK\jre\lib\ext\dnsns.jar;E:\JAVAJDK\jre\lib\ext\jaccess.jar;E:\JAVAJDK\jre\lib\ext\jfxrt.jar;E:\JAVAJDK\jre\lib\ext\localedata.jar;E:\JAVAJDK\jre\lib\ext\nashorn.jar;E:\JAVAJDK\jre\lib\ext\sunec.jar;E:\JAVAJDK\jre\lib\ext\sunjce_provider.jar;E:\JAVAJDK\jre\lib\ext\sunmscapi.jar;E:\JAVAJDK\jre\lib\ext\sunpkcs11.jar;E:\JAVAJDK\jre\lib\ext\zipfs.jar;E:\JAVAJDK\jre\lib\javaws.jar;E:\JAVAJDK\jre\lib\jce.jar;E:\JAVAJDK\jre\lib\jfr.jar;E:\JAVAJDK\jre\lib\jfxswt.jar;E:\JAVAJDK\jre\lib\jsse.jar;E:\JAVAJDK\jre\lib\management-agent.jar;E:\JAVAJDK\jre\lib\plugin.jar;E:\JAVAJDK\jre\lib\resources.jar;E:\JAVAJDK\jre\lib\rt.jar;C:\Users\a2032\Desktop\JAVA cod\mybatis_study\myabtis-1\target\test-classes;C:\Users\a2032\Desktop\JAVA cod\mybatis_study\myabtis-1\target\classes;C:\Users\a2032\.m2\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar;C:\Users\a2032\.m2\repository\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar;C:\Users\a2032\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\a2032\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.zwl.dao.UserDaoTest,test
java.lang.ExceptionInInitializerError
at com.zwl.dao.UserDaoTest.test(UserDaoTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/zwl/dao/UserMapper.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.zwl.dao.UserMapper.GetUserList. please check com/zwl/dao/UserMapper.xml and com/zwl/dao/UserMapper.java (best guess)
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at com.zwl.utils.MybatisUtils.<clinit>(MybatisUtils.java:18)
... 25 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.zwl.dao.UserMapper.GetUserList. please check com/zwl/dao/UserMapper.xml and com/zwl/dao/UserMapper.java (best guess)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 27 more
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.zwl.dao.UserMapper.GetUserList. please check com/zwl/dao/UserMapper.xml and com/zwl/dao/UserMapper.java (best guess)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:947)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:903)
at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:702)
at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:297)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parseStatement(MapperAnnotationBuilder.java:358)
at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:139)
at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72)
at org.apache.ibatis.session.Configuration.addMapper(Configuration.java:775)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.bindMapperForNamespace(XMLMapperBuilder.java:436)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:96)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:374)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
... 29 more
Process finished with exit code -1
package com.zwl.dao;
import com.zwl.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("select * from user")
List<User> getUserList();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zwl.dao.UserMapper">
<select id="getUserList" resultType="com.zwl.pojo.User">
select * from mybaits.User
</select>
</mapper>
package com.zwl.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
package com.zwl.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
//sqlSessionFactory-->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "Mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://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/mysql/mybaits?useSSL=true&useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="18870025610"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zwl/dao/UserMapper.xml"/>
</mappers>
</configuration>
package com.zwl.dao;
import com.zwl.pojo.User;
import com.zwl.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> usersist = mapper.getUserList();
for (User user : usersist) {
System.out.println(user);
}
sqlSession.close();
}
}
本帖最后由 holistic杀手 于 2024-10-1 16:42 编辑
你的错误是既写了注解的sql语句,又写了mapper的sql语句,你把@select(......)接口的这个sql删了就行
|