我正在尝试使用MyBatis将小型Java应用程序与数据库连接。
XML文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@xxxx:xxxx:xxxx"/>
<property name="username" value="xxxxx"/>
<property name="password" value="xxxxx"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="xml/Mapper.xml"/>
</mappers>
</configuration>
之前,我得到了如下的会议
String resource = "Configuration.xml";
SqlSession session = null;
try{
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
session = sqlMapper.openSession();
我想在不使用xml文件的情况下连接到数据库。任何帮助将不胜感激。
答案 0 :(得分:0)
你查看过MyBatis 3用户指南了吗?有一节名为“构建SqlSessionFactory” 没有XML“。也没有必要使用基于XML的映射,你可以使用基于注释的语句映射并完全避免XML配置。
答案 1 :(得分:0)
这对我有用:
import javax.sql.DataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
String user = "xxxxx";
String password = "xxxxx";
String databasenameURL = "jdbc:oracle:thin:@xxxx:xxxx:xxxx";
String dbDriver = "oracle.jdbc.driver.OracleDriver";
DataSource dataSource = new org.apache.ibatis.datasource.pooled.PooledDataSource(
dbDriver, databasenameURL, user, password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development",
transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(configuration);
答案 2 :(得分:0)
也许为时已晚,但对于未来的读者来说。
您可以将xml解析为字符串,并在以后修改所需的任何值。
这对我有用:
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.CharSequenceReader;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Reader;
public class MySqlSessionFactory
{
private static Logger log = Logger.getLogger(MySqlSessionFactory.class);
private static SqlSessionFactory sessionFactory;
public static SqlSessionFactory getSqlSessionFactory()
{
return sessionFactory;
}
/**
* Initialize SqlSessionFactory instance, to be used later in all the project
*
* @return boolean if sqlSessionFactory is build
*/
public static boolean initializeMySqlSessionFactory() throws IOException
{
log.trace("Enter method initializeMySqlSessionFactory.");
String resource = "mybatis-config.xml";
boolean result;
Reader reader = null;
Reader parsedReader = null;
try
{
reader = Resources.getResourceAsReader(resource);
String parsedXMLConfig = IOUtils.toString(reader);
parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${jdbcUrl}", "databaseURL");
parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${username}", "databaseUser");
parsedXMLConfig = StringUtils.replace(parsedXMLConfig, "${password}", "databasePassword");
parsedReader = new CharSequenceReader(parsedXMLConfig);
sessionFactory = new SqlSessionFactoryBuilder().build(parsedReader);
result = true;
}
catch(IOException e)
{
result = false;
log.error("Error calling initializeMySqlSessionFactory.", e);
throw new IOException(e);
}
finally
{
if(reader != null )
{
reader.close();
}
if(parsedReader != null )
{
parsedReader.close();
}
}
log.trace("Exit method initializeMySqlSessionFactory. Method result: " + result);
return result;
}
}
也可以通过这种方式更改您想要的任何指定字段...,并注意将相同的占位符放入xml文件($ {jdbcUrl},$ {username},$ {password})