package com.hao.config; import com.github.pagehelper.PageHelper; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.util.Properties; /** * Created by user on 2016/2/24. */ @Configuration @EnableTransactionManagement @MapperScan(basePackages = "com.hao.mapper",annotationClass = Repository.class) public class MyBatisConfig implements ApplicationContextAware{ private ApplicationContext context; @Autowired private DataSource dataSource; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.hao.model"); factoryBean.setMapperLocations(context.getResources("classpath:/mybatis/*.xml")); PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("reasonable","true"); properties.setProperty("supportMethodsArguments","true"); properties.setProperty("returnPageInfo","check"); properties.setProperty("params","count=countSql"); pageHelper.setProperties(properties); factoryBean.setPlugins(new Interceptor[]{pageHelper}); return factoryBean.getObject(); } @Bean(name = "transactionManager") public DataSourceTransactionManager transactionManager(){ DataSourceTransactionManager manager = new DataSourceTransactionManager(); manager.setDataSource(dataSource); return manager; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }