MyBatis Type Alias Configuration and Usage
Overview
In MyBatis mapper XML files, fully qualified class names for parameterType and resultType attributes can become lengthy. Type aliases provide a cleaner solution by allowing you to reference classes by simple names.
Defining Type Aliases
Aliases are configured in the mybatis-config.xml file within the <typeAliases> section. This section must be placed after <properties> and before <environments>.
Single Class Alias
<typeAliases>
<typeAlias type="com.example.entity.User" alias="User"/>
</typeAliases>
After defining this alias, you can use it in mapper files:
<insert id="addUser" parameterType="User">
INSERT INTO users(name, email) VALUES(#{name}, #{email})
</insert>
Package-Level Alias
When working with multiple classes in the same package, defining aliases individualyl becomes tedious. The <package> element automatically registers all classes in a package using their simple class names:
<typeAliases>
<package name="com.example.entity"/>
</typeAliases>
With this configuration, User, Product, and Order classes are automatically available as aliases.
Practical Example
Entity Class
package com.example.entity;
public class Product {
private Long id;
private String productName;
private BigDecimal price;
public Product() {}
public Product(String productName, BigDecimal price) {
this.productName = productName;
this.price = price;
}
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getProductName() { return productName; }
public void setProductName(String productName) { this.productName = productName; }
public BigDecimal getPrice() { return price; }
public void setPrice(BigDecimal price) { this.price = price; }
}
DAO Interface
package com.example.dao;
import com.example.entity.Product;
public interface ProductDao {
void save(Product product);
}
DAO Implementation
package com.example.dao;
import com.example.entity.Product;
import org.apache.ibatis.session.SqlSession;
public class ProductDaoImpl implements ProductDao {
@Override
public void save(Product product) {
try (SqlSession session = MyBatisSessionFactory.openSession()) {
session.insert("ProductMapper.insert", product);
session.commit();
}
}
}
Mapper XML
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="ProductMapper">
<insert id="insert" parameterType="Product">
INSERT INTO products(product_name, price)
VALUES(#{productName}, #{price})
</insert>
</mapper>
MyBatis Configuration
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<package name="com.example.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/ProductMapper.xml"/>
</mappers>
</configuration>
Dtaabase Properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myapp
jdbc.username=root
jdbc.password=secret
Key Points
| Aspect | Description |
|---|---|
| Location | Must be in mybatis-config.xml between <properties> and <environments> |
| Single alias | Use <typeAlias> with type and alias attributes |
| Package alias | Use <package> to auto-register all classes in a package |
| Case sensitivity | Aliases are case-insensitive by default |
The package-level approach is recommended for most projects as it reduces configuration overhead and keeps the codebase maintainable.