简体   繁体   中英

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:

@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:

@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 Concurrency Test result

As you can see, there exists barely 600+ concurrency requests differs in between. I would like to ask whether my implementation is not accurate? Is this about me not hooking into Spring Beans life cycle? Thanks in advance!

why not use mybatis-spring-boot-starter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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