Can I set xml mapper namespace to Interface object?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I set xml mapper namespace to Interface object?

leo k
I defined two sqlSessionTemplate for master and slave database as masterSqlSessionTemplate and slaveSqlSessionTemplate.
I want to separate the master and slave interfaces in the interface phase.
The xml mapper wants to use one file.

Is there an annotation about to 
@MapperNameSpace("path/to/xml/mapper")
in mybatis?

Ex)

application-context-database.xml
<mybatis:scan base-package="com.test.mapper.**" 
annotation="com.test.annotations.MasterDatabase"
template-ref="masterSqlSessionTemplate"/>
<mybatis:scan base-package="com.test.mapper.**" 
annotation="com.test.annotations.MasterDatabase"
template-ref="slaveSqlSessionTemplate"/>



TestMasterDatabaseMapper.java
package com.test.mapper;

import com.test.annotations.MasterDatabase;

@Mapper
@MasterDatabase
public interface TestMasterDatabaseMapper {
    // select test list from master database;
   
public List<TestVo> selectTestList();  // I want mapping to com.test.mapper.TestMapper xml mapper namespace
}

TestSlaveDatabaseMapper.java
package com.test.mapper;

import com.test.annotations.SlaveDatabase;

@Mapper
@SlaveDatabase
public interface TestSlaveDatabaseMapper {
    // select test list from slave database;
   
public List<TestVo> selectTestList();  // I want mapping to com.test.mapper.TestMapper xml mapper namespace
}


TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mapper.TestMapper">
    <select id="selectTestList">
        ........
    </select>
</mapper>


--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can I set xml mapper namespace to Interface object?

Erwan Letessier
The namespace must match mapper interface full qualified name.
I'm not aware of any way to override that.
IMO it would not be a good design. It seems you want to implement a kind of fail over. The fact that there is 1, 2 or 50 databases is a detail of the infrastructure the application should not be aware of.


On Thu, Dec 13, 2018, 03:08 leo k <[hidden email] wrote:
I defined two sqlSessionTemplate for master and slave database as masterSqlSessionTemplate and slaveSqlSessionTemplate.
I want to separate the master and slave interfaces in the interface phase.
The xml mapper wants to use one file.

Is there an annotation about to 
@MapperNameSpace("path/to/xml/mapper")
in mybatis?

Ex)

application-context-database.xml
<mybatis:scan base-package="com.test.mapper.**" 
annotation="com.test.annotations.MasterDatabase"
template-ref="masterSqlSessionTemplate"/>
<mybatis:scan base-package="com.test.mapper.**" 
annotation="com.test.annotations.MasterDatabase"
template-ref="slaveSqlSessionTemplate"/>



TestMasterDatabaseMapper.java
package com.test.mapper;

import com.test.annotations.MasterDatabase;

@Mapper
@MasterDatabase
public interface TestMasterDatabaseMapper {
    // select test list from master database;
   
public List<TestVo> selectTestList();  // I want mapping to com.test.mapper.TestMapper xml mapper namespace
}

TestSlaveDatabaseMapper.java
package com.test.mapper;

import com.test.annotations.SlaveDatabase;

@Mapper
@SlaveDatabase
public interface TestSlaveDatabaseMapper {
    // select test list from slave database;
   
public List<TestVo> selectTestList();  // I want mapping to com.test.mapper.TestMapper xml mapper namespace
}


TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mapper.TestMapper">
    <select id="selectTestList">
        ........
    </select>
</mapper>


--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.