Place MyBatis Main Config file under src/main/resources

429 views
Skip to first unread message

Vladimir Alarcon

unread,
Nov 28, 2020, 1:49:01 PM11/28/20
to mybati...@googlegroups.com
Hi,

Is it possible to place the MyBatis Main Config file
("mybatis-config.xml") file inside src/main/resources ?

I have a Spring Boot project. I'm using MyBatis XML mappers and they
are located under src/main/resources. Good.

However, the main MyBatis XML config file needs to be placed under
src/main/webapp; if I place it under src/main/resources MyBatis does
not find it. This has also the undesired side effect that it becomes
available as a web resource. For example, this shouldn't work but it
does:

$ wget localhost:8080/mybatis-config.xml

This is how I configured it:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="mybatis-config.xml" />
<property name="objectFactory" ref="springBeanObjectFactory" />
</bean>

I'm using this configuration to set the "objectFactory" and read VOs
as Spring Beans.

Do you know any way to place it unser src/main/resources? A solution
using Java annotations is also welcome, but I haven't been able to
make it work.

Thank you,
Vlad

Jeff Butler

unread,
Nov 28, 2020, 8:41:22 PM11/28/20
to mybati...@googlegroups.com
In Spring you can use the “classpath:” prefix before a resource to load it from the classpath (src/main/resources is typically on the classpath of an application). 

You can also use the MyBatis Spring boot starter and avoid the main configuration file altogether. 

Jeff Butler


From: mybati...@googlegroups.com <mybati...@googlegroups.com> on behalf of Vladimir Alarcon <vladi...@gmail.com>
Sent: Saturday, November 28, 2020 1:48:56 PM
To: mybati...@googlegroups.com <mybati...@googlegroups.com>
Subject: Place MyBatis Main Config file under src/main/resources
 
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAP3Y0Vb3CwjxFcigbufw-4vzSG-DH_UeoF0NhuniQ2UPEgHF8g%40mail.gmail.com.

Biao Huang

unread,
Nov 29, 2020, 8:27:32 PM11/29/20
to mybati...@googlegroups.com
In the applicatioin.yml, you can specify the location of the mappers such as (placing the mapper files in the directory src/main/resources/mapper):

mybatis.mapper-locations: classpath:mapper/**/*.xml



Jeff Butler <jeffg...@gmail.com> 于2020年11月29日周日 上午9:41写道:


--
=====================================
QQ:     26664141
Skype: biao.mac
EMail:  biao...@gmail.com

Tel: Home:  495312217000
Tel: Mobile: 4917620793369
Reply all
Reply to author
Forward
0 new messages