mapper接口,面向接口编程的思想还是很重要的。也是本次博文最重要的部分
》Person 实体类
public class Person {
private int pid;
private String pname;
private int age;
private String address;
》Mapper接口类
package com.kgc.mybatis.pojo;
import java.util.List;
public interface PersonMapper2 {
public List<Person> getPersonList2();
}
》config.xml 配置
> 环境配置,链接数据库
> mapper配置,具体如下:
<mappers>
<!-- <mapper class="com.kgc.mybatis.pojo.PersonMapper2"/> -->
<mapper resource="com/kgc/mybatis/pojo/PersonMapper2.xml"/>
</mappers>
》mapper.xml 配置
>xml文件的namespace要写成mapper接口的路径,像下面
<mapper namespace="com.kgc.mybatis.pojo.PersonMapper2">
<select id="getPersonList2" resultMap="Person2" >
select * from t_person
</select>
</mapper>
》测试关键代码
@Test
public void getPersonList8Test(){
PersonMapper2 personMapper = session.getMapper(PersonMapper2.class);
List<Person> personList = personMapper.getPersonList2();
for (Person person : personList) {
System.out.println(person);
}
}