Mybatis引入映射器的方法
映射器是mybatis最复杂、最核心的
映射器定义命名空间(namespace)的方法,命名空间对应的是一个接口的全路径,而不是实现类。
pojo(Plain Ordinary Java Object)->普通Java对象
package com.szd.com.szd.mybatis.pojo;
public class Role implements java.io.Serializable {
private Long id;
private String roleName;
private String note;
/** setter and getter **/
}
首先定义接口
package com.szd.mybatis.mapper;
**...**
public interface RoleMapper {
public Role getRole(Long id);
}
其次,给出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 namespace="com.szd.mybatis.mapper.RoleMapper">
<!-- selectOneStudent -->
<select id="getRole" parameterType="long" resultType="com.szd.mybatis.pojo.Role">
select * from t_role where id = #{id}
</select>
</mapper>
1.用文件路径引入映射器
<mappers>
<mapper resource="com/szd/mybatis/mapper/RoleMapper.xml"/>
</mappers>
2.用包名引入映射器
<mappers>
<package name="com.szd.mybatis.mapper"/>
</mappers>
3.用类注册引入映射器
<mappers>
<mapper class="com.szd.mybatis.RoleMapper"/>
<mapper class="..."/>
...
</mappers>
4.用URL引入映射器
<mappers>
<mapper url="file:///var/mappers/com/szd/mybatis/mapper/RoleMapper.xml" />
<mapper url="..." />
...
</mappers>