@Test public void testConfiguration() { UnpooledDataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory(); Properties dataSourceProperties = new Properties(); dataSourceProperties.put("driver", "org.hsqldb.jdbcDriver"); dataSourceProperties.put("url", "jdbc:hsqldb:mem:xml_references"); dataSourceProperties.put("username", "sa"); dataSourceFactory.setProperties(dataSourceProperties); Environment environment = new Environment("test", new JdbcTransactionFactory(), dataSourceFactory.getDataSource()); Configuration configuration = new Configuration(); configuration.setEnvironment(environment); configuration.getTypeAliasRegistry().registerAlias(Person.class); configuration.addMapper(PersonMapper.class); configuration.addMapper(PersonMapper2.class); new DefaultSqlSessionFactory(configuration); }
private SqlSessionFactory getSqlSessionFactoryWithConstructor() { UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory(); Properties properties = new Properties(); properties.setProperty("driver", "org.hsqldb.jdbcDriver"); properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor"); properties.setProperty("username", "sa"); unpooledDataSourceFactory.setProperties(properties); Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource()); Configuration configuration = new Configuration(); configuration.setEnvironment(environment); configuration.addMapper(StudentConstructorMapper.class); configuration.addMapper(TeacherMapper.class); configuration.getMappedStatementNames(); configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE); return new DefaultSqlSessionFactory(configuration); }
@Override public SqlSessionFactory getObject() throws Exception { SqlSessionFactory sqlSessionFactory = super.getObject(); if (sqlSessionFactory instanceof DefaultSqlSessionFactory) return proxy(sqlSessionFactory, methodInvocation -> { if (methodInvocation.getMethod().getName().equals("openSession")) { SqlSession session = (SqlSession) methodInvocation.proceed(); if (session instanceof DefaultSqlSession) { DefaultSqlSession defaultSqlSession = (DefaultSqlSession) session; Executor executor = (Executor) ReflectionUtils.getFieldValue(defaultSqlSession, "executor"); if (executor instanceof CachingExecutor) { CachingExecutor cachingExecutor = (CachingExecutor) executor; SimpleExecutor ex = (SimpleExecutor) ReflectionUtils.getFieldValue(cachingExecutor, "delegate"); System.out.println("ex-> " + ex.getClass().getName()); ReflectionUtils.setDeclaredFieldValue(cachingExecutor, "delegate", proxy(ex, invocation -> { System.out.println("method->" + invocation.getMethod()); return invocation.proceed(); })); } } return session; } return methodInvocation.proceed(); }); return sqlSessionFactory; }
@Test(expected = BindingException.class) public void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() { Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment()); configuration.addMapper(AuthorMapperWithMultipleHandlers.class); SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration); SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession(); try { DefaultResultHandler handler1 = new DefaultResultHandler(); DefaultResultHandler handler2 = new DefaultResultHandler(); AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class); mapper.selectAuthor(101, handler1, handler2); } finally { sqlSession.close(); } }
@Test(expected = BindingException.class) public void shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds() { Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment()); configuration.addMapper(AuthorMapperWithRowBounds.class); SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration); SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession(); try { RowBounds bounds1 = new RowBounds(0, 1); RowBounds bounds2 = new RowBounds(0, 1); AuthorMapperWithRowBounds mapper = sqlSession.getMapper(AuthorMapperWithRowBounds.class); mapper.selectAuthor(101, bounds1, bounds2); } finally { sqlSession.close(); } }
@Test public void shouldUseDefaultId() throws Exception { Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multidb/MultiDbConfig.xml"); DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) new SqlSessionFactoryBuilder().build(reader); Configuration c = sqlSessionFactory.getConfiguration(); assertEquals("hsql", c.getDatabaseId()); }
@Test public void shouldUseProvider() throws Exception { Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multidb/ProviderConfig.xml"); DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) new SqlSessionFactoryBuilder().build(reader); Configuration c = sqlSessionFactory.getConfiguration(); assertEquals("translated", c.getDatabaseId()); }
public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); }
public SqlSessionFactory build(Configuration config) { // 源码解析: 创建SqlSession工厂, 传入Configuration对象 return new DefaultSqlSessionFactory(config); }
@Before public void instantiate() { factory = new JxSqlSessionFactory(new DefaultSqlSessionFactory(null)); }
public static void main(String[] args) throws Exception { TestH2 h2 = new TestH2(); // 开始服务 h2.startServer(); h2.testH2(); Class.forName("org.h2.Driver"); JdbcDataSource JdbcDataSource=new JdbcDataSource(); JdbcDataSource.setUrl("jdbc:h2:./test"); JdbcDataSource.setUser("sa"); JdbcDataSource.setPassword(""); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("Production", transactionFactory, JdbcDataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(PersonMapper.class); SqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration); SqlSession sqlSession = sqlSessionFactory.openSession(); PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); Person person=new Person(); person.setId(1); Person person1= personMapper.queryByPrimaryKey(person); System.out.println(person1); h2.stopServer(); }
/** * MyBatis内部通过Configuration对象来创建SqlSessionFactory, * 用户也可以自己通过API构造好Configuration对象,调用此方法创建SqlSessionFactory * @param config * @return */ public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); }