实体类:
package tk.mybatis.simple.model; public class Country { public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getCountryname() { return countryname; } public void setCountryname(String countryname) { this.countryname = countryname; } public String getCountrycode() { return countrycode; } public void setCountrycode(String countrycode) { this.countrycode = countrycode; } private Long id; private String countryname; private String countrycode; }
Mapper.xml文件:
<?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>XML的根文件,属性namespace定义了当前XML的命名空间 --> <mapper namespace="tk.mybatis.simple.mapper.CountryMapper"> <!-- <select>元素,我们所定义的一个SELECT查询 id属性:定义了当前查询的唯一一个id(这个id需要与实体类中的方法名对应么?) resultType:定义了当前查询的返回值类型, --> <select id="selectAll" resultType="Country"> SELECT id,countryname,countryCode FROM country </select> </mapper>