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);
}