返回主键自增的值
useGeneratedKeys=”true” keyProperty=”cid” ```xml
//方式一
insert into cloth values (default ,#{cname},#{ebrand},#{cgender})
//方式二:
order:取值AFTER|BEFORE,表示在新增之后|之前执行
2. _//参数传递之 多个对象<br />_Flower selectOne5(@Param(**"f1"**) Flower flower,@Param(**"f2"**) Flower flower2);
```xml
<!-- id 接受第一个flower对象中id name 接受第二个flower对象中name-->
<!--select * from flower where id=#{param1.id} and name=#{param2.name}-->
<!--select * from flower where id=#{arg0.id} and name=#{arg1.name}-->
<select id="selectOne5" resultType="flower">
select * from flower where id=#{f1.id} and name=#{f2.name}
</select>
_//查询单个信息 —多个参数 map.put(‘a’,1) map.put(‘b’,’西兰花’)
_Flower selectOne3(@Param(“a”) int id, @Param(“b”) String name);<select id="selectOne3" resultType="flower"> select * from flower where id=#{a} and name=#{b} </select>
延迟加载(懒加载)
延迟加载,又称按需加载。
延迟加载的内容等到真正使用时才去进行加载(查询)。多用在关联对象或集合中。
延迟加载的好处:先从单表查询、需要时再从关联表去关联查询,大大降低数据库在单位时间内的查询工作量,将工作在时间上的分配更加均匀,而且单表要比关联查询多张表速度要快。
延迟加载的设置:
第一步:全局开关:在mybatis.xml中打开延迟加载的开关。配置完成后所有的association和collection元素都生效
<settings> <setting name=”lazyLoadingEnabled” value=”true”/> <setting name=”aggressiveLazyLoading” value=”false”/> </settings> |
---|
lazyLoadingEnabled:是否开启延迟加载。是Mybatis是否启用懒加载的全局开关。当开启时,所有关联对象都会延迟加载。特定关联关系中可通过设置fetchType属性来覆盖该项的开关状态
aggressiveLazyLoading:当开启时,任何方法的调用都会懒加载对象的所有属性。否则,每个属性会按需加载,
第二步:分开关:指定的association和collection元素中配置fetchType属性。eager:表示立刻加载;lazy:表示延迟加载。将覆盖全局延迟设置。
- 一级缓存和二级缓存
一级缓存:一级存储是SqlSession上的缓存,内存上的缓存,默认开启,是一种内存型缓存,不要求实体类对象实现Serializable接口。在没有任何配置的情况下,默认一级缓存。
二级缓存:二级缓存是以namespace为标记的缓存,可能要借助磁盘,磁盘上的缓存,可以是由一个SqlSessionFactory创建的SqlSession之间共享缓存数据。默认并不开启。
1) 全局开关:在mybatis.xml文件中的
<settings> <setting name=”cacheEnabled” value=”true”/> </settings> |
---|
cacheEnabled的默认值就是true,所以这步的设置可以省略。
2) 分开关:在要开启二级缓存的mapper文件中开启缓存:
<mapper namespace=”com.bjsxt.mapper.EmployeeMapper”> <cache/> </mapper> |
---|
3) 二级缓存未必完全使用内存,有可能占用硬盘存储,缓存中存储的JavaBean对象必须实现序列化接口,
public class Employee implements Serializable { } |
---|