3.4 Provider注解
- SelectProvider
- InsertProvider
- UpdateProvider
- DeleteProvider
public interface PrivilegeMapper {
@SelectProvider(type = PrivilegeProvider.class, method = "selectById")
SysPrivilege selectById(Long id);
}
Provider注解中,type和method属性是必填的,method属性指定的方法的返回值必须是Stringpublic class PrivilegeProvider {
public String selectById(final Long id) {
return new SQL() {
{
SELECT("id, privilege_name, privilege_url");
FROM("sys_privilege");
WHERE("id = #{id}");
}
}.toString();
}
}
测试:public class PrivilegeMapperTest extends BaseMapperTest {
@Test
public void testSelectById() {
SqlSession sqlSession = getSqlSession();
try {
PrivilegeMapper privilegeMapper = sqlSession.getMapper(PrivilegeMapper.class);
SysPrivilege sysPrivilege = privilegeMapper.selectById(1L);
Assert.assertNotNull(sysPrivilege);
Assert.assertEquals("用户管理", sysPrivilege.getPrivilegeName());
}finally {
sqlSession.close();
}
}
}
注解方式不是主流,了解即可