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

发布于 2024-11-09 02:15:23 字数 1418 浏览 0 评论 0原文

假设我有 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 的配置中?

感谢您的任何建议。

Let's say I have swing app and I am using 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 looks like this:

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;
    }
}

How can I get variables from swing application into configuration of sqlfactory?

Thanks for any advices.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩于岁月 2024-11-16 02:15:23

您将变量传递到 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;
    }
}

You pass the variables into the SQL factory.

You can do this by changing the class MyBatisSQLSessionFactory into something like this:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文