美食优秀设计网站,用html制作淘宝网页,南靖网站建设,龙岩网站设计制作本文会以 mybatis 为例#xff0c;通过对比 mybatis-spring 和 mybatis-spring-boot-starter 代码示例#xff0c;了解 Starter 的作用。并对 mybatis-spring-boot-starter 进行简单剖析#xff0c;了解 Starter 原理。 下面还有投票#xff0c;一起参与进来吧#x1f44d… 本文会以 mybatis 为例通过对比 mybatis-spring 和 mybatis-spring-boot-starter 代码示例了解 Starter 的作用。并对 mybatis-spring-boot-starter 进行简单剖析了解 Starter 原理。 下面还有投票一起参与进来吧 文章目录 前言什么是 StarterStarter 的作用spring 整合组件spring-boot 整合组件 Starter 原理 前言
有没有在入行后直接基于 SpringBoot 开发项目没有 spring、servlet 开发经历的举个手。
有没有用 SpringBoot 开发项目但是第一次听说 Starter 或者听过却不知道是干嘛的举个手。
有没有知道 Starter 是干嘛的但不知其原理的举个手。
有没有想了解 Starter 原理或想自己实现一个 Starter 提供别人使用的举个手。
如果有以上情况的希望通过本文可以帮助你了解 Starter 。
什么是 Starter
大家都知道基于 SpringBoot 开发项目可以简化 Spring 应用的搭建以及开发过程提高程序员开发效率这是由于其“约定大约配置”的策略及其自动装配的特点。 约定大约配置是指 SpringBoot 指定了特定的方式进行配置application.properties/yam/yaml开发人员不需要像在 Spring 框架开发时定义配置文件。 自动装配是指在使用某个组件或框架时需要引用其依赖、配置类、配置文件等工作时SpringBoot 帮我们做了这些工作。 那跟 Starter 有关系吗答案是有 Starter 就是自动装配的具体实现其就是一个 maven 项目对某个组件的依赖、配置进行管理。通过导入 “Starter” 模块更容易使用这个组件。
Starter 的作用
我们通过对比 mybatis-spring 和 mybatis-spring-boot-starter 代码示例了解 Starter 的作用。
spring 整合组件
先看下在 spring 项目中如何使用 mybatis 的。大概有以下几个步骤
引入 spring、mybatis、jdbc 等相关依赖。创建 mybatis-config.xml 配置文件。 声明数据源 DataSource。声明 SqlSessionFactoryBean。声明 MapperScannerConfigurer。声明等等配置。 编写 xxxMapper.xml 及 xxMapper.java 文件。业务编码调用。
相关依赖
dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.2.5.RELEASE/version
/dependency
dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.1/version
/dependency
dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion1.3.0/version
/dependency
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.9/version
/dependency
dependencygroupIdxxx/groupIdartifactIdxxxx/artifactIdversionxxx/version
/dependency
...mybatis-config.xml 配置文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
configurationcontext:property-placeholder locationclasspath:jdbc.properties/settingssetting namelogImpl valueSTDOUT_LOGGING//settingsmapperspackage namecom.xxx.dao//mappersbean idmyDataSource classcom.alibaba.druid.pool.DruidDataSource init-methodinit destroy-methodcloseproperty nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//beanbean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource /property namemapperLocations valueclasspath:xxxx/*.xml//beanbean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.xxx.dao//beanbean class.xxxxxx.xxx!-- 指定SqlSessionFactory对象的名称 --property namesqlSessionFactoryBeanName valuefactory/!-- 指定基本包dao接口所在的包名 --property namebasePackage valuecom.xxx.dao//beanbean class.xxxxxx.xxx.../bean/configuration业务编码调用
Autowired
private XxxDao xxxDao;xxxDao.insert(xx);作为一个开发人员是否觉得很麻烦答案一定是的如果稍不留神少了哪个配置或依赖那就排查问题吧。
spring-boot 整合组件
这时候我们如果用基于 SpringBoot 开发那 mybatis-spring-boot-starter 就可以帮助我们做这些事。
那我们继续看下在 SpringBoot 项目中如何使用 Mybatis 的。大概有以下几个步骤
引入 mybatis-spring-boot-starter 依赖。application.properties 文件中添加相关配置。编写 xxxMapper.xml 及 xxMapper.java 文件。业务编码调用。
引入 mybatis-spring-boot-starter 依赖
dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.1.1/version
/dependencyapplication.properties 文件中添加相关配置。 spring.datasource.usernamex********spring.datasource.password********spring.datasource.url********spring.datasource.driver-class-name********mybatis.mapper-locationsclasspath:mapper/*.xml编写 xxxMapper.xml 及 xxMapper.java 文件
Mapperpublic interface XXXMapper {ListXXX list(xxx);}业务编码调用
Autowired
private XxxDao xxxDao;xxxDao.insert(xx);通过以上的代码比较可以明显的感觉到利用 Starter 后我们编写的代码更少了特别是 1、2 步骤这就是 Starter 的作用。 mybatis-spring-boot-starter 帮助我们做了以下几件事
整合了组件相关的依赖使我们直接引入 mybatis-spring-boot-starter 依赖即可也避免了版本冲突问题。自动发现存在的 DataSource做到自动配置。帮我们创建并注册SqlSessionFactory、SqlSessionTemplate等减少了配置类、配置项。自动扫描映射器Mapper注入到 Spring Bean 中。
Starter 原理
那 mybatis-spring-boot-starter 是如何做这些事的我们扒开裤子看个究竟。
首先看 mybatis-spring-boot-starter 项目结构其只有一个pom.xml文件文件中已经帮我们引入相关依赖跟上面 Spring 整合 Mybatis 的依赖是不是差不多。 其中有一个 mybatis-spring-boot-autoconfigure 依赖我们看下其项目结构 其通过 SPI 机制引入了 MybatisAutoConfiguration 配置类该类帮我们做了以下几件事 发现存在的 DataSource 并注入配置。 注册 SqlSessionFactory、SqlSessionTemplate 到 Spring 容器中。 内部类 AutoConfiguredMapperScannerRegistrar 扫描存在 Mapper 注解类转化为 BeanDefinition 并注册到 Spring 容器中。