淮北建设工程质量安全站网站,免费网站建设必找186一6159一6345,网站 微站建设排名,寿光网站建设推广将mybatis与spring进行整合#xff0c;主要解决的问题就是讲SqlSessionFactory对象交由spring来管理#xff0c;所以#xff0c;该整合#xff0c;只需要将SqlSessionFactory的对象生成器SqlSessionFactoryBean注册在spring容器中#xff0c;再将其注入给Dao的实现类即可完…将mybatis与spring进行整合主要解决的问题就是讲SqlSessionFactory对象交由spring来管理所以该整合只需要将SqlSessionFactory的对象生成器SqlSessionFactoryBean注册在spring容器中再将其注入给Dao的实现类即可完成整合实现spring与mybatis的整合常用的方式扫描的Mapper动态代理. spring就像插线板一样mybatis框架是插头可以很容易的组合到一起。mybatis插头插入spring插线板就是一个整体。 我们需要spring创建以下对象 1.独立的连接池对象 2.SqlSessionFactory对象 3.创建出dao对象 上面三个对象的创建使用xml的bean标签
1.Maven配置
dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.0/version
/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion2.0.0/version
/dependency
将mybatis集成到spring之后就可以被spring的ioc容器托管再也不用自己创建SqlSessionFactory 、打开SqlSession等操作。其中最重要的配置就是定义好SqlSessionFactoryBean。mybatis通过SqlSessionFactoryBean将SqlSessionFactory对象集成到spring中它实现了InitializingBean接口在SqlSessionFactoryBean初始化时解析配置并创建DefaultSqlSessionFactory对象。它还实现了FactoryBean在getObject时返回我们创建好的DefaultSqlSessionFactory使得DefaultSqlSessionFactory也被spring管理起来。
很多框架集成到spring的方法基本都是靠InitializingBean和FactoryBean这两个接口来实现的很好的设计
2.集成到spring中
有两种方式
第一种方式自定义类 /*** Mybatis会话工厂* Mybatis Session Factory* 声明mybatis中提供的SqlSessionFactoryBean类这个类内部创建SqlSessionFactory*/Beanpublic SqlSessionFactoryBean mybatisSqlSessionFactoryBean(DataSource dataSource) throws IOException {//dataSourceSqlSessionFactoryBean bean new SqlSessionFactoryBean();bean.setDataSource(dataSource);//resolverPathMatchingResourcePatternResolver resolver new PathMatchingResourcePatternResolver();bean.setMapperLocations(resolver.getResources(classpath*:mapper/**/*Mapper.xml));//configuration配置类相当于mybatis的主配置文件configuration.xmlorg.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文件*/Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc new MapperScannerConfigurer();msc.setBasePackage(org.techwebsite.logic.mapper);return msc;}
pagehelper官方
Releases · pagehelper/Mybatis-PageHelper (github.com)
PageHelper分页使用详解_笔记大全_设计学院
用的当时的最新版6.0.0 第二种方式xml配置文件 mybatis主配置文件 configuration.xml
?xml version1.0 encodingUTF-8?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtdconfiguration!--settings:控制mybatis全局行为--settings!-- 懒加载开启--setting namelazyLoadingEnabled valuetrue/!-- 积极加载关闭 --setting nameaggressiveLazyLoading valuefalse/!--设置mybatis输出日志--setting namelogImpl valueSTDOUT_LOGGING//settings!--类别名--typeAliases!-- 批量定义别名告诉mybatis要扫描的包 --!-- mybatis会自动扫描这个包下的所有类给所有的类生成别名生成的别名就是这个类的简单类名首字母可以是大写/小写 --package namecom.example.eshop_manager.entity//typeAliases
/configuration
applicationContext.xml Spring核心配置文件 !-- 开启包扫描 --
!-- 扫描这个包下所有的相关注解需要被bean容器池管理的会自动帮你创建对象 --
context:component-scan base-packagecom.example.eshop_manager/context:component-scan!-- 读取属性文件 --
context:property-placeholder locationclasspath:jdbc.properties/context:property-placeholder!-- 定义数据源使用c3p0 --
bean iddbSource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass value${jdbc.driver}/propertyproperty namejdbcUrl value${jdbc.url}/propertyproperty nameuser value${jdbc.username}/propertyproperty namepassword value${jdbc.password}/property
/bean!-- 声明mybatis中提供的SqlSessionFactoryBean类这个类内部创建SqlSessionFactory --
bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- 数据源set注入把数据库连接池赋给了dataSource属性 --property namedataSource refdbSource/property!-- 加载mybatis主配置文件的位置因为是在spring中指定mybatis主配置文件所以要用classpath:configLocation:属性是Resource类型的负责读取配置文件赋值使用 value 指定文件的路径 --property nameconfigLocation valueclasspath:mybatis/configuration.xml /!-- 自动扫描XxxMapping.xml文件**任意路径 --property namemapperLocations valueclasspath*:com/tgq/**/mapper/*.xml/!--配置pagehelper插件--!-- PageHelper 配置pagehelper.helperDialectmysqlpagehelper.reasonabletruepagehelper.supportMethodsArgumentstruepagehelper.paramscountcountSql--property namepluginsarraybean classcom.github.pagehelper.PageInterceptorproperty namepropertiesvaluehelperDialectmysql/value/property/bean/array/property
/bean!-- mapper扫描从mapper包中扫描出mapper接口
自动创建代理对象并且在spring的bean容器池中管理,遵循规范将mapper.java和mapper.xml映射文件名称保持一致且在一个目录 中,自动扫描出来的mapper的bean的id为mapper类名首字母小写
MapperScannerConfigurer:在内部调用SqlSession的getMapper()生成每个dao接口的代理对象
--
bean classorg.mybatis.spring.mapper.MapperScannerConfigurer!-- 指定扫描的包名,如果扫描多个包每个包中间使用半角逗号分隔
MapperScannerConfigurer会扫描这个包中所有的接口把每个接口都执行一次getMapper()方法
得到每个接口的dao对象创建好的dao对象放入spring容器中创建好的dao对象名称就是接口名首字母小写例如studentDao--property namebasePackage valuecom.example.eshop_manager.dao/property namesqlSessionFactoryBeanName valuesqlSessionFactory/
/bean 参考
分页配置org.mybatis.spring.SqlSessionFactoryBean.setPlugins()方法的使用及代码示例_其他_大数据知识库
【MyBatis】spring整合mybatis教程详细易懂_无法自律的人的博客-CSDN博客