Mybatis-day03

续上篇,理解得很表面,也不一定正确,有误希望大家可以指出。

4.解决属性名和字段名不一致的问题&&复杂查询

对于返回的结果集中存在基本类型的属性名和字段名不一致问题,我们可以通过resultMap标签进行映射;但是对于含有类或者集合等复杂类型的映射,我们需要通过另外的标签association或者collection进行数据库字段和实体类属性的映射。

情况一

<resultMap id="" type="">
	<result property="" column="">
	</result>
	...
</resultMap>

这里的property是属性的意思,也是实体类的属性;而这里的column是列的意思,也是数据库的字段。column的值需要是查询结果的字段名称。

情况二
这里涉及到多对一或者一对多的查询。当属于多对一的情况时,使用association标签将实体类中的类属性的属性与另一张表的字段对应起来。
当属于一对多的情况时,使用collection标签将实体类中的存放了类对象的集合属性和另一张表的字段对应起来。来两个例子解释[省去了设置别名、实体类的环节了,不要介意哈,实体类是学生类(id,name,tid)、老师类(id,name)]。

sql=select s.id sid,s.name sname,t.name tname from student s,teacher t where s.tid=t.id;

<select id="" resultMap="ST">
	select s.id,s.name,t.name from student s,teacher t where s.tid=t.id
</select>
<resultMap id="ST" type="Student">
	<result property="id" column="sid"></result>
	<result property="name" column="sname"></result>
	<association property="teacher" javaType="teacher">
		<result property="id" column="tid"></result>
		<result property="name" column="tname"></result>
	</association>
</resultMap>
<select id="" resultMap="TS">
	select t.id,t.name tid, s.id,s.name,from student s,teacher t where s.tid=t.id and tid=#{tid}
</select>
<resultMap id="TS" type="Teacher">
	<result property="id" column="tid"></result>
	<result property="name" column="tname"></result>
	<collection property="student" ofType="student">
		<result property="id" column="sid"></result>
		<result property="name" column="sname"></result>
		<result property="tid" column="tid"></result>
	</collection >
</resultMap>

javaType和ofType的区别是:当类型是实体类对象时使用javaType;当是取泛型时使用ofType

5.动态SQL

5.1 IF

<!- if标签相当于正常sql语句的where之后的判断语句-->
<if test="">
xxx=#{}
</if>

5.2 choose when otherwise

<!- 这三个标签类似于java中的switch语句,当遇到匹配的情况时不会继续向下执行-->
<choose>
	<when test="">
	...
	</when>
	<when test="">
	...
	</when>
	<otherwise test="">
	...
	</otherwise>
</choose>

5.3 SQL片段

<sql>
	....这里是经常重复的SQL标签语句
	注意,不可以有where标签。例
	<if test="">
	xxx=#{}
	</if>
</sql>

5.4 set where trim

<set>
	<if test="">
		xxx=#{xxx},
	</if>
	<if test="">
		xxx=#{xxx},
	</if>
</set>
<where>
	<if test="">
		xxx=#{xxx}
	</if>
	<if test="">
		and xxx=#{xxx}
	</if>
</where>

5.5 foreach

适用于某个数据是否在一个集合里
sql=“select * from student where (name =xxx or name=xxx)”

<where>
	<foreach item="nam" collection=names open="and (" seperator=" or " end=")">
	name=#{nam}
</foreach>
</where>

6. 缓存

通过缓存,可以使得数据读取直接读取缓存里的数据,而不用从数据库里查询,提高读取效率。
一级缓存会被刷新的情况:会话被关闭;存在增删改操作都有可能影响数据库的数据,所以缓存也会被刷新。

6.1 一级缓存

6.2 二级缓存

二级缓存的设置需要在核心配置文件中设置:

`<settings>
	<setting name="cacheEnabled" value="true"/></setting>
</settings>

以及在映射文件中添加标签:

<cache/>

查询的数据一般先会保存在一级缓存里,当会话关闭或者clearCache()后会把数据存放在二级缓存cache里。
使用二级缓存时,多个SqlSession使用同一个Mapper的sql语句去操作数据库,得到的数据会存在二级缓存区域。这样下一次查询时会先看二级缓存是否有数据,如果没有就会查看一级缓存有没有相应的数据,如果没有最后就会去数据库里进行查询。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值