简体   繁体   English

MyBatis 如何集成到 Spring 框架中

[英]How MyBatis intergrated into Spring Framework

every sincere one on the net, I am brand new to this site eagring for your help, Yesterday.网上的每一位真诚的人,昨天我是这个网站的新手,渴望得到你的帮助。 I've just intergrated MyBatis into Spring Boot by configuring bean via Annotation style: Here is my code:我刚刚通过注释样式配置 bean 将 MyBatis 集成到 Spring Boot 中:这是我的代码:

@Configuration
public class MyBatisBuild {

    @Bean
    public SqlSessionFactory createSqlSessionFactory() throws IOException {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        InputStream inputStream = Resources.getResourceAsStream("MyBatis.config.xml");
        return sqlSessionFactoryBuilder.build(inputStream);
    }
}

Relative Controller:相对Controller:

@RestController
public class DiaryController {

    private DiaryService diaryService;

    @GetMapping("/diary/all")
    public List<Diary> getAll() {
        return diaryService.getAll();
    }

    @Autowired
    public void setDiaryService(DiaryService diaryService) {
        this.diaryService = diaryService;
    }
}

The Service injected:注入的服务:

@Service
public class DiaryService {

    private final SqlSessionFactory sqlSessionFactory;

    public DiaryService(@Autowired SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    public List<Diary> getAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, TransactionIsolationLevel.READ_UNCOMMITTED);
        List<Diary> list = sqlSession.selectList("com.qlintonger.xxlint.dao.DiaryDao.getAllDiaries");
        sqlSession.close();
        return list;
    }
}

The request flow concurrency test result:请求流并发测试结果:

My-Own Concurrency Test result我自己的并发测试结果

For comparision, place officially MyBatis-Spring intergration concurrency result convincing that every Model, Mapper, Service and Controller settings are all the same:为了比较,官方将 MyBatis-Spring 集成并发结果证明每个 Model、Mapper、Service 和 Controller 设置都是相同的:

MyBatis-Spring Concurrency Test result MyBatis-Spring 并发测试结果

As you can see, there exists barely 600+ concurrency requests differs in between.如您所见,两者之间几乎不存在 600 多个并发请求。 I would like to ask whether my implementation is not accurate?请问我的执行是否不准确? Is this about me not hooking into Spring Beans life cycle?这是关于我没有进入 Spring Beans 生命周期的问题吗? Thanks in advance!提前致谢!

why not use mybatis-spring-boot-starter为什么不使用mybatis-spring-boot-starter

<dependency>
 <groupId>org.mybatis.spring.boot</groupId>
 <artifactId>mybatis-spring-boot-starter</artifactId>
 <version>2.2.0</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM