import com.sun.source.tree.UsesTree;
import entity.Student;
import 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.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestStudent {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
// mybatis 配置文件的文件名
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindAll() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findAll();
list.forEach((e)->System.out.println(e));
}
@Test
public void testFindById(){
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.findById(2);
System.out.println(student);
}
@Test
public void testFindBy() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findBy("王凯",7);
list.forEach((e)->System.out.println(e));
}
@Test
public void testFind() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setPhone("13852615963");
stu.setSname("王凯");
stu.setGid(7);
List<Student> list = mapper.find(stu);
list.forEach((e)->System.out.println(e));
}
@Test
public void testFindByName() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findByName("凯");
list.forEach((e)->System.out.println(e));
}
@Test
public void testFindByName2() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findByName("%"+"凯"+"%");
list.forEach((e)->System.out.println(e));
}
@Test
public void testQuery() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.query("","");
list.forEach((e)->System.out.println(e));
}
@Test
public void testUpdate() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
//将sid=2的学生的电话改为13920840987, 地址为 北京市海淀区
Student stu = new Student();
stu.setSid(2);
stu.setPhone("");
stu.setAddress("上海市浦东区");
mapper.update(stu);
System.out.println("修改成功");
session.commit();
List<Student> list = mapper.query("","");
list.forEach((e)->System.out.println(e));
}
@Test
public void testDel() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
//
int[] nums = {9,6,7,10,8};
mapper.delByIds(nums);
session.commit();
List<Student> list = mapper.query("","");
list.forEach((e)->System.out.println(e));
}
@Test
public void test2() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.queryWithGrade();
list.forEach((e)->System.out.println(e));
}
@Test
public void test2_1() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.queryWithGrade2();
list.forEach((e)->System.out.println(e));
}
}