博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis之SqlSessionFactory
阅读量:4203 次
发布时间:2019-05-26

本文共 3950 字,大约阅读时间需要 13 分钟。

MyBatis官方文档

  • ORM框架

范围和生命周期

SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,没有任何理由对它进行清除或重建。使用 SqlSessionFactory 的最佳实践是在应用运行期间不要重复创建多次,多次重建 SqlSessionFactory 被视为一种代码“坏味道(bad smell)”。因此 SqlSessionFactory 的最佳范围是应用范围。有很多方法可以做到,最简单的就是使用单例模式或者静态单例模式。【】

源码

package org.apache.ibatis.session;import java.sql.Connection;/** * Creates an {@link SqlSession} out of a connection or a DataSource * @author Clinton Begin */public interface SqlSessionFactory {
// 自动提交属性默认为false SqlSession openSession(); // 自定义自动提交属性 SqlSession openSession(boolean autoCommit); SqlSession openSession(Connection connection); SqlSession openSession(TransactionIsolationLevel level); SqlSession openSession(ExecutorType execType); SqlSession openSession(ExecutorType execType, boolean autoCommit); SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level); SqlSession openSession(ExecutorType execType, Connection connection); Configuration getConfiguration();}

实战

package com.sankuai.inf.leaf.segment.dao.impl;import com.sankuai.inf.leaf.segment.dao.IDAllocDao;import com.sankuai.inf.leaf.segment.dao.IDAllocMapper;import com.sankuai.inf.leaf.segment.model.LeafAlloc;import org.apache.ibatis.mapping.Environment;import org.apache.ibatis.session.Configuration;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.apache.ibatis.transaction.TransactionFactory;import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;import javax.sql.DataSource;import java.util.List;public class IDAllocDaoImpl implements IDAllocDao {
SqlSessionFactory sqlSessionFactory; public IDAllocDaoImpl(DataSource dataSource) {
TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(IDAllocMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); } @Override public List
getAllLeafAllocs() {
// 设置自动提交属性为false SqlSession sqlSession = sqlSessionFactory.openSession(false); try {
// 查询不需要提交事务 return sqlSession.selectList("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.getAllLeafAllocs"); } finally {
// 关闭sqlSession sqlSession.close(); } } @Override public LeafAlloc updateMaxIdAndGetLeafAlloc(String tag) {
// 自动提交属性默认为false SqlSession sqlSession = sqlSessionFactory.openSession(); try {
sqlSession.update("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.updateMaxId", tag); LeafAlloc result = sqlSession.selectOne("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.getLeafAlloc", tag); // 手动提交事务 sqlSession.commit(); return result; } finally {
// 关闭sqlSession sqlSession.close(); } } @Override public LeafAlloc updateMaxIdByCustomStepAndGetLeafAlloc(LeafAlloc leafAlloc) {
SqlSession sqlSession = sqlSessionFactory.openSession(); try {
sqlSession.update("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.updateMaxIdByCustomStep", leafAlloc); LeafAlloc result = sqlSession.selectOne("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.getLeafAlloc", leafAlloc.getKey()); sqlSession.commit(); return result; } finally {
sqlSession.close(); } } @Override public List
getAllTags() {
SqlSession sqlSession = sqlSessionFactory.openSession(false); try {
return sqlSession.selectList("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.getAllTags"); } finally {
sqlSession.close(); } }}

转载地址:http://dnvli.baihongyu.com/

你可能感兴趣的文章
Script and Test Data
查看>>
在ITPub上发表文章《如何进行测试自动化的成本估算》
查看>>
深圳市软件质量提升工程系列活动——安全测试百人大课堂
查看>>
做培训讲师就像做一名导演
查看>>
深圳51testing笔架山一日游
查看>>
LoadRunner如何在脚本运行时修改log设置选项?
查看>>
QC数据库表结构
查看>>
自动化测试工具的3个关键部分
查看>>
测试工具厂商的编程语言什么时候“退休”?
查看>>
资源监控工具 - Hyperic HQ
查看>>
LoadRunner中Concurrent与Simultaneous的区别
查看>>
SiteScope - Agentless监控
查看>>
QTP的智能识别(Smart Identification)过程
查看>>
LoadRunner各协议所需耗费的内存资源表
查看>>
AutomatedQA收购Smart Bear?
查看>>
使用QTP进行WEB页面性能测试
查看>>
LoadRunner的VS.NET 2005插件
查看>>
LoadRunner中如何验证下载的文件大小、统计下载时间、度量下载速度?
查看>>
LoadRunner脚本评审Checklist
查看>>
在LoadRunner中设置HTTP请求time-out的时间
查看>>