1 package com.bjpowernode;
2
3 import com.bjpowernode.dao.StudentDao;
4 import com.bjpowernode.domain.Student;
5 import com.bjpowernode.utils.MybatisUtils;
6 import org.apache.ibatis.session.SqlSession;
7 import org.junit.Test;
8
9 import java.util.List;
10
11 /**
12 * @author lihp
13 * @version 1.0
14 * @since 1.0
15 * 使用动态代理的方式sqlsession.getMapper(dao.class)获取dao接口对象,执行对应方法即可
16 * 这样做不用去写StudentDao接口的实现类就可以直接执行对应sql语句
17 * 要求就是我们sqlMapper文件要书写规范,namespace要写dao接口的全限定名称,标签id要写接口方法名
18 */
19 public class TestMybatis {
20 @Test
21 public void testSelectStudents(){
22 SqlSession sqlSession = MybatisUtils.getSqlSession();
23 StudentDao dao = sqlSession.getMapper(StudentDao.class);
24 List<Student> list = dao.selectStudents();
25 for(Student stu:list){
26 System.out.println(stu);
27 }
28 sqlSession.close();
29 }
30
31 @Test
32 public void testInsertStudent(){
33 SqlSession sqlSession = MybatisUtils.getSqlSession();
34 StudentDao dao = sqlSession.getMapper(StudentDao.class);
35 Student student = new Student();
36 student.setId(1006);
37 student.setName("王楠");
38 student.setEmail("wangnan@qq.com");
39 student.setAge(29);
40 dao.insertStudent(student);
41 sqlSession.commit();
42 sqlSession.close();
43 }
44 }