将mybatis与spring进行整合,主要解决的问题就是讲SqlSessionFactory对象交由spring来管理,所以,该整合,只需要将SqlSessionFactory的对象生成器SqlSessionFactoryBean注册在spring容器中,再将其注入给Dao的实现类即可完成整合,实现spring与mybatis的整合常用的方式:扫描的Mapper动态代理.
spring就像插线板一样,mybatis框架是插头,可以很容易的组合到一起。mybatis插头插入spring插线板就是一个整体。
我们需要spring创建以下对象
1.独立的连接池对象
2.SqlSessionFactory对象
3.创建出dao对象
上面三个对象的创建,使用xml的bean标签
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.0version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.0version>
- dependency>
将mybatis集成到spring之后,就可以被spring的ioc容器托管,再也不用自己创建SqlSessionFactory 、打开SqlSession等操作。其中最重要的配置就是定义好SqlSessionFactoryBean。 mybatis通过SqlSessionFactoryBean将SqlSessionFactory对象集成到spring中,它实现了InitializingBean接口,在SqlSessionFactoryBean初始化时解析配置并创建DefaultSqlSessionFactory对象。它还实现了FactoryBean,在getObject时返回我们创建好的DefaultSqlSessionFactory,使得DefaultSqlSessionFactory也被spring管理起来。 很多框架集成到spring的方法基本都是靠InitializingBean和FactoryBean这两个接口来实现的,很好的设计
有两种方式
- /**
- * Mybatis会话工厂
- * Mybatis Session Factory
- * 声明mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory
- */
- @Bean
- public SqlSessionFactoryBean mybatisSqlSessionFactoryBean(DataSource dataSource) throws IOException {
- //dataSource
- SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
- bean.setDataSource(dataSource);
- //resolver
- PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
- bean.setMapperLocations(resolver.getResources("classpath*:mapper/**/*Mapper.xml"));
- //configuration配置类相当于mybatis的主配置文件configuration.xml
- org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
- configuration.setCacheEnabled(true);
- configuration.setLazyLoadingEnabled(false);//懒加载:关闭
- configuration.setMapUnderscoreToCamelCase(true);//mybatis驼峰映射
- configuration.setLogImpl(Log4jImpl.class);
- bean.setConfiguration(configuration);
- //自定义别名,mybatis会自动扫描这个包下的所有类,给所有的类生成别名,生成的别名就是这个类的简单类名(首字母可以是大写/小写)
- bean.setTypeAliasesPackage("org.techwebsite.domain");//自定义别名
- /***pagehelper分页插件*****/
- Interceptor interceptor = new PageInterceptor();
- Properties properties = new Properties();
- properties.setProperty("helperDialect", "mysql");//数据库
- properties.setProperty("offsetAsPageNum", "true");//是否将参数offset作为PageNum使用
- properties.setProperty("rowBoundsWithCount", "true");//是否进行count查询
- properties.setProperty("reasonable", "false");//是否分页合理化
- interceptor.setProperties(properties);
- //添加插件
- bean.setPlugins(new Interceptor[]{interceptor});
- return bean;
- }
-
-
- /**
- * MapperScannerConfigurer:在内部调用SqlSession的getMapper()生成每个dao接口的代理对象
- * 或者使用注解@MapperScan(basePackages = "org.techwebsite.logic.mapper")//扫描mybatis的mapper文件
- */
- @Bean
- public MapperScannerConfigurer mapperScannerConfigurer(){
- MapperScannerConfigurer msc = new MapperScannerConfigurer();
- msc.setBasePackage("org.techwebsite.logic.mapper");
- return msc;
- }
pagehelper官方:
Releases · pagehelper/Mybatis-PageHelper (github.com)
用的当时的最新版6.0.0
mybatis主配置文件
configuration.xml
- "1.0" encoding="UTF-8"?>
- configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
-
- <configuration>
-
- <settings>
-
- <setting name="lazyLoadingEnabled" value="true"/>
-
- <setting name="aggressiveLazyLoading" value="false"/>
-
- <setting name="logImpl" value="STDOUT_LOGGING"/>
-
- settings>
-
-
- <typeAliases>
-
-
- <package name="com.example.eshop_manager.entity"/>
- typeAliases>
- configuration>
applicationContext.xml (Spring核心配置文件)
-
- <context:component-scan base-package="com.example.eshop_manager">context:component-scan>
-
-
- <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
-
- <bean id="dbSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driver}">property>
- <property name="jdbcUrl" value="${jdbc.url}">property>
- <property name="user" value="${jdbc.username}">property>
- <property name="password" value="${jdbc.password}">property>
- bean>
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-
- <property name="dataSource" ref="dbSource">property>
-
- <property name="configLocation" value="classpath:mybatis/configuration.xml" />
-
- <property name="mapperLocations" value="classpath*:com/tgq/**/mapper/*.xml"/>
-
-
-
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageInterceptor">
- <property name="properties">
- <value>
- helperDialect=mysql
- value>
- property>
- bean>
- array>
- property>
- bean>
-
-
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-
- <property name="basePackage" value="com.example.eshop_manager.dao"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- bean>
参考:
分页配置:org.mybatis.spring.SqlSessionFactoryBean.setPlugins()方法的使用及代码示例_其他_大数据知识库