1、读取操作系统设置的环境变量信息
String hais_db_host = System.getenv("hais_db_host");
String hais_db_port = System.getenv("hais_db_port");
String hais_db_name = System.getenv("hais_db_name");
String hais_db_user_name = System.getenv("hais_db_user_name");
String hais_db_password = System.getenv("hais_db_password");
String url = "jdbc:mysql://" + hais_db_host + ":" + hais_db_port + "/" + hais_db_name
+ "?characterEncoding=UTF-8";
2、模拟创建连接池,通过拼接方式连接数据库
PooledDataSource pooledDataSource = new PooledDataSource("com.mysql.jdbc.Driver", url, hais_db_user_name,
hais_db_password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("cybatis", transactionFactory, pooledDataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(SQLMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
public static SqlSession getSession() throws IOException {
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
SqlSession sqlSession = getSession();
SQLMapper sqlMapper = (SQLMapper) sqlSession.getMapper(SQLMapper.class);
List<String> results= sqlMapper.getResults();