packageenity;importorg.junit.Test;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importjava.io.IOException;importjava.io.InputStream;importjava.util.List;public classStudentMybatis {
/按id查找, 第一个有注释 其他的省略未写
@Testpublic voidfindStudentId()throwsIOException {
//指定全局文件String resource ="mybatis-config.xml";//读取配置文件InputStream inputStream = Resources.getResourceAsStream(resource);//构建SqlSessionFactorySqlSessionFactory sqlSessionFactory =newSqlSessionFactoryBuilder().build(inputStream);//获取sqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();//创建一个statement值内容为StudentMapper.xml里的namespace +需要调用的idString statement ="mappers.StudentMapper.findStudentId";//statement可以用上方的"xx"内容替代 不用再创建statementStudent student = sqlSession.selectOne(statement,3);System.out.println(student);//关闭sqlsessionsqlSession.close();}
//查找全部@Testpublic voidselectAllStudent()throwsIOException {
String resource ="mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory =newSqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession();String statement ="mappers.StudentMapper.selectAllStudent";List students = sqlSession.selectList(statement);System.out.println(students);sqlSession.close();
}
//删除@Testpublic voiddeleteStudent()throwsIOException {
String resource ="mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory =newSqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession();String statement ="mappers.StudentMapper.deleteStudent";Student student = sqlSession.delectOne(statement,12);System.out.println(student);sqlSession.commit();sqlSession.close();
}
//更改
@Testpublic voidupdateStudent()throwsIOException {
String resource ="mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory =newSqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession();String statement ="mappers.StudentMapper.updateStudent";Student student =newStudent();student.setName("王望");student.setType("后端");student.setId(22);inti = sqlSession.update(statement,student);if(i >0) {
System.out.println(true);}else{
System.out.println(false);}
sqlSession.commit();sqlSession.close();
}
//插入
@Testpublic voidinsertStudent()throwsIOException {
String resource ="mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory =newSqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession();String statement ="mappers.StudentMapper.insertStudent";Student student =newStudent();student.setId(13);student.setName("王丽丽");student.setqq(15849996);// insert的()里,需要路径和student的具体的值sqlSession.insert(statement,student);System.out.println(student.getId());sqlSession.commit();sqlSession.close();}
}