mybatis中使用枚举判断

2019-04-15  本文已影响0人  若愚同学

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

枚举类
@Getter
@AllArgsConstructor
public enum UserType {

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

    private String code;
    private String name;
}
mapper.xml
<select id="countByType" resultType="int">
  select count(1) from `user`
  <where>
    <if test="code == 'alliance' or code == 'horde'">
      and `type` = #{type}
    </if>
  </where>
</select>
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));
    }
}    
上一篇 下一篇

猜你喜欢

热点阅读