<!--User getUserByUsername(String username);--> <selectid="getUserByUsername"resultType="User"> <!--#{} 里面的名称不固定,因为是根据位置获取参数值,而不是参数名--> <!--select * from t_user where username = #{username}-->
<!--${} 里面的名称同样可以更改--> select * from t_user where username = '${username}' </select>
<!--User getUserByUsernameAndPassword(String username,String password);--> <!--arg0,arg1 以及 param1,param2 都可以获取参数值,注意下标,可以混着用--> <selectid="getUserByUsernameAndPassword"resultType="User"> <!--select * from t_user where username = #{arg0} and password = #{arg1}--> select * from t_user where username = '${param1}' and password = '${param2}' </select>
<!--User getUserByMap(Map<String,Object> map);--> <selectid="getUserByMap"resultType="User"> <!--select * from t_user where username = #{username} and password = #{password}--> select * from t_user where username = '${username}' and password = '${password}' </select>
<!--int addUser(User user);--> <insertid="addUser"> <!--insert into t_user values (null,#{username},#{password},#{age},#{sex},#{email})--> insert into t_user values (null,'${username}','${password}','${age}','${sex}','${email}') </insert>
<!--User getUserByParam(@Param("username") String username,@Param("password") String password);--> <selectid="getUserByParam"resultType="User"> <!--select * from t_user where username = #{username} and password = #{password}--> select * from t_user where username = '${username}' and password = '${password}' </select>
<!--User getUserById(@Param("id") Integer id);--> <selectid="getUserById"resultType="User"> select * from t_user where id = #{id} </select>
<!--Map<String,Object> getUserByIdToMap(@Param("id") Integer id);--> <selectid="getUserByIdToMap"resultType="Map"> select * from t_user where id = #{id} </select>
<!--List<User> getAllUser();--> <selectid="getAllUser"resultType="User"> select * from t_user </select>
<!--List<Map<String,Object>> getAllUserToMap();--> <selectid="getAllUserToMap"resultType="Map"> select * from t_user </select>
<!--int getCount();--> <selectid="getCount"resultType="Integer"> select count(*) from t_user </select>
<!--List<User> getUserByLike(@Param("username") String username);--> <selectid="getUserByLike"resultType="User"> <!--select * from t_user where username like '%${username}%'--> <!--select * from t_user where username like concat('%',#{username},'%')--> select * from t_user where username like "%"#{username}"%" </select>
<!--int deleteMore(@Param("ids") String ids);--> <deleteid="deleteMore"> delete from t_user where id in (${ids}) </delete>
<!--List<User> getUsersByTableName(@Param("tableName") String tableName);--> <selectid="getUsersByTableName"resultType="User"> select * from ${tableName} </select>
<!--方式一:级联解决多对一映射关系--> <resultMapid="EmpAndDeptResultMapOne"type="Emp"> <idproperty="eid"column="eid" /> <resultproperty="empName"column="emp_name" /> <resultproperty="age"column="age" /> <resultproperty="sex"column="sex" /> <resultproperty="email"column="email" /> <resultproperty="dept.did"column="did" /> <resultproperty="dept.deptName"column="dept_name" /> </resultMap> <!--Emp getEmpAndDept(int eid);--> <selectid="getEmpAndDept"resultMap="EmpAndDeptResultMapOne"> select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid} </select>
assosiation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<!--方式二:association解决多对一映射关系--> <resultMapid="EmpAndDeptResultMapTwo"type="Emp"> <idproperty="eid"column="eid" /> <resultproperty="empName"column="emp_name" /> <resultproperty="age"column="age" /> <resultproperty="sex"column="sex" /> <resultproperty="email"column="email" /> <associationproperty="dept"javaType="Dept"> <idproperty="did"column="did" /> <resultproperty="deptName"column="dept_name" /> </association> </resultMap> <!--Emp getEmpAndDept(int eid);--> <selectid="getEmpAndDept"resultMap="EmpAndDeptResultMapTwo"> select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid = #{eid} </select>
分步查询
DeptMapper.xml
1 2 3 4
<!--Dept getEmpAndDeptStepTwo(@Param("did") int did);--> <selectid="getEmpAndDeptStepTwo"resultType="Dept"> select * from t_dept where did = #{did} </select>
EmpMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!--方式三:分步解决多对一映射关系--> <resultMapid="EmpAndDeptStep"type="Emp"> <idproperty="eid"column="eid" /> <resultproperty="empName"column="emp_name" /> <resultproperty="age"column="age" /> <resultproperty="sex"column="sex" /> <resultproperty="email"column="email" /> <!--当前配置文件中设置了延迟加载,之后可以通过fetchType属性设置某个查询是否需要延迟加载,eager代表立即加载,lazy代表延迟加载--> <associationproperty="dept"select="com.cuc.mybatis.mapper.DeptMapper.getEmpAndDeptStepTwo"column="did"fetchType="lazy"/> </resultMap> <!--Emp getEmpAndDeptStepOne(@Param("eid") int eid);--> <selectid="getEmpAndDeptStepOne"resultMap="EmpAndDeptStep"> select * from t_emp where eid = #{eid} </select>
<resultMapid="DeptAndEmp"type="Dept"> <idproperty="did"column="did" /> <resultproperty="deptName"column="dept_name" /> <!-- collection:用来处理一对多映射关系 ofType:该属性所对应的集合中存储数据的类型 --> <collectionproperty="emps"ofType="Emp"> <idproperty="eid"column="eid" /> <resultproperty="empName"column="emp_name" /> <resultproperty="age"column="age" /> <resultproperty="sex"column="sex" /> <resultproperty="email"column="email" /> </collection> </resultMap> <!--Dept getDeptAndEmp(@Param("did") int did);--> <selectid="getDeptAndEmp"resultMap="DeptAndEmp"> select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did} </select>
分步查询
EmpMapper.xml
1 2 3 4
<!--List<Emp> getDeptAndEmpStepTwo(@Param("did") int did);--> <selectid="getDeptAndEmpStepTwo"resultType="Emp"> select * from t_emp where did = #{did} </select>
DeptMapper.xml
1 2 3 4 5 6 7 8 9
<resultMapid="DeptAndEmpStepOne"type="Dept"> <idproperty="did"column="did" /> <resultproperty="deptName"column="dept_name" /> <collectionproperty="emps"select="com.cuc.mybatis.mapper.EmpMapper.getDeptAndEmpStepTwo"column="did" /> </resultMap> <!--Dept getDeptAndEmpStepOne(@Param("did") int did);--> <selectid="getDeptAndEmpStepOne"resultMap="DeptAndEmpStepOne"> select * from t_dept where did = #{did} </select>
<!--List<Emp> getEmpByCondition(Emp emp);--> <selectid="getEmpByCondition"resultType="Emp"> select * from t_emp where 1=1 <iftest="empName != null and empName != ''"> and emp_name = #{empName} </if> <iftest="age != null and age != ''"> and age = #{age} </if> <iftest="sex != null and sex != ''"> and sex = #{sex} </if> <iftest="email != null and email != ''"> and email = #{email} </if> </select>
<!--List<Emp> getEmpByConditionTwo(Emp emp);--> <!-- where标签:可以将语句前面多余的 and 或 or 去掉 注意:where标签不能把后面的and or 去掉,例如emp_name = #{empName} and --> <selectid="getEmpByConditionTwo"resultType="Emp"> select * from t_emp <where> <iftest="empName != null and empName != ''"> and emp_name = #{empName} </if> <iftest="age != null and age != ''"> and age = #{age} </if> <iftest="sex != null and sex != ''"> or sex = #{sex} </if> <iftest="email != null and email != ''"> and email = #{email} </if> </where> </select>