这里只写select,其它元素很相似。
select元素
自动映射
在Configuration中,设置了autoMappingBehavior参数决定是否自动映射,源码为:
/**
* 指定MyBatis是否以及如何自动将列映射到fields / properties。
*/
public enum AutoMappingBehavior {
/**
* 禁用自动映射
*/
NONE,
/**
* 将仅自动映射结果,而不在内部定义嵌套结果映射。
*/
PARTIAL,
/**
* 将自动映射任何复杂性的结果映射(包含嵌套或其他)
*/
FULL
}
传递多个参数
- @Param
- JavaBean
注解方式:
@Select("select * from person where id=#{id}")
Person selectPerson(@Param("id")int id);
JavaBean方式:
//Java接口
Map<String,Object> selectPersonById(Person p);
//xml文件
<select id="selectPersonById" parameterType="cn.hhm.mybatisdemo.pojo.Person" resultType="java.util.Map" >
select id,name from person where id = #{id}
</select>
resultType和resultMap
resultType:
<select id="selectPersonById" parameterType="cn.hhm.mybatisdemo.pojo.Person" resultType="java.util.Map" >
select id,name from person where id = #{id}
</select>
外部resultMap:
Person selectPersonById(Person p);
<resultMap type="cn.hhm.mybatisdemo.pojo.Person" id="personMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
</resultMap>
<select id="selectPersonById" parameterType="cn.hhm.mybatisdemo.pojo.Person" resultMap="personMap" >
select * from person where id = #{id}
</select>
级联
MyBatis中级联分为3种:
- association:一对一关系。
- collection:一对多的关系。
- discriminator:鉴别器,根据实际选择采用哪个类作为实例。
association 一对一级联
举个例子,学生(Student)和学生证(StuCard)是一对一的关系,建立2个类,Student和StuCard,StuCard为Student中的成员属性,这样就形成了一对一的级联关系。
StudentMapper.java和StudentMapper.xml
public interface StudentMapper {
Student getStudent(int sid);
}
<mapper namespace="cn.hhm.mybatisdemo.mapper.StudentMapper">
<resultMap type="cn.hhm.mybatisdemo.pojo.Student" id="studentMap">
<id property="sid" column="sid"/>
<result property="sname" column="sname"/>
<association property="stuCard" column="sid"
select="cn.hhm.mybatisdemo.mapper.StuCardMapper.getStuCard">
</association>
</resultMap>
<select id="getStudent" resultMap="studentMap" >
select * from student where sid = #{sid}
</select>
</mapper>
StuCardMapper.java和StuCardMapper.xml
public interface StuCardMapper {
StuCard getStuCard(int sid);
}
<mapper namespace="cn.hhm.mybatisdemo.mapper.StuCardMapper">
<select id="getStuCard" resultType="cn.hhm.mybatisdemo.pojo.StuCard" >
select * from stu_card where stuId = #{sid}
</select>
</mapper>
collection 一对多级联
学生和课程成绩,学生和课程成绩是一对多,课程成绩对应一门课程,一对一。
StudentMapper.xml
<mapper namespace="cn.hhm.mybatisdemo.mapper.StudentMapper">
<resultMap type="cn.hhm.mybatisdemo.pojo.Student" id="studentMap">
<id property="sid" column="sid" />
<result property="sname" column="sname" />
<!-- 一对一 -->
<association property="stuCard" column="sid"
select="cn.hhm.mybatisdemo.mapper.StuCardMapper.getStuCard"/>
<!-- 一对多 -->
<collection property="list" column="sid"
select="cn.hhm.mybatisdemo.mapper.CourseStudentMapper.getCourseStudent"/>
</resultMap>
<select id="getStudent" resultMap="studentMap">
select * from
student where sid = #{sid}
</select>
</mapper>
CourseStudentMapper.xml
<mapper namespace="cn.hhm.mybatisdemo.mapper.CourseStudentMapper">
<resultMap type="cn.hhm.mybatisdemo.pojo.CourseStudent" id="courseStudentMap">
<id property="id" column="id" />
<result property="sid" column="sid" />
<result property="cid" column="cid" />
<result property="score" column="score" />
<association property="course" column="cid"
select="cn.hhm.mybatisdemo.mapper.CoursedMapper.getStuCard"
/>
</resultMap>
<select id="getCourseStudent" resultMap="courseStudentMap">
select * from
course_student where sid = #{sid}
</select>
</mapper>
CoursedMapper.java
public interface CoursedMapper {
@Select("select * from course where cid=#{cid}")
Course getStuCard(int cid);
}
缓存 cache
默认情况下,MyBatis只开启了一级缓存(SqlSession级别的缓存)。
若开启二级缓存(SqlSessionFactory级别),MyBatis要求返回的POJO必须序列化。
<cache eviction="LRU" flushInterval="10000" size="1024" readOnly="true"/>
属性:
eviction:代表的是缓存回收策略。
(1)LRU:最近最少使用,删除最长时间不用的对象;
(2)FIFO:先进先出;
(3)SOFT:软引用;
(4)WEAK:弱引用。
flushInterval:刷新间隔时间。
size:缓存最多可缓存对象数量。
readOnly:只读。