需求:
1:联盟/部落玩家查询在线人数时只能查本阵营
2:管理员可以查询所有
枚举类
- 此处我直接使用了lombok插件,如果不喜欢使用lombok插件的可以自己生成get方法和构造器
@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));
}
}