MyBatis複習(十):註解開發

MyBatis有兩種開發方式:XML開發和註解開發


註解與XML對應表格

註解 對應XML 說明
@Insert <insert> 新增SQL
@Update <update> 更新SQL
@Delete <delete> 刪除SQL
@Select <select> 查詢SQL
@Param - - - 參數映射
@Results <resultMap> 結果映射
@Result <id> <result> 字段映射

XML開發

@Repository
public interface UserDao {

	/**
	 * 有多個參數時,需要使用@Param註解進行綁定關聯
	 */
    List<User> selectBySexAndAge(@Param("sex")String sex , @Param("age")Integer age , @Param("limit")Integer limit);

}
<?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.cd.blog.dao.UserDao">

    <select id="selectBySexAndAge" resultType="User">
        select * from t_user where sex = #{sex} and age = #{age} order by age
    </select>
    
</mapper>

註解開發

@Repository
public interface UserDao {

	/**
	 * 有多個參數時,需要使用@Param註解進行綁定關聯
	 */
    @Select("select * from t_user where sex = #{sex} and age = #{age} order by age limit 0 , #{limit}")
    List<User> selectBySexAndAge(@Param("sex")String sex , @Param("age")Integer age , @Param("limit")Integer limit);

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章