0

我知道这行不通,这让 IMO 很糟糕,但我想知道我最好的解决方案是什么。经过一番搜索,我发现了有关 ORM 和休眠的信息。在了解了它们之后,我发现很多人说它比它更值得,并促进了懒惰。请问有人能指点我一个好的方向吗?

//Setup the weapon classes
logger.info("Initializing the weapon classes.");
stat = conn.prepareStatement("SELECT *" + 
                            " FROM " + DB_NAME + ".weapons");
rs = stat.executeQuery();
while (rs.next()) {
    Weapon rs.getString("name") = new Weapon(rs.getint("weapon_id"), rs.getString("name"), rs.getString("description"), rs.getString("filename"), rs.getint("rarity"), rs.getint("sell_price"), rs.getint("quantity_max"), rs.getint("slot"), rs.getint("level_requirement"), rs.getint("strength_requirement"), rs.getint("dexterity_requirement"), rs.getint("intelligence_requirement"), rs.getint("enchanted_increase"), rs.getint("enchanted_cost"), rs.getint("hit_min"), rs.getint("hit_max"), rs.getint("speed"), rs.getint("elemental_type"), rs.getint("elemental_hit_min"), rs.getint("elemental_hit_max"));
}

我知道这已经被问过很多次了,但我只是发现了很多相互矛盾的意见。:(

4

3 回答 3

2

我建议使用Map来存储您从数据库中获取的武器实例。键是名称,值是您的 Weapons 类的实例。

就像是:

while(rs.next()){
    Weapon weapon = new Weapon(rs.getint("weapon_id"), rs.getString("name"), rs.getString("description"), rs.getString("filename"), rs.getint("rarity"), rs.getint("sell_price"), rs.getint("quantity_max"), rs.getint("slot"), rs.getint("level_requirement"), rs.getint("strength_requirement"), rs.getint("dexterity_requirement"), rs.getint("intelligence_requirement"), rs.getint("enchanted_increase"), rs.getint("enchanted_cost"), rs.getint("hit_min"), rs.getint("hit_max"), rs.getint("speed"), rs.getint("elemental_type"), rs.getint("elemental_hit_min"), rs.getint("elemental_hit_max"));
    //Assuming you have an instance of Map<String, Weapon>
    map.add(rs.getString("name"), weapon);
}
于 2012-11-07T06:03:20.223 回答
1

使用 ORM 取决于您的项目大小。就个人而言,通过查看您对 Java 的理解,我建议您现在跳入休眠等。我建议您首先直接使用 SQL,以便更好地理解持久性。

至于您发布的代码,您可以执行以下操作:

//Setup the weapon classes
logger.info("Initializing the weapon classes.");
stat = conn.prepareStatement("SELECT *" + 
                            " FROM " + DB_NAME + ".weapons");

HashMap<String, Weapon> weapons = new HashMap<String, Weapon>();

rs = stat.executeQuery();
while (rs.next()) {
    Weapon weapon = new Weapon()
        .setId(rs.getint("weapon_id"))
        .setName(rs.getString("name"))
        .setDescription(rs.getString("description"))
        .setFilename(rs.getString("filename"))
        .setRarity(rs.getint("rarity"))
        .setSellPrice(rs.getint("sell_price"))
        .setQuantityMax(rs.getint("quantity_max"))
        .setSlot(rs.getint("slot"))
        .setLvlRequirement(rs.getint("level_requirement"))
        .setStrRequirement(rs.getint("strength_requirement"))
        .setDexRequirement(rs.getint("dexterity_requirement"))
        .setIntRequirement(rs.getint("intelligence_requirement"))
        .setEnchantedInc(rs.getint("enchanted_increase"))
        .setEnchantedCost(rs.getint("enchanted_cost"))
        .setHitMin(rs.getint("hit_min"))
        .setHitMax(rs.getint("hit_max"))
        .setSpeed(rs.getint("speed"))
        .setElementalType(rs.getint("elemental_type"))
        .setElementalHitMin(rs.getint("elemental_hit_min"))
        .setElementalHitMax(rs.getint("elemental_hit_max"));

    weapons.put(rs.getString("name"), weapon);
}

避免使用带有太多参数的构造函数(甚至是方法!)。除非您将类导出给第三方或将其暴露给某些用户插件并且不希望任何人更改内部值,否则不要担心 setter!然后每个set*方法都返回this,因此它们可以被链接起来。例如 :

public Weapon setHitMax(int max) {
    this.hitMax = max;
    return this;
}

你可以,然后使用Map. 比如,例如:

weapons.get("sword").getHitMax();
于 2012-11-07T06:08:44.627 回答
0

“动态类创建”到底是什么意思?从您的代码中我可以理解两件事:

a) 你是一个非常糟糕的 Java 程序员 :) 或者

b)您试图为数据库中的每一行命名不同的变量,(这没有任何意义)

于 2012-11-07T06:03:11.640 回答