![](/img/trans.png)
[英]How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application?
[英]MyBatis - how to set database properties from swing application?
假設我有一個秋千應用,而我正在使用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工廠。
您可以通過將類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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.