@Param注解
1,使用@Param注解
当以下面的方式进行写SQL语句时:
@Select("select column from table where userid = #{userid} ")
public int selectColumn(int userid);
当你使用了使用@Param注解来声明参数时,如果使用 #{} 或 ${} 的方式都可以。
@Select("select column from table where userid = ${userid} ")
public int selectColumn(@Param("userid") int userid);
当你不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 ${} 的方式,会报错。
@Select("select column from table where userid = ${userid} ")
public int selectColumn(@Param("userid") int userid);
2,不使用@Param注解
不使用@Param注解时,参数只能有一个,并且是Javabean。在SQL语句里可以引用JavaBean的属性,而且只能引用JavaBean的属性。
// 这里id是user的属性
@Select("SELECT * from Table where id = ${id}")
Enchashment selectUserById(User user);
======================================================================
mybatis中的resultMap
集合查询 (两条SQL语句)
注意:SQL的查询结果最终映射的是接口的返回值类型 所以 id 处要有对应的返回值类型的抽象方法
<resultMap type="Employee" id="MyaMap1"> <id column="employee_id" property="employee_id" /> <result column="last_name" property="last_name"/> <result column="first_name" property="first_name"/> <result column="email" property="email"/> <result column="phone_number" property="phone_number"/> <result column="job_id" property="job_id"/> <result column="salary" property="salary"/> <result column="commission_pct" property="commission_pct"/> <result column="manager_id" property="manager_id"/> <result column="department_id" property="department_id"/> <result column="hiredate" property="hiredate"/> </resultMap> <select id="getListEmployee" resultMap="MyaMap1"> select * from employees where department_id = #{id} </select> <!-- 集合查询 --> <resultMap type="Departments" id="listEmp"> <id column="department_id" property="department_id"/> <result column="department_name" property="department_name"/> <result column="manager_id" property="manager_id"/> <result column="location_id" property="location_id"/> <collection property="employees" ofType="Employee" select="getListEmployee" column="{id=department_id}"> </collection> </resultMap> <select id="findDepartmentEmployee" resultMap="listEmp"> select * from departments where department_id = #{id} </select>
对象属性查询 (一条sql语句)
<resultMap type="employee" id="myMap"> <id column="employee_id" property="employee_id" /> <result column="last_name" property="last_name"/> <result column="first_name" property="first_name"/> <result column="email" property="email"/> <result column="phone_number" property="phone_number"/> <result column="job_id" property="job_id"/> <result column="salary" property="salary"/> <result column="commission_pct" property="commission_pct"/> <result column="manager_id" property="manager_id"/> <result column="department_id" property="department_id"/> <result column="hiredate" property="hiredate"/> <association property="departments" javaType="Departments" > <id column="department_id" property="department_id"/> <result column="department_name" property="department_name"/> <result column="manager_id" property="manager_id"/> <result column="location_id" property="location_id"/> </association> </resultMap> <select id="getEmployeeAndDepartment" resultMap="myMap"> select e.employee_id,e.last_name,e.first_name,e.email,e.phone_number,e.job_id, e.salary,e.commission_pct,e.manager_id,e.department_id,e.hiredate, d.department_id,d.department_name,d.manager_id,d.location_id from employees e, departments d where e.department_id= d.department_id </select>