3.4 Provider注解

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