1

假设我有 Swing 应用程序并且我正在使用 mybatis:

public class MyAppCView extends FrameView {

    public SqlSession session;
    public StaticMapper mapper;

    public Config c = new Config();

    public MyAppView(SingleFrameApplication app) {
        super(app);      

        String user="root", pswd="root"    

        session = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();
        mapper = session.getMapper(StaticMapper.class);  

MyBatisSqlSessionFactory 看起来像这样:

public class MyBatisSqlSessionFactory {
    public static Map<String,String> propeties = new HashMap<String,String>();

    protected static final SqlSessionFactory FACTORY;


    static {
        try {
            Properties props = new Properties();

            props.setProperty("username", user);
            ....

            // how can i get variables from swing application into configuration of sqlfactory?

            Reader reader = Resources.getResourceAsReader("wsnscc/mybatis/xml/Configuration.xml");
            FACTORY = new SqlSessionFactoryBuilder().build(reader,props);
        } catch (Exception e){
            throw new RuntimeException("Fatal Error.  Cause: " + e, e);
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return FACTORY;
    }
}

如何从 Swing 应用程序中获取变量到 sqlfactory 的配置中?

感谢您的任何建议。

4

1 回答 1

0

您将变量传递到 SQL 工厂。

您可以通过将类更改为以下内容来做到这MyBatisSQLSessionFactory一点:

public class MyBatisSqlSessionFactory {
    public static Map<String,String> properties = new HashMap<String,String>();

    protected static final SqlSessionFactory FACTORY;

    public MyBatisSqlSessionFactory(String userid, String password) {
        try {
            Properties props = new Properties();

            props.setProperty("username", userid);
            props.setProperty("password", password);

            Reader reader = Resources.getResourceAsReader
               ("wsnscc/mybatis/xml/Configuration.xml");

        } catch (Exception e){
            throw new RuntimeException("Fatal Error.  Cause: " + e, e);
        }
    }

    public static SqlSessionFactory getSqlSessionFactory
            (String userid, String password) 
            throws RuntimeException {
        if (FACTORY == null) 
            FACTORY = new MyBatisSqlSessionFactory(userid, password);
        return FACTORY;
    }
}
于 2011-05-24T19:34:45.027 回答