是否可以将mybatis-config.xml转换为Spring MVC的类文件?

时间:2018-12-13 16:01:07

标签: spring-mvc mybatis spring-mybatis

是否可以将mybatis-config.xml(文件类型为xml的sql-map-config)转换为Java类文件?

我尝试搜索,但只能找到mapper文件的类配置。

请让我知道这是否真的可以通过上课吗?

2 个答案:

答案 0 :(得分:1)

我不确定我是否理解您的问题,但是请尝试以下操作:

import org.apache.commons.dbcp.BasicDataSource;
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;
import org.apache.ibatis.type.TypeAliasRegistry;

public class MyBatisTest {

    private SqlSessionFactory   sqlSessionFactory;
    private Configuration       configuration;
    private SqlSession          sqlSession;

    /**
     * Call this method first
     */
    public void configure() throws Exception {

        DataSource ds = getDataSource();

        Environment env = new Environment("env", new JdbcTransactionFactory(), ds);

        configuration = new Configuration(env);

        configureMappers();

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
    }

    /**
     * Configure data source
     * @return
     */
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("driver.class.name");
        dataSource.setUrl("jdbc:db_connection_string");
        dataSource.setUsername("usr");
        dataSource.setPassword("pass");
        return dataSource;
    }

    /**
     * Configure all mappers
     */
    private void configureMappers() {

        TypeAliasRegistry aliases = configuration.getTypeAliasRegistry();

        aliases.registerAlias("myBean1Alias", MyBean1.class);

        configuration.addMapper(MyMapper1.class);

        // do the same for all bean and mapper classes

    }

    /**
     * Get SqlSession object
     * @return
     */
    public SqlSession getSession() {
        if (sqlSessionFactory == null) {
            throw Exception("...");
        }
        sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }

    /**
     * Close session
     */
    public void closeSession() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    public static void main(String[] args) {

        MyBatisTest test = new MyBatisTest();

        try {

            test.configure();

            SqlSession session = test.getSession();

            // do work
            MyMapper1 mapper = session.getMapper(MyMapper1.class);
            // etc.

        } catch (Exception e) {
            //
        } finally {
            test.closeSession();
        }

    }

}

答案 1 :(得分:1)

如果将mybatis与Spring Boot结合使用,请尝试使用MyBatis-Spring-Boot-Starter,然后可以在ConfigurationCustomizer中自定义自己的Configuration,如下所示:

// @Configuration class
@Bean
ConfigurationCustomizer mybatisConfigurationCustomizer() {
    return new ConfigurationCustomizer() {
        @Override
        public void customize(Configuration configuration) {
            // customize ...
        }
    };
}