一参数处理
多的话就封装成Map
当一个参数传递时用#{}申明上当多个参数时用封装成map 用@Param("id");在函数上或者写一个map封装
当参数时LIST时#{list[0]}array[0]
#{}时通过预编译形式,${}取直接拼装,会有安全问题
二.//级联属性查夺表一对对
<resultMap type="emp" id="Mydifemp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.deptName"/>
</resultMap>
<select id="getEmpIdAndDept" resultMap="Mydifemp">//起别名 外键 管联
SELECT e.id id, e.last_name last_name,
e.gender gender, e.email email, e.d_id d_id,
d.id did,d.dept_name dept_name
FROM tabl_emple e,tnl_dept d
WHERE e.d_id=d.id AND e.id=1;
</select>
关联对象<association property="关联的名字" javaType=关联的类型>
<id column="did" property="id"/>//列和属性关联
<result column="dept_name" property="deptName"/>
一对多的分步查询 select后面另一个表的空间(namespace)+id" colum把其中的值传到下一个函数
<resultMap type="emp" id="myempstp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<association property="dept" select="com.mybatis.dao.DepartmentMapper.getDeptById" column="d_id">
</association>
</resultMap>
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.deptName"/>
</resultMap>
<select id="getEmpIdAndDept" resultMap="Mydifemp">//起别名 外键 管联
SELECT e.id id, e.last_name last_name,
e.gender gender, e.email email, e.d_id d_id,
d.id did,d.dept_name dept_name
FROM tabl_emple e,tnl_dept d
WHERE e.d_id=d.id AND e.id=1;
</select>
关联对象<association property="关联的名字" javaType=关联的类型>
<id column="did" property="id"/>//列和属性关联
<result column="dept_name" property="deptName"/>
一对多的分步查询 select后面另一个表的空间(namespace)+id" colum把其中的值传到下一个函数
<resultMap type="emp" id="myempstp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<association property="dept" select="com.mybatis.dao.DepartmentMapper.getDeptById" column="d_id">
</association>
</resultMap>
三 多对多
列如查询部分是把部分下的所有员工查询出来
sql语句,多表查询
select d.id did, d.dept_name dept_name,
e.id eid,e.last_name last_name, e.gender gender, e.email email
from tnl_dept d
LEFT JOIN tabl_emple e
ON d.id=e.id
WHERE d.id=1
<resultMap type="com.mytatis.po.Department" id="myempDuo1">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
<!-- collection定义关联集合查询
ofType 指定元素的对象-->
<collection property="emps" ofType="com.mytatis.po.EEmployee">
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</collection>
</resultMap>
集合的分步查询 要查询公司有多少员工,现有公司的ID 然后员工下申明lisT函数通过关联的id传值
<resultMap type="com.mytatis.po.Department" id="myempfenbu">
<id column="id" property="id"></id>
<result column="dept_name" property="deptName"/>
<collection property="emps" select="com.mybatis.dao.EmploeeMapper.getDeptById1" column="id"></collection>
</resultMap>
select id="getDeptByIdStep" resultMap="myempfenbu">
select *from tnl_dept where id =#{id}
</select>
sql语句,多表查询
select d.id did, d.dept_name dept_name,
e.id eid,e.last_name last_name, e.gender gender, e.email email
from tnl_dept d
LEFT JOIN tabl_emple e
ON d.id=e.id
WHERE d.id=1
<resultMap type="com.mytatis.po.Department" id="myempDuo1">
<id column="did" property="id"/>
<result column="dept_name" property="deptName"/>
<!-- collection定义关联集合查询
ofType 指定元素的对象-->
<collection property="emps" ofType="com.mytatis.po.EEmployee">
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</collection>
</resultMap>
集合的分步查询 要查询公司有多少员工,现有公司的ID 然后员工下申明lisT函数通过关联的id传值
<resultMap type="com.mytatis.po.Department" id="myempfenbu">
<id column="id" property="id"></id>
<result column="dept_name" property="deptName"/>
<collection property="emps" select="com.mybatis.dao.EmploeeMapper.getDeptById1" column="id"></collection>
</resultMap>
select id="getDeptByIdStep" resultMap="myempfenbu">
select *from tnl_dept where id =#{id}
</select>