-----------------插入数据返回主键
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aaa.examination.dao.teacher.QuestionManageDao">
<!--namespace -->
</mapper>
resultType 返回结果集类型
parameterType 传入结果集类型
<insert id="addexam" parameterType="map">
<selectKey keyProperty="EXAM_ID" order="BEFORE" resultType="int">
select exam_exam.nextval as exam_id from DUAL
</selectKey>
insert into
</insert>
selectKey 标签代表子查询中主键提取问题
keyColumn 表示查询语句返回结果的列名
keyProperty 表示将属性设置到某个列中
order before表示在插入语句之前执行
resultType int表示返回值得类型为int类型
------------------------------------------------------redis缓存
<cache type="org.mybatis.caches.redis.RedisCache" blocking="false"
flushInterval="0" readOnly="true" size="1024" eviction="FIFO"/>
flushInterval 刷新间隔
eviction 回收策略先进先出
size 最多缓存对象数
readOnly 只读
flushInterval 自动刷新时间
-------------------------------resultMap多对多
<select id="manyToMny" resultMap="roleAndType">
select r.id as roleid,r.name as rname,p.* from tb_role r left join tb_role_power rp on r.id=rp.roleid
left join tb_power p on rp.powerid=p.id
</select>
<resultMap id="roleAndType" type="role">
<id column="roleid" property="id"></id>
<result column="rname" property="name"></result>
<collection property="powerList" ofType="power">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="url" property="url"></result>
<result column="pid" property="pid"></result>
</collection>
</resultMap>
<resultMap id="唯一的标识" type="映射的pojo对象">
<id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
<result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
<association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
<id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
<result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
</association>
<!-- 集合中的property须为oftype定义的pojo对象的属性-->
<collection property="pojo的集合属性" ofType="集合中的pojo对象">
<id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
<result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />
</collection>
</resultMap>
--------------------------------------------------多对一
<select id="manyToOne" resultMap="newsAndType">
select n.*,t.typename from TB_NEWS n,tb_newstype t where n.typeid=t.typeid
</select>
<!--高级映射 多对一-->
<resultMap id="newsAndType" type="news">
<id column="newsId" property="newsId" ></id>
<result column="title" property="title"></result>
<result column="content" property="content"></result>
<result column="addTime" property="addTime"></result>
<!--association多对一使用-->
<association property="newsType" javaType="NewsType">
<id column="typeId" property="typeId"></id>
<result column="typeName" property="typeName"></result>
</association>
</resultMap>
----------------------------------------一对多
<!--高级映射一对多-->
<select id="oneTwoMany" resultMap="deptAndEmps">
select d.deptno as dno,d.dname,d.loc,e.empno,e.ename,e.sal,e.comm,e.job from dept d left join emp e on d.deptno=e.deptno
</select>
<!--一对多的映射配置 property实体属性 javaType实体类里面的类型 jdbcType数据库中的类型 可以不写-->
<resultMap id="deptAndEmps" type="dept">
<id column="dno" property="deptNo" javaType="int" jdbcType="INTEGER"></id>
<result column="dname" property="dname"></result>
<result column="loc" property="loc"></result>
<collection property="empList" ofType="emp">
<id column="empno" property="empNo"></id>
<result column="ename" property="ename"></result>
<result column="sal" property="salary"></result>
<result column="comm" property="comm"></result>
<result column="job" property="job"></result>
</collection>
</resultMap>
--------------------------------------------------
<sql id="rel">
select deptno as deptNo,dname,loc
</sql>
<!--id是接口中的方法名称 parameterType方法的参数(有参数时用) resultType返回值类型-->
<select id="getList" resultType="com.aaa.mb.enity.Dept">
<include refid="rel"></include> from dept
</select>
<select id="getById" parameterType="int" resultType="com.aaa.mb.enity.Dept">
<include refid="rel"></include> from dept where deptno=#{deptNo}
</select>