<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.hrf.mapper.KingMapper"><!-- 维护列和属性值的映射关系,type="king"最终返回的结果类型,java类型--><resultMap id="kingMap" type="king"><!-- 主键字段property="id"类中的属性名字,
列 column="id"自己写的sql语句的列名(表中的列名)或者是列的别名,
一般来说如果主表属性名字和从表属性名字相同,一定要起别名,
在association或者collection中的column=""写别名就行了,
如果和主表列名写一样的不容易区分会出错.--><id property="id" column="id"/><!-- 普通的属性,其他字段--><result property="name" column="name"/><!-- 对象属性1:1
property="queen" 类中的属性字段
javaType="queen" java类型--><association property="queen"
javaType="queen"><id property="id" column="qid"/><result property="name" column="qname"/></association><!-- 集合 1:n
property="girlList"
ofType="girl" java类型--><collection property="girlList" ofType="girl"><id property="id" column="gid"/><result property="name" column="gname"/></collection></resultMap><!-- 数据来自两或多张表,多表关联的时候用resultMap="kingMap"--><select id="findKingAll" resultMap="kingMap">
select k.id,k.name,q.id qid,q.name qname,q.kid,g.id gid,g.name gname,g.kid
from king k left join queen q on k.id=q.kid
left join girl g on k.id = g.kid
</select><select id="findKingById" resultType="king">
select * from king where id=#{id}</select><!-- 使用动态sql
<where>如果里面有一个条件成立就会动态拼接where关键字
where里面trim会自动去重,所以第一个and会自动去掉
'%' #{name }'%''%${name }%'--><select id="search" resultType="king">
select * from king
<where><if test="id!=null">
and id=#{id}</if><if test="name!=null">
and name like '%' #{name }'%'</if></where></select><!--<when otherwise>ififelse--><select id="search2" resultType="king">
select * from king
<where><choose><when test="id!=null">
and id = #{id}</when><otherwise>
and name like '%${name}%'</otherwise></choose></where></select><insert id="saveKing" useGeneratedKeys="true" keyProperty="id">
insert into king(name) values (#{name})</insert><update id="updateKing">
update king set name=#{name} where id=#{id}</update><delete id="deleteKing">
delete from king where id=#{id}</delete></mapper>
在写一个mapper映射文件配置
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.hrf.mapper.GirlMapper"><!--打开多个二级缓存--><!--<cache/>--><insert id="saveGirl">
insert into girl(name,kid)values(#{name},#{kid})</insert><!--如果属性名和列名相同可以写autoMapping="true"进行自动映射--><resultMap id="girlMap" type="girl" autoMapping="true"><id property="id" column="id"/><result property="name" column="name"/><association property="king" javaType="king"><id property="id" column="kid"/><result property="name" column="kname"/></association></resultMap><!-- useCache="false"不使用二级缓存的效果
useCache="true"当前sql语句使用二级缓存
flushCache="true"当前sql语句执行完毕以后会清空缓存
--><select id="findGirlAll" resultMap="girlMap">
select g.id,g.name,g.kid,k.id kid,k.name kname from girl g
left join king k on g.kid=k.id
</select><!-- 不是动态更新--><update id="update2">
update girl set name=#{name},kid=#{kid}
where id=#{id}</update><!--动态更新--><update id="update">
update girl
<set><if test="name!=null">
name=#{name},</if><if test="kid!=null">
kid=#{kid}</if></set>
where id=#{id}</update></mapper>