mybatis最终加载的是编译后target目录下面的配置文件,若此目录下没有执行会报错:
https://blog.csdn.net/Doctor_LY/article/details/83000745
创建模版的步骤:
创建模版文件:
创建文件选择使用的模版:
public class SqlSessionUtils {
private static SqlSessionFactory sessionFactory = null;
// 初始化sqlsessionFactory
static {
String config = "mybatis.xml";
try {
InputStream resourceAsStream = Resources.getResourceAsStream(config);
sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建方法,获取SqlSession对象
public static SqlSession getSqlSession() {
SqlSession sqlSession = null;
if (null != sessionFactory) {
sqlSession = sessionFactory.openSession();
}
return sqlSession;
}
}
使用帮助类:
/** * 工具类批量查询 */
@Test
public void testSelectStudentsByUtils() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
String sqlId = "com.macay.dao.StudentDao"+"."+"selectStudents";
List<Student> students = sqlSession.selectList(sqlId);
for (Student student : students) {
System.out.println(student);
}
sqlSession.close();
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_44075963/article/details/116373524
内容来源于网络,如有侵权,请联系作者删除!