您的位置:首页 > 数据库

MyBatis 映射SQL的几种方式

2017-05-15 21:58 253 查看
1配置文件方式:

ex:

1.1 SQL映射文件

<mapper namespace="com.test.dao.EmployeeMapper">
<select id="getEmpById" resultType="com.test.beans.Employee">
select id,last_name lastName,email,gender from tb1_emplyee where id = #{id}
</select>
</mapper>

1.2全局配置文件:
使用Mapper标签进行绑定:

<mappers>
<mapper resource="EmployeeMapper.xml" />

</mappers>2.注解方式:
定义一个接口:

public interface EmployeeMapperAnnotation {
@Select("select id,last_name lastName,email,gender from tb1_emplyee where id = #{id}")
public Employee getEmpById(Integer id);
}
然后在全局配置文件中进行注册:
<mappers>

<mapper class="com.test.dao.EmployeeMapperAnnotation"></mapper>
</mappers>3.批量注册
<mappers>

<package name="com.atguigu.mybatis.dao"/>
</mappers>

tips:
映射文件名必须和接口同名,并且放在与接口同一目录下,不然注册不了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: