2011-05-21 31 views
1

比方说,我有Swing应用程序,我使用的MyBatis:MyBatis - 如何从swing应用程序设置数据库属性?

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的配置?

感谢您的任何建议。

回答

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; 
    } 
} 
相关问题