Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:04:09+00:00 2026-05-28T18:04:09+00:00

Is it possible to have both the XML + Annotation based configuration for MyBatis

  • 0

Is it possible to have both the XML + Annotation based configuration for MyBatis in an application.

The reason that I am asking this is because, in my application I am using the Annotation based approach.
But in one of the scenarios , I require to use an IN clause, which can be built using the

<foreach></foreach>

based XML configuration.

However , when I start my application , it does not seem to recognize my Annotation based mappers and gives me a Type interface is not known to the MapperRegistry exception

Therefore I would like to know whether is it possible to have both the XML + Annotation based configuration for MyBatis in an application.
Please suggest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:04:10+00:00Added an answer on May 28, 2026 at 6:04 pm

    Its possible to have both XML + Annotation based configuration

    For xml based configuration:

    Start with your spring context file.In your context file add following lines

    <beans:bean id="dataSource"
                  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
                  p:driverClassName="yourDriverClassName"
                  p:url="yourUrl"
                  p:username="yourUsername"
                  p:password="yourPassword" />
    
    <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
    </beans:bean>
    <!-- assuming you have a dao named UserDao -->
    <beans:bean id="userDao" class="com.yourcomp.dao.Userdao">
        <beans:property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </beans:bean>
    

    your mybatis-config.xml should be something like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
      <settings>
    
      </settings>
      <typeAliases>
    
        <typeAlias alias="User" type ="com.yourcomp.domain.User" />
    
      </typeAliases>
      <mappers>
    
        <mapper resource="com/yourcomp/domain/UserMapper.xml"/>
        <mapper resource="com/yourcomp/domain/AnotherDomainObjectMapper.xml"/>
      </mappers>
    
    </configuration>
    

    and your userMapper.xml in src/com/yourcomp/domain/ might be something like this

    <?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="org.pbl.rms.RMSUserDao">-->
    
    <mapper namespace="com.yourcomp.domain.User">
    
      <resultMap id="userMap" type="User">
      <id property="userId" column="USER_ID" javaType="int" jdbcType="NUMERIC"/>
      <result property="userName"  column="USER_NAME" javaType="String" jdbcType="VARCHAR"/>
      <result property="userFullName"  column="USER_FULL_NAME" javaType="String" jdbcType="VARCHAR"/>
      <result property="password"  column="PASSWORD" javaType="String" jdbcType="VARCHAR"/>
      <result property="passwordExpiryDate"  column="PASWRD_EXPIRY_DATE" javaType="java.util.Date" jdbcType="DATE"/>
      <result property="status" column="STATUS" javaType="_integer" jdbcType="DECIMAL"/>
      </resultMap>
    
      <select id="getUserById" parameterType="map" resultMap="userMap">
      select  * from user where USER_ID=#{userId}
      </select>
    
    </mapper>
    

    now in your DAO layer you might have class like follows:

    public class UserDAO{
    
        private SqlSessionFactory sqlSessionFactory;
    
        public UserDAO() {
    
        }
    
        public UserDAO(SqlSessionFactory sqlSessionFactory ) {
            this.sqlSessionFactory = sqlSessionFactory;
    
        }
        public String getUserById(Integer userId) {
            SqlSession session = sqlSessionFactory.openSession();
            //int success = -100;
            String name=null;
            try {
                name = (String)session.selectOne("com.yourcomp.domain.User.getUserById",userId);
            }catch(Exception e){
    
            }finally {
                session.close();
            }
            return name;
        }
    }
    

    Now For using annotation based configuration add the followings:

    You might have an interface like follows:

    public interface MyMapper {
        @Select(value="select something from some_table")
        public String getSomeValue(@Param("someParam") String someParam);
    
    }
    

    now add the followings to your context file:

    <beans:bean id="annotatedMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <beans:property name="mapperInterface" value="com.yourcomp.MyMapper" />
      <!-- remember the sqlsession factory defined earlier for xml based conf ? -->
      <beans:property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </beans:bean>
    

    Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if this is possible in jaxb2. I have a xml that can
Is it possible to have a PHP function that is both recursive and anonymous?
Is this possible? I have a solution with both projects, I just want to
I have an application that runs on both C# .Net and Java . Two
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
I have set up a WCF service that will accept both JSON and XML
I have a XML file with data that is used in both my C#
Is it possible to have both NetTcp bound endpoints, and basicHttp bound endpoints with
Is it possible to have both versions of maven running on the same computer?
is it not possible to have both CLICK and DOUBLE_CLICK on the same display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.