public class MyBatisUtils {
private static SqlSessionFactory factory = null;
static {
try {
String config = "mybatis.xml";
InputStream in = Resources.getResourceAsStream(config);
factory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession sqlSession = null;
if (factory!=null){
sqlSession = factory.openSession();
}
return sqlSession;
}
}
运行类
public class TestMybatis {
@Test
public void testSelectStudentIf() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
/* Student student = new Student();
student.setName("李四");
student.setAge(20);
List<Student> students = dao.selectStudentIf(student);
for (Student stu:students){
System.out.println("if==="+stu);
}*/
}