相比較與SSH,DAO層只需要編寫接口即可,並不需要接口的實現來對數據庫進行操作
1.接口需與mapper.xml文件在同一個目錄且同名
2.mapper文件
1 <mapper namespace="com.hero.dao.BaseDictDao"> 2 3 <select id="getBaseDictByCode" parameterType="string" resultType="BaseDict"> 4 SELECT 5 * 6 FROM 7 base_dict 8 WHERE 9 dict_type_code =#{typeCode} 10 </select> 11 12 </mapper>
namespace對應接口的權限名
在select標簽中,id 對應接口中的方法名;
parameterType 對應方法中的參數類型
resultType 對應方法返回值的類型
3.sql塊
1 <sql id="cust_list_where"> 2 <where> 3 <if test="custName !=null and custName !=''"> 4 and cust_name like '%${custName}%' 5 </if> 6 <if test="custSource !=null and custSource !=''"> 7 and cust_source=#{custSource} 8 </if> 9 <if test="custIndustory !=null and custIndustory !=''"> 10 and cust_industry=#{custIndustory} 11 </if> 12 <if test="custLevel !=null and custLevel != ''"> 13 AND cust_level = #{custLevel} 14 </if> 15 </where> 16 </sql>
對於公共的sql語句,可以抽取出來sql代碼塊,在需要的時候引入即可使用
引用sql塊:
1 <include refid="cust_list_where"></include>