🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ package com.test; import com.nobb.domain.User; import com.nobb.mapper.UserMapper; 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; import java.util.List; public class Test { @org.junit.Test public void testFindAll() { //定义配置文件路径 String resource = "mybatis-config.xml";//不能加 根目录 / InputStream inputStream = null; try {//加载配置文件 inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); }//根据配置文件创建SessionFactory对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获得SqlSession SqlSession session = sqlSessionFactory.openSession(); //获得UserMapper接口的实现类 UserMapper mapper = session.getMapper(UserMapper.class); //调用UserMapper方法,执行xml中定义的SQL语句 List<User> list = mapper.findAll(); //打印list System.out.println(list); } } ~~~