gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:45:22 25 4
gpt4 key购买 nike

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

感谢您的建议。

最佳答案

您将变量传递到 SQL 工厂。

您可以通过将类 MyBatis SQLSessionFactory 更改为如下内容来实现此目的:

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

关于java - MyBatis - 如何从 swing 应用程序设置数据库属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6084169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com