Mybatis Starting基于Spring JavaConfig

今天我们来继续学习使用mybatis-spring来基于spring容器环境的JavaConfig方式配置mybatis。

JavaConfig

收下摘自网上别人的理解,细细品读:

从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。但是,仍然允许使用经典的XML方式来定义bean和配置,JavaConfig是另一种替代解决方案。所以,在Spring3以后的版本中,支持xml方式和javaConfig两种Spring配置方式。
优点:
在XML和直接注解在一个Bean类之间有一个有趣的选择,它就是JavaConfig,它是从V3.0后嵌入到Spring里的一个以前的独立项目。它用Java的编译时检查整合了XML的去耦的优势。JavaConfig能够等价看成是XML文件,不过它只是用Java编写的。

过多的话就不说了,代码贴上:

使用JavaConfig方式配置Mybatis

DataConfig.java

package org.wbw.mybatisplus.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.wbw.mybatisplus.handler.BlogTypeHandler;

@Configuration
@MapperScan(basePackages = "org.wbw.mybatisplus.mapper")
public class DataConfig {

    // DS
    @Bean
    public BasicDataSource dataSource(){
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8");//防止乱码
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setInitialSize(5);
        dataSource.setMaxActive(10);
        return dataSource;
    }

    // spring jdbc transaction manager
    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    // sqlSessionFactory
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setTypeAliasesPackage("org.wbw.mybatisplus.entity");

        // typehandler
        BlogTypeHandler[] blogTypeHandlers = {new BlogTypeHandler()};
        sessionFactory.setTypeHandlers(blogTypeHandlers);

        sessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver()
                        .getResources("classpath:org/wbw/mybatisplus/mapper/*.xml"));
        return sessionFactory;
    }
}

MybatisSpringJavaConfigStarter.java

package org.wbw.mybatisplus;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.wbw.mybatisplus.config.DataConfig;
import org.wbw.mybatisplus.entity.Blog;
import org.wbw.mybatisplus.mapper.BlogMapper;

/**
 *
 *
 */
public class MybatisSpringJavaConfigStarter {
    public static void main(String[] args) {


        ApplicationContext context = new AnnotationConfigApplicationContext(DataConfig.class);

        BlogMapper blogMapper = context.getBean(BlogMapper.class);
        Blog blog = blogMapper.selectBlog(6);
        System.out.println(blog);
    }
}

你可能感兴趣的:(Mybatis,spring)