mybatis中使用枚举判断

需求:
1:联盟/部落玩家查询在线人数时只能查本阵营
2:管理员可以查询所有

枚举类
  • 此处我直接使用了lombok插件,如果不喜欢使用lombok插件的可以自己生成get方法和构造器
@Getter
@AllArgsConstructor
public enum UserType {

    GM("gm","游戏管理员"),
    ALLIANCE("alliance","联盟"),
    HORDE("horde","部落");

    private String code;
    private String name;
}
mapper.xml

service.java/mapper.java
public interface IUserService {
    Integer countByType(UserType member);
}
public interface UserMapper {
    Integer countByType(UserType type);
}
service.java/mapper.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceImplTest {

    @Autowired
    private IUserService userService;

    @Test
    public void count(){
        System.out.println(userService.countByType(UserType.ALLIANCE));
        System.out.println(userService.countByType(UserType.HORDE));
        System.out.println(userService.countByType(UserType.GM));
    }
}    

你可能感兴趣的:(mybatis中使用枚举判断)