mybatis3 初步认识

一、创建

1.创建sqlSessionFactory

2.通过sqlSessionFactory获取sqlSession-->(用来映射sql语句,是一个流吗,记得关闭流。有select等方法)

例子:BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);

二、sql映射(SqlSession --Mapper)

1.<mapper namespace="org.mybatis.example.BlogMapper"> 命名空间
<select id="selectBlog" parameterType="int" resultType="Blog"> 参数和返回值的配置
select * from Blog where id = #{id} 相关语句
</select>
</mapper>

2.BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);

3.java注释替换xml

package org.mybatis.example;
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}----缺点:有局限性,复杂的sql语句比较混乱

三、作用域和生命周期

sqlSessionFactory---始终存在Applicatio

sqlSession ---每个线程都有自己的SqlSession 实例作用域 Request

mapper --和sqlSession一样,但是不用手动finally,它会自己随着sqlSession销毁

四、XML 配置文件

1.提供了各种类型,用户还可以自定义类型

2.通过xml配置文件可以对mybatis的一些属性配置

五、对象工厂(ObjectFactory)

创建相应的对象

六、映射器(Mappers)

用url来告诉java去哪里找映射

注:最为重要的就是xml中各个标签元素的使用

七、缓存(cache)

1.作用:

1.select语句将被缓存

2.inser、update、delete语句会会被清空

3.用最少使用的算法来收回

4.“读、写”缓存是安全的,不被他人所共享

2.缓存可以自定义

3.缓存可以共享

八、动态语句的编写

1.作用:简化sql复杂化的编写

九、javaAPI的调用方法使用

猜你喜欢

转载自jiangyanivy.iteye.com/blog/1416745
今日推荐