SoFunction
Updated on 2025-04-14

How to use XML and annotation hybrid configuration process in MyBatis

MyBatis uses a hybrid configuration with XML and annotations

In MyBatis, you can flexibly choose XML configuration methods and annotation methods, or mix these two methods to configure your mapper (Mapper).

Using a hybrid configuration approach, you can combine the advantages of both, for example, configuring complex queries and dynamic SQL with XML, while simplifying simple mapping configurations with annotations.

Below we will discuss in detail how to implement this hybrid configuration method in MyBatis.

Basic concepts

In MyBatis, a mapper is an interface where you can provide SQL mapping statements through XML files or annotations.

MyBatis will automatically scan these interfaces and mapping files when it is started, creating proxy objects for mappers for you to call in your code.

Use Notes

Annotation configuration is a relatively intuitive and easy to understand configuration method.

You can directly use @Select, @Insert, @Update and other annotations to specify SQL statements on the Mapper interface method, as shown below:

public interface UserMapper {
    @Select("SELECT * FROM users WHERE id = #{id}")
    User getUserById(int id);
}

Using XML

XML configuration allows you to write more complex SQL statements and dynamic SQL.

Typically, you create an XML file of the same name for each Mapper interface and define the SQL mapping statement in it, as follows:

<!--  -->
<mapper namespace="">
    <select  resultType="">
        SELECT * FROM users WHERE id = #{id}
    </select>
</mapper>

Mix XML and annotations

When you need to mix XML and annotations, it is important to make sure that MyBatis can find and handle all mapping configurations correctly.

The following steps show how to implement a hybrid configuration:

Configure the SQLSessionFactory of MyBatis

  • First, you need to configure MyBatisSqlSessionFactory, make sure it can load into all Mapper interfaces and XML mapping files.
  • If you are using an XML-based MyBatis configuration file, you can specify the location of the Mapper interface and XML file:
<configuration>
    <mappers>
        <mapper class=""/>
        <mapper resource="com/example/mapper/"/>
    </mappers>
</configuration>

If you use Java configuration, you can useSqlSessionFactoryBeanSet the location of the Mapper interface and XML mapping files:

@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    (dataSource());
    (
        new PathMatchingResourcePatternResolver().getResources("classpath*:com/example/mapper/*.xml"));
    ("");
    return ();
}

Pay attention to the matching of the Mapper interface and XML mapping files

  • When you define the same mapping methods in the Mapper interface and the corresponding XML file, you need to make sure they match correctly.
  • This is usually done by ensuring that the XML file isnamespaceThe attribute matches the full pathname of the Mapper interface, and the methodidImplemented by matching the interface method name.

Define annotation and XML mappings separately

  • You can use annotations to define some simple SQL mappings in the Mapper interface, and place more complex SQL statements and dynamic SQL in the XML mapping file.

Example

Suppose you have oneUserMapperInterface, you want to configure a simple query method through annotations, and at the same time configure a complex query method through XML:

public interface UserMapper {
    @Select("SELECT * FROM users WHERE id = #{id}")
    User getUserById(int id);
}

Then, inDefine another method map in:

<mapper namespace="">
    <select  resultType="">
        SELECT * FROM users WHERE name LIKE #{name}
    </select>
</mapper>

In the above configuration,getUserByIdThe method is configured by annotation, andfindUsersByNameMethods are configured via XML. In this way, you can enjoy the advantages of annotation and XML configuration methods in the same Mapper interface.

Summarize

MyBatis allows you to flexibly configure SQL mapping using XML and annotations, and even mix these two methods in the same Mapper interface.

This hybrid configuration allows you to quickly configure simple SQL statements by annotating, and can also take advantage of the power of XML configuration to handle more complex SQL and dynamic SQL. Just make sure to configure correctlySqlSessionFactory, so that MyBatis can recognize and load all mapping information.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.