Beispiel #1
0
 public SqlSession getSession() throws Exception {
   String resource = "myBatis-configuration.xml";
   Reader reader = Resources.getResourceAsReader(resource);
   SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
   SqlSessionFactory factory = builder.build(reader);
   return factory.openSession();
 }
  private SqlSessionFactory getSqlSessionFactory(
      String componentName, String environment, boolean isRead) {
    String ibatisConfigFile = componentName + IBATIS_CONFIG_SUFFIX;
    String ibatisConfigProperties = componentName + IBATIS_PROPERTIES_SUFFIX;
    // use componentName + "::" + environment as the key in void that environment conflict in the
    // ibatis runtime
    String environmentKey = componentName + "::" + environment;

    Map<String, SqlSessionFactory> tempSqlSessionFactoryMap = sqlSessionFactoryMap;

    if (!tempSqlSessionFactoryMap.containsKey(environmentKey)) {
      synchronized (LOCK) {
        if (!tempSqlSessionFactoryMap.containsKey(environmentKey)) {
          try {
            LOG.info(String.format("build datasource:%s", environmentKey));
            long start = System.currentTimeMillis();
            org.apache.ibatis.session.SqlSessionFactoryBuilder builder =
                new org.apache.ibatis.session.SqlSessionFactoryBuilder();
            reader = Resources.getResourceAsReader(ibatisConfigFile);
            Configuration configuration =
                ConfigurationManager.getInstance().getByFileName(ibatisConfigProperties);
            SqlSessionFactory factory =
                builder.build(reader, environment, configuration.toProperties());
            LOG.info(
                "Init database connection pool "
                    + factory.getConfiguration().getVariables().toString());
            tempSqlSessionFactoryMap.put(environmentKey, factory);

            long end = System.currentTimeMillis();
            if (end - start > 1000) {
              LOG.info(
                  String.format("build datasource %s cost %d ms", environmentKey, (end - start)));
            }
            return factory;
          } catch (Exception e) {
            e.printStackTrace();
            LOG.error(
                "build environment error " + environmentKey + " with error " + e.getMessage());
            return null;
          } finally {
            try {
              reader.close();
            } catch (IOException e) {
              LOG.error(e.getMessage());
            }
          }
        }
      }
    }
    return tempSqlSessionFactoryMap.get(environmentKey);
  }
Beispiel #3
0
  public static void main(String[] args) throws IOException {

    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

    InputStream inputStream = Resources.getResourceAsStream("mybatis/mybatis_config.xml");
    SqlSessionFactory factory = builder.build(inputStream);
    SqlSession session = factory.openSession();

    MemberMapper mapper = session.getMapper(MemberMapper.class);
    List<Member> list = mapper.selectAll();

    for (Member m : list) System.out.println(m.getId() + " " + m.getEmail());

    Member m = mapper.selectById(1001);
    System.out.println(m.getId() + " " + m.getEmail());
  }