MyBatis - 如何从swing应用程序设置数据库属性?

时间:2011-05-21 20:08:37

标签: java swing netbeans mybatis

假设我有摇摆应用程序而且我正在使用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的配置?

感谢您的任何建议。

1 个答案:

答案 0 :(得分: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;
    }
}