1. 程式人生 > >MyBatis對應xml對映檔案l配置例子

MyBatis對應xml對映檔案l配置例子

<?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,namespace的值習慣上設定成包名+sql對映檔名,這樣就能夠保證namespace的值是唯一的
	例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml檔案去除字尾)
-->
<mapper namespace="me.gacl.mapping.userMapper">
    <!-- 
    	在select標籤中編寫查詢的SQL語句, 設定select標籤的id屬性為getUser,
    	id屬性值必須是唯一的,不能夠重複
  	 	 使用parameterType屬性指明查詢時使用的引數型別,resultType屬性指明查詢返回的結果集型別
    	resultType="me.gacl.domain.User"就表示將查詢結果封裝成一個User類的物件返回
    	User類就是users表所對應的實體類
    -->
    <!-- 
        根據id查詢得到一個user物件
     -->
    <select id="getUser" parameterType="int" 
        resultType="me.gacl.domain.User">
        select * from users where id=#{id}
    </select>
</mapper>