<?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="cn.fy.domain.UserMapper">
<select id="findUserById" parameterType="int" resultType="cn.fy.domain.User">
SELECT id,username,birthday,sex,address FROM USER WHERE id = #{id}
</select>
</mapper>
/**
* 用户管理mapper
*/
Public interface UserMapper {
//方法名符合第二条规范, 参数符合第三条规范,返回值类型符合第四条规范
public User findUserById(int id) throws Exception;
}
<mappers>
<mapper resource="UserMapper.xml"/> // resource根目录
</mappers>
@Test
public void Mapper() {
SqlSession session = MyBatisUtil.getSession();
//获取mapper接口的代理对象
UserMapper userMapper = session.getMapper(UserMapper.class);
try {
userMapper.findUserById(1);
} catch (Exception e) {
e.printStackTrace();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |