2013/12/27 Christopher Clark <
ludach...@gmail.com>:
> Thanks for your reply Eduardo. I'm entirely new to MyBatis, so perhaps this
> is
> newbie stuff.
>
> Indeed the Java Mapper interfaces are registered when the correct package is
> scanned with @MapperScan. I should specify further: my problem is in binding
> the
> Java Mapper interfaces to a peer XML mapper.
>
> I see in the MyBatis documentation: "MyBatis will automatically look for and
> load a peer XML file if it exists" with the same name as the Java Mapper
> interface and located in the same package.
>
> Unfortunately I couldn't get that working
1)
If you want to use Spring + MyBatis , I recommend you to use Spring IOC
I paste the code that I use
<!-- Enable autowire -->
<context:annotation-config />
<context:property-placeholder location="/WEB-INF/database.properties" />
<bean id="dataSource1" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName"
value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="maxActive" value="${database.maxactive}" />
<property name="maxWait" value="${database.maxwait}" />
<property name="maxIdle" value="${database.maxidle}" />
<property name="minIdle" value="${database.minidle}" />
<property name="initialSize" value="${database.initialsize}" />
<property name="removeAbandoned"
value="${database.removeabandoned}" />
<property name="validationQuery"
value="${database.validationquery}" />
<property name="testOnBorrow" value="true"/>
</bean>
<!-- Define the SqlSessionFactory, Notice that configLocation
is not needed when you use MapperFactoryBean -->
<bean id="sqlSessionFactory1"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="configLocation"
value="WEB-INF/sqlmap-config.xml" />
</bean>
<!-- Scan for mappers and let them be @Autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.myapp.persistence.mapper.*"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>
</bean>
Have a look like mapper interfaces are registered into Spring context
with MapperScannerConfigurer , scanning in a basePackage
Now, in your class, you can inject (only) your mappers, like:
@Autowired
MyAppMapper myappMapper;
This mapper is thread-safe ( if I'm not wrong )
2)
If your mapper interfaces have all theirs methods annotated with
@Select annotations, you don't need any XML file
But. if they don't, XML files and mapper interfaces must be in the
same directory
I hope this help you
Regards