MyBatis - 如何从 swing 应用程序设置数据库属性?
假设我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将变量传递到 SQL 工厂。
您可以通过将类
MyBatisSQLSessionFactory
更改为如下内容来实现此目的:You pass the variables into the SQL factory.
You can do this by changing the class
MyBatisSQLSessionFactory
into something like this: