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

Java MyBatis-如何从swing应用程序设置数据库属性?,java,swing,netbeans,mybatis,Java,Swing,Netbeans,Mybatis,假设我有swing应用程序,我正在使用mybatis: public class MyAppCView extends FrameView { public SqlSession session; public StaticMapper mapper; public Config c = new Config(); public MyAppView(SingleFrameApplication app) { super(app);

假设我有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;
    }
}
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;
    }
}
公共类MyBatisSqlSessionFactory{
公共静态映射属性=新HashMap();
受保护的静态最终SqlSessionFactory工厂;
静止的{
试一试{
Properties props=新属性();
props.setProperty(“用户名”,用户);
....
//如何将swing应用程序中的变量配置到sqlfactory中?
Reader=Resources.getResourceAsReader(“wsnscc/mybatis/xml/Configuration.xml”);
FACTORY=new SqlSessionFactoryBuilder().build(读卡器、道具);
}捕获(例外e){
抛出新的运行时异常(“致命错误。原因:”+e,e);
}
}
公共静态SqlSessionFactory getSqlSessionFactory(){
返回工厂;
}
}
如何将swing应用程序中的变量配置到sqlfactory中


谢谢您的建议。

您可以将变量传递到SQL工厂

您可以通过将类
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;
    }
}
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;
    }
}
公共类MyBatisSqlSessionFactory{
公共静态映射属性=new HashMap();
受保护的静态最终SqlSessionFactory工厂;
公共MyBatisSqlSessionFactory(字符串用户ID、字符串密码){
试一试{
Properties props=新属性();
props.setProperty(“用户名”,userid);
props.setProperty(“密码”,password);
Reader=Resources.getResourceAsReader
(“wsnscc/mybatis/xml/Configuration.xml”);
}捕获(例外e){
抛出新的运行时异常(“致命错误。原因:”+e,e);
}
}
公共静态SqlSessionFactory getSqlSessionFactory
(字符串用户ID、字符串密码)
抛出运行时异常{
如果(工厂==null)
工厂=新的MyBatisSqlSessionFactory(用户ID、密码);
返回工厂;
}
}