06_关于SqlSession
一.SqlSession适用范围
(1).SqlSessionFactoryBuilder
通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory
将SqlSessionFactoryBuilder当成一个工具类使用,无需单例模式管理SqlSessionFactoryBuilder。
在需要创建SqlSessionFactory时,只需new一次SqlSessionFactoryBuilder即可。
//mybatis配置文件
String resource="SqlMapConfig.xml"; //得到配置文件
InputStream inputStream=Resources.getResourceAsStream(resource); //创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
(2)SqlSessionFactory
通过SqlSessionFactory创建SqlSession,使用单例模式管理SqlSessionFactory(工厂一旦创建,使用一个单例)
(MyBatis与Spring整合后,使用单例模式管理SqlSessionFactory)
//通过工厂得到SqlSession
SqlSession sqlSession=sqlSessionFactory.openSession();
(3)SqlSession
SqlSession是一个面向用户(软件工程师)的接口。
SqlSession中提供了许多操纵数据库的方法,如:
selectOne():返回单个对象
selectList():返回当个或多个对象
.....
SqlSession是线程不安全的,在SqlSession实现类中除了接口中的方法(操作数据库的方法),还有数据域属性。
SqlSession最佳应用场合在方法体内,定义成局部变量使用。
//通过SqlSession操作数据库
//第一个参数:映射文件中statement的id,等于:namespace+"."+statement的id
//第二个参数:指定映射文件中的所匹配的parameterType类型的参数
User user=sqlSession.selectOne("test.findUserById", 2);
//删除用户
sqlSession.delete("test.deleteUserById",2);
//插入数据,后返回id
sqlSession.insert("test.insertUserReturnId",user);
//更新数据
sqlSession.update("test.updateUserById",user);
06_关于SqlSession的更多相关文章
- java.lang.NoClassDefFoundError: org/apache/ibatis/session/SqlSession
在配置一个springmvc+mybatis的项目时,总是有报一个错误: org.springframework.beans.factory.BeanCreationException: Error ...
- MyBatis的Dao层注入SqlSession
有点坑爹,以前没用过Mybatis,最近才用,而且一直用Mybatis推荐的接口映射的方式,但是今天有人告诉我接口方式用得少,大多还是采用从配置文件里面读sql的方式,当然接口也是类似的,都是利用ma ...
- MyBatis 3与spring整合之使用SqlSession
SqlSessionTemplate是MyBatis-Spring的核心.这个类负责管理MyBatis的SqlSession.调用MyBatis的SQL方法. SqlSessionTemplate是线 ...
- MyBatis Mapper 接口如何通过JDK动态代理来包装SqlSession 源码分析
我们以往使用ibatis或者mybatis 都是以这种方式调用XML当中定义的CRUD标签来执行SQL 比如这样 <?xml version="1.0" encoding=& ...
- 异常:java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession
使用mybatis-3.2.2.jar + mybatis-spring-1.2.0.jar集成时,报以下异常: 15:42:48.538 [Thread-1] DEBUG o.s.b.f.s.Dis ...
- MyBatis 入门到精通(一) 了解MyBatis获取SqlSession
MyBatis是什么? MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...
- 李洪强iOS开发Swift篇—06_流程控制
李洪强iOS开发Swift篇—06_流程控制 一.swift中的流程控制 Swift支持的流程结构如下: 循环结构:for.for-in.while.do-while 选择结构:if.switch 注 ...
- MyBatis 源码分析——SqlSession接口和Executor类
mybatis框架在操作数据的时候,离不开SqlSession接口实例类的作用.可以说SqlSession接口实例是开发过程中打交道最多的一个类.即是DefaultSqlSession类.如果笔者记得 ...
- mybatis随笔三之SqlSession
在上一篇文章我们已经得到了DefaultSqlSession,接下来我们对sqlSession.getMapper(DemoMapper.class)这种语句进行分析 @Override public ...
随机推荐
- java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 *&* 解决方法
java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0*&*^ at java. ...
- tail -f logfile.log 一直监控某个文件,若该文件有改动,立即在屏幕上输出
tail -f logfile.log 可以一直监控某个文件,只要文件有改动,就立即在屏幕上输出
- Script.NET Perl解释器代码已经在GitHub开源发布
Script.NET Perl解释器的代码已经提交到GitHub网站.GitHub项目地址: https://github.com/blueantst/Script.NET Perl解释器代码在Src ...
- [struts2]struts标签遍历map[转]
转至:http://hanxin0311.iteye.com/blog/1745792 private Map<String, String> strMap = new HashMap&l ...
- Android开源git40个App源码
http://www.itbbu.com/1039.html (JamsMusicPlayer)很棒的音乐播放器(new) (F8)日程安排的软件 (Conversations)基于XMPP的 ...
- [Canvas] Introduction to drawing with p5js
In this lesson we look at using color and the basic drawing methods of p5js, and how they can be com ...
- android107 指针入门
#include <stdio.h> #include <stdlib.h> //指针就是内存地址 //32为操作系统最大是4G内存,32为系统则是2的32次方, //所以只能 ...
- 进程环境之setjmp和longjmp函数
在C中,goto语句是不能跨越函数的,而执行这样跳转功能的是函数setjmp和longjmp.这两个函数对于处理发生在深层嵌套函数调用中的出错情况是非常有用的. setjmp和longjmp函数也称为 ...
- Android开发之自定义圆角矩形图片ImageView的实现
android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...
- Android(java)学习笔记158:Java程序入口和Android的APK入口
1.Java程序的入口:static main()方法 public class welcome extends Activity { @Override public void onCreate(B ...