SqlSessionFactory ssf = null;
@Before
public void init() {
// 1.创建会话工厂
try {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
ssf = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void findAll(){
//获取sqlsession,执行SQL
SqlSession session = ssf.openSession();
//利用接口方法
List<Map<String, Object>>list = session.selectList("doorns.findAll");
//处理结果
for (Map<?, ?> map : list) {
System.out.println(map);
}
//释放资源
session.close();
}
等价于
SqlSessionFactory ssf = null;
@Before
public void init() {
// 1.创建会话工厂
try {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
ssf = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void findAll(){
//获取sqlsession,执行SQL
SqlSession session = ssf.openSession();
//利用接口方法
List<Map<String, Object>>list = session.selectList("doorns.findAll");
//处理结果
for (Map<String, Object> map : list) {
System.out.println(map);
}
//释放资源
session.close();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="doorns">
<select id="findAll" resultType="map">
select * from tb_door
</select>
</mapper>