在helloworld的基础上,有几点变更:
创建一个接口
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
}
修改EmployeeMapper.xml配置文件
// namespace的值是接口的全路径名
// id的值是接口中定义的方法
<mapper namespace="com.stayreal.mybatis.EmployeeMapper">
<select id="getEmpById" resultType="com.stayreal.mybatis.Employee">
select id,last_name lastname,email,gender from tbl_employee where id = #{id}
</select>
</mapper>
junit测试
// 接口编程
@Test
public void testLoad2() throws IOException {
// 2. 获取session实例 能执行已经映射的sql语句
session = sqlSessionFactory.openSession();
EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
// mybatis会根据接口创建代理对象
Employee employee = employeeMapper.getEmpById(1);
System.out.println(employee.toString());
}