Overview
Generic Maper removes the need for manual mapper XML configuration files. In most scenarios, you don't even need to define custom mapper interface methods, which dramatically improves development productivity.
Dependency Configuration
For version 3.1.0 and later, use the following Maven dependency:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.9</version>
</dependency>
For versions prior to 3.1.0, use this instead:
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>2.3.4</version>
</dependency>
Framework Configuration
Add the Generic Mapper interceptor to your MyBatis configuration file:
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<!-- Identity strategy for primary key generation -->
<property name="IDENTITY" value="MYSQL"/>
<!-- Base mapper interface -->
<property name="mappers" value="com.github.abel533.mapper.Mapper"/>
</plugin>
Creating a Mapper Interface
Create a mapper interface that extends the generic Mapper<T> interface, specifying your entity class as the type parameter:
public interface UserAccountMapper extends Mapper<UserAccount> {
// Custom methods can be added here if needed
}
Once extended, the mapper automatically inherits these CRUD operations:
List<T> select(T record);
int selectCount(T record);
T selectByPrimaryKey(Object key);
int insert(T record);
int insertSelective(T record);
int delete(T record);
int deleteByPrimaryKey(Object key);
int updateByPrimaryKey(T record);
int updateByPrimaryKeySelective(T record);
Additional example-based methods:
int selectCountByExample(Object example);
int deleteByExample(Object example);
List<T> selectByExample(Object example);
int updateByExampleSelective(T record, Object example);
int updateByExample(T record, Object example);
Entity Class Mapping Rules
The entity class must follow these conventions for automatic table mapping:
- Table names default to the class name with camelCase converted to snake_case. For instance,
UserProfilemaps touser_profile. - Use
@Table(name = "tableName")to override the default table name. - Column names default to fieldd names with camelCase converted to snake_case.
- Use
@Column(name = "fieldName")to specify custom column mappings. - Apply
@Transientto fields that should be excluded from database mapping. - Mark at least one field with
@Idas the primary key. Multiple@Idfields create a composite key.
Configuring Auto-Increment Primary Keys
For auto-generated primary keys, use the following annotation:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Complete Implementation Example
Entity Class:
@Table(name = "account_users")
public class UserAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String fullName;
private Integer age;
private Integer gender;
private Date birthDate;
private Date createTime;
private Date updateTime;
}
Mapper Interface:
public interface UserAccountMapper extends Mapper<UserAccount> {
}
Test Class:
public class UserAccountMapperTest {
private UserAccountMapper accountMapper;
@Before
public void initialize() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring/applicationContext*.xml");
this.accountMapper = context.getBean(UserAccountMapper.class);
}
@Test
public void testFindByName() {
UserAccount criteria = new UserAccount();
criteria.setFullName("John Doe");
UserAccount result = this.accountMapper.selectOne(criteria);
System.out.println(result);
}
@Test
public void testFindAll() {
List<UserAccount> accounts = this.accountMapper.select(null);
for (UserAccount account : accounts) {
System.out.println(account);
}
}
}
Advanced Queries with Example Criteria
The Example class enables dynamic query construction:
@Test
public void testFindByIds() {
Example criteria = new Example(UserAccount.class);
List<Object> idList = new ArrayList<>();
idList.add(1L);
idList.add(2L);
idList.add(3L);
criteria.createCriteria().addIn("id", idList);
List<UserAccount> results = this.accountMapper.selectByExample(criteria);
for (UserAccount account : results) {
System.out.println(account);
}
}
The generated SQL automatically applies pagination and ordering:
SELECT * FROM account_users WHERE id IN (1, 2, 3) ORDER BY create_time DESC LIMIT ?, ?