package Test; import com.cs183.Musician; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; public class Test { public static void main(String[] args) { InputStream inputStream= Test.class.getClassLoader().getResourceAsStream("config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder =new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); SqlSession sqlSession= sqlSessionFactory.openSession(); String statement ="com.cs183.mapper.MusicianMapper.save"; Musician musician=new Musician(2,"g","eason"); sqlSession.insert(statement,musician); } }以上代码为什么会报这样的错误Exception in thread "main" java.lang.NullPointerException at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:84) at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64) at Test.Test.main(Test.java:16)
时间: 2024-02-24 17:57:29 浏览: 178
AIGC musician.zip
这个错误可能是因为没有正确加载MyBatis配置文件。请检查config.xml文件是否正确放置在资源目录下,并且文件名是否拼写正确。另外,可以尝试使用绝对路径来加载配置文件,例如使用以下代码:
```
String resource = "D:/path/to/config.xml";
InputStream inputStream = new FileInputStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
```
如果你使用的是相对路径,请确保路径是相对于项目根目录。
阅读全文