Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure SqlSessionFactoryBean based on java file for springboot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "springboot based on java files how to configure SqlSessionFactoryBean", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "springboot based on java files how to configure SqlSessionFactoryBean" bar!

Brief introduction

Springboot + mybatis + mysql demo with the simplest configuration

Inside is the simplest configuration SessionFactoryConfig we specify to scan our mapper.java files through @ MapperScan, but we need to put the corresponding mapper.xml in a folder under the same package path of resources to load it; similar to the following image

Of course, if the package path of our mapper is deep, the directory of this folder will also be relatively deep.

We can configure SqlSessionFactoryBean based on xml to specify a xml configuration that loads multiple paths

Of course, java-based configuration is also possible; the following two figures are the differences before and after modification

Modified configuration @ Bean (name = "sqlSessionFactory") public SqlSessionFactoryBean createSqlSessionFactoryBean () throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean (); / * * set mybatis configuration scan path * / sqlSessionFactoryBean.setConfigLocation (new ClassPathResource (MYBATIS_CONFIG)); / * set datasource * / sqlSessionFactoryBean.setDataSource (dataSource) / * * set typeAlias package scan path * / sqlSessionFactoryBean.setTypeAliasesPackage (typeAliasPackage); return sqlSessionFactoryBean;} modified configuration @ Bean (name = "sqlSessionFactory") public SqlSessionFactoryBean createSqlSessionFactoryBean () throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean () / * * set mybatis configuration scan path * / sqlSessionFactoryBean.setConfigLocation (new ClassPathResource (MYBATIS_CONFIG)); / * * set datasource * / sqlSessionFactoryBean.setDataSource (dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver (); sqlSessionFactoryBean.setMapperLocations (resolver.getResources ("classpath:sqlmapper/*.xml")); / / load the address of the configuration file / / sqlSessionFactoryBean.setMapperLocations (new Resource [] {new ClassPathResource ("classpath:mapper/*.xml")}); / * * set typeAlias package scan path * / sqlSessionFactoryBean.setTypeAliasesPackage (typeAliasPackage); return sqlSessionFactoryBean;} PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver (); sqlSessionFactoryBean.setMapperLocations (resolver.getResources ("classpath:sqlmapper/*.xml")); / / address to load the configuration file With the code content above, we specified to load all the xml under the sqlmapper directory under the resources directory

It should be noted that it does not seem to work to use the following configuration directly

Thank you for reading, the above is "springboot based on java file how to configure SqlSessionFactoryBean" content, after the study of this article, I believe you on springboot based on java file how to configure SqlSessionFactoryBean this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report