import cn.edu.ccut.bo.Student;
import cn.edu.ccut.bo.User;
import cn.edu.ccut.mapper.StudentMapper;
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 org.junit.Test;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@SuppressWarnings("all")
public class StudentTest {
@Test
public void testLoginUser(){
SqlSession session = null;
try {
//获取sqlSessionFactory对象
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlSession对象
session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
User user = mapper.doLogin("admin", "admin");
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSelectAllStudent(){
SqlSession session = null;
try {
//获取sqlSessionFactory对象
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlSession对象
session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> students = mapper.doListAllForPage(2,2);
System.out.println(students);
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testDelAllStudent(){
SqlSession session = null;
try {
//获取sqlSessionFactory对象
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlSession对象
session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Set<String> all = new HashSet<>();
all.add("1");
all.add("2");
all.add("3");
mapper.doBantchRemove(all);
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testUpdateStudent(){
SqlSession session = null;
try {
//获取sqlSessionFactory对象
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlSession对象
session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = new Student();
student.setId("20183501");
student.setName("垃圾");
student.setAge(11);
student.setSex("男");
student.setTel("12323453454");
student.setLoc("北京");
mapper.doUpdate(student);
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
session.close();
}
}
@Test
public void testFindStudentByName() {
/* SqlSession session = null;
try {
//获取sqlSessionFactory对象
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlSession对象
session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.findByName("垃");
System.out.println(student);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
session.close();
}*/
}
@Test
public void testRemoveStudent() {
SqlSession session = null;
try {
//获取sqlSessionFactory对象
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlSession对象
session = sqlSessionFactory.openSession();
Set<String> set = new HashSet<>();
set.add("1");
set.add("2");
StudentMapper mapper = session.getMapper(StudentMapper.class);
mapper.doBantchRemove(set);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
session.close();
}
}
}