mybatis入门中

一参数处理

多的话就封装成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>

三 多对多

列如查询部分是把部分下的所有员工查询出来
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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值