xml文件信息:
<?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.wy.pojo.StudentMapper">
<!-- 查询所有的用户信息 id:自定义 唯一 见名知意 resultType:接收数据的类型 -->
<!-- 指定字段和属性的映射关系 -->
<resultMap type="Student" id="baseMap">
<id column="s_id" property="s_id"/>
<result column="s_name" property="s_name"/>
<result column="s_birth" property="s_birth"/>
<result column="s_sex" property="s_sex"/>
</resultMap>
<!-- 指定字段和属性的映射关系 -->
<resultMap type="Student" id="baseMap2">
<id column="s_id" property="s_id"/>
<result column="s_name" property="s_name"/>
<result column="s_birth" property="s_birth"/>
<result column="s_sex" property="s_sex"/>
<!-- 一对多关系配置 scoreList:student类中的集合名称,ofType相对应的javabean -->
<collection property="scoreList" ofType="Score">
<id column="s_id" property="s_id"/>
<result column="c_id" property="c_id"/>
<result column="s_score" property="s_score"/>
</collection>
</resultMap>
<!-- sql块 :将常用的sql部分提取出来,公用 -->
<sql id="baseSql">
s_id,s_name,s_birth,s_sex
</sql>
<select id="queryScore" resultMap="baseMap2">
select * from student st left join score sc on sc.s_id=st.s_id
</select>
<!-- if语句 -->
<select id="query1" parameterType="Student"
resultType="Student">
select
<include refid="baseSql"></include>
from
student
<where>
<if test="s_id != null">
and s_id=#{s_id}
</if>
<if test="s_name != null">
and s_name=#{s_name}
</if>
</where>
</select>
<!-- 优先级查询 -->
<select id="query2" parameterType="Student"
resultType="Student">
select
<include refid="baseSql"></include>
from
student
<where>
<choose>
<when test="s_id != null">
and s_id=#{s_id}
</when>
<when test="s_name != null">
and s_name=#{s_name}
</when>
<otherwise>
order by s_id
</otherwise>
</choose>
</where>
</select>
<select id="query5" resultType="Student">
select
<include refid="baseSql"></include>
from student
<where>
1=1
<if test="list != null">
and s_id in
<foreach collection="list" item="a" open="(" close=")" separator=",">
#{a}
</foreach>
</if>
</where>
</select>
<select id="select" resultType="Student">
select
<include refid="baseSql"></include>
from
student s
</select>
<update id="update" parameterType="Student">
update student set s_name=#{s_name},s_birth=#{s_birth},s_sex=#{s_sex} where
s_id=#{s_id}
</update>
<delete id="deleteById" parameterType="String">
delete from student where s_id=#{s_id}
</delete>
<insert id="save" parameterType="Student">
insert into student values(#{s_id},#{s_name},#{s_birth},#{s_sex})
</insert>
<update id="updateNull" parameterType="Student">
update student
<set>
<if test="s_name !=null">
s_name=#{s_name},
</if>
<if test="s_birth !=null">
s_birth=#{s_birth},
</if>
<if test="s_sex !=null">
s_sex=#{s_sex}
</if>
</set>
where s_id=#{s_id}
</update>
</mapper>
注意事项:
延迟加载
mybatis-config.xml 中添加
<!-- 开启延迟加载 -->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
一对一延迟加载mapper中的配置:
<mapper namespace="com.wy.pojo.CourseMapper">
<!-- 指定字段和属性的映射关系 -->
<resultMap type="Course" id="baseMap">
<id column="c_id" property="c_id"/>
<result column="c_name" property="c_name"/>
<!-- -->
<result column="t_id" property="t_id"/>
<!-- 一对一关系配置 scoreList:student类名称,ofType相对应的javabean -->
<association property="teacher" javaType="Teacher" column="t_id" select="findByCourseId">
<id column="t_id" property="t_id"/>
<result column="t_name" property="t_name"/>
</association>
</resultMap>
<select id="queryAll" resultMap="baseMap">
select * from course c ;
</select>
<select id="findByCourseId" resultType="Teacher" parameterType="int" >
select * from teacher t where t_id=#{t_id};
</select>
</mapper>
一对多延迟加载配置:
<resultMap type="Student" id="baseMap2">
<id column="s_id" property="s_id"/>
<result column="s_name" property="s_name"/>
<result column="s_birth" property="s_birth"/>
<result column="s_sex" property="s_sex"/>
<!-- 一对多关系配置 scoreList:student类中的集合名称,ofType相对应的javabean -->
<collection property="scoreList" ofType="Score" column="s_id" select="queryScoreAll">
<id column="s_id" property="s_id"/>
<result column="c_id" property="c_id"/>
<result column="s_score" property="s_score"/>
</collection>
</resultMap>
<select id="queryScoreAll" resultType="Score" parameterType="String">
select * from score where s_id=#{s_id}
</select>
<select id="queryAll" resultMap="baseMap2">
select * from student
</select>
mybatis缓存:
mybatis的一级缓存是默认存在的,存在,一个session周期
二级缓存,引入ehcache.jar包
ehcache-1.2.3.jar
mybatis-ehcache-1.0.3.jar
在需要二级缓存的mapper中添加cahe节点
<!-- 设置了该标记的映射文件中的所有擦汗寻都会进行二级缓存 -->
<cache type="org.mybatis.caches.ehcache.EhcacheCache.class"></cache>
注意:mybatis中做commit后缓存会清空