0

谁能帮我解决这个问题,我正在使用 SQLMap(ibatis) 使用 java。我有 3 个类,即 MainConfiguration、SQLMap、DBUtility。

  1. 主要配置(该类用于在 SQLMap 类中设置对象)

public class MainConfiguration
{
     public static String file = "configuration/db/SQLMapConfig.conf";

     public static void main(String[] args) throws Exception
     {
        new MainConfiguration().loadConfiguration();    
     }  

     public static void loadConfiguration()
     {
        SQLMap.setMapFile(file);
        List list = DBUtility.loadUsers();
     }
}
  1. SQL Map(这个类是and对象的getter和setter)

public final class SQLMap
{
    private static SqlMapClient sqlMap;

    public static void setMapFile(String sMapFile)
    {
        try
        {
            sqlMap = SqlMapClientBuilder.buildSqlMapClient(new FileReader(sMapFile));
        }
        catch (Exception e)
        {
            throw new RuntimeException("Error initializing SqlMapClient class", e);
        }
    }

    public static SqlMapClient getSqlMapInstance()
    {
        return sqlMap;
    }
}
  1. DBUtility(这个类是对象实例和从 SQLMap 类获取对象的地方)

    public class DBUtility
    {
    // object utility
    protected static SqlMapClient sqlMap = SQLMap.getSqlMapInstance();
    
    //constructor
    public DBUtility() throws Exception
    {
    }
    
    public static List loadUsers()
    {
        //it's working
        logger.info("SQLMap Get Instance = " + SQLMap.getSqlMapInstance());
    
        //it's not working
        logger.info("SQLMap Get Instance = " + sqlMap);
    
        //code below will be error because of null sqlMap
        try
        {
            listUser = sqlMap.queryForList("getUsers");
        }
        catch (Exception sqle)
        {
            logger.error("Error on load all user", sqle);
        }
        return listUser;
    }
    }
    

记录器给我这个:

SQLMap 获取实例 = com.ibatis.sqlmap.engine.impl.SqlMapClientImpl@76707e36

SQLMap 获取实例 = null

即使我有实例对象,第二个日志怎么给我null?

4

1 回答 1

0

加载 DBUtility 类时会初始化您的字段sqlMap,这显然发生在SQLMap.setMapFile(file);调用之前。所以,sqlMap指向不同的东西:null在静态字段中,以及在调用 getter 时的实际实例loadUsers()

问题是DBUtilitysqlMap得太早了。它必须等到file传递给SQLMap. 像这样更改代码以延迟初始化DbUtility.sqlMap

public static void loadConfiguration()
{
    SQLMap.setMapFile(file);
    DBUtility.initMapClient();  // notify DBUtility
    List list = DBUtility.loadUsers();
}

public class DBUtility
{
    protected static SqlMapClient sqlMap;  // do not initialize too early

    public static void initMapClient()
    {
        sqlMap = SQLMap.getSqlMapInstance();  // wait for SQLMap to be ready
    }

当然,如果您甚至没有sqlMapDBUtility 中的字段,那会更简单。SQLMap.getSqlMapInstance()每次需要的时候就打电话。如果实例发生变化,这一点尤其重要:

listUser = SQLMap.getSqlMapInstance().queryForList("getUsers");

阅读什么时候初始化静态变量?有关静态字段的更详细说明。

于 2017-06-09T08:40:35.537 回答