import com.msb.mapper.EmpMapper;
import com.msb.pojo.Dept;
import com.msb.pojo.Emp;
import com.msb.pojo.Projectrecord;
import com.msb.pojo.Projects;
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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Test1 {
SqlSession sqlSession = null;
EmpMapper mapper =null;
@Before
public void test1(){
//首先做一个对象SqlSessionFactoryBuilder建立一个绘话
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
//有一个文本输入的io流进行读取操作
InputStream stream = null;
try {
//这里的路径直接会定位到配置文件classes下面;所以这个文件在次目录下--编译和
//-图纸;对数据库文件进行读取,获取一个io流,由于配置文件在classes下面,直接写文件名即可
stream = Resources.getResourceAsStream("sqlMapConfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
//build需要指向一个文件进行读取出来--工厂
SqlSessionFactory factory = ssfb.build(stream);
//需要用sqlSession去调用增删改查--工人去获取数据,打开这个绘话
sqlSession = factory.openSession();
}
/*多对多的查询*/
@Test
public void test7(){
mapper = sqlSession.getMapper(EmpMapper.class);
Projects piddle = mapper.findPiddle(1);
System.out.println("Pid");
System.out.println(piddle.getPid());
System.out.println("Pname");
System.out.println(piddle.getPname());
System.out.println("Money");
System.out.println(piddle.getMoney());
System.out.println("ProjectrecordList");
List<Projectrecord> projectrecordList = piddle.getProjectrecordList();
projectrecordList.forEach(System.out::println);
}
@After
public void test3(){
if (sqlSession!=null){
sqlSession.close();
}
}
}
|