一对多联查
<resultMapid="userResult"type="User"autoMapping="true">
<idcolumn="id"property="id"/>
<resultproperty="nickname"column="nickname"/>
<resultproperty="schoolName"column="school_name"/>
<collectionproperty="addressList"column="phone"ofType="Address"
autoMapping="true">
<idcolumn="address_id"property="id"/>
</collection>
</resultMap>
<selectid="selectById"parameterType="int"resultMap="userResult">
selecttu.id,
username,
password,
nickname,
ta.address_id,
addr,
phone,
postcode,
user_id
fromt_usertu
left join t_addresstaontu.id=ta.user_id
wheretu.id=#{id}
</select>
autoMapping代表自动封装,如果不填写,则需要添加所有的对应关系。
这里需要配置ofType来指定返回值类型
事务
事务四大特性:
1、原子性:事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行。
2、一致性:几个并行执行的事务,其执行结果必须与按某一顺序串行执行的结果相一致。
3、隔离性:事务的执行不受其他事务的干扰,事务执行的中间结果对其他事务必须是透明的。
4、持久性:对于任意已提交事务,系统必须保证该事务对数据库的改变不被丢失,即使数据库出现故障。
事务的三大问题:
1、脏读
2、不可重复读
3、幻读
在test测试类的@before可以设置自动提交事务
//设置为自动提交
sqlSession=MybatisUtils.openSession(true);