发表于: 2018-04-01 17:14:14

1 569


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin) 

复习spring +mybatis 首先从applicationContext.xml开始

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

   <context:component-scan base-package="mybatis"/>

<!--&lt;!&ndash;不扫描 @Controller注解的类  &ndash;&gt;-->
   <!--<context:exclude-filter type="annotation"-->

   <!--expression="org.springframework.stereotype.Controller"/>-->


   <!-- 导入数据库配置文件 -->
   <!--<context:property-placeholder location="classpath:mybatis"/>-->

    <!-- 配置数据库连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/><!--druid可以不配置驱动-->
<property name="url" value="jdbc:mysql://localhost:3306/task1?useUnicode=true&amp;useSSL=true&amp;characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="135246" />


</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="mybatis.modle.User" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/UserMapper.xml"/>
</bean>

<!--Mybatis的Mapper接口识别-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="mybatis.mapper"/>

</bean>
</beans>


<context:annotation-config/>

<context:component-scan base-package="mybatis"/>

第一行是开启注解,第二行是扫描注解包。如果有第二行代码,第一行可以不写。

对于不同的层,spring提供了不同的注解。

  • dao层使用 @Repository,可以在其后添加("")来指定bean的名字(即name或者id)
  • service层使用 @Service,可以在其后添加("")来指定bean的名字(即name或者id)
  • controller层使用 @Controller,可以在其后添加("")来指定bean的名字(即name或者id)
  • 其他情况下可以使用 @Component,可以在其后添加("")来指定bean的名字(即name或者id)
注意,以上注解都是在接口的实现类进行。并且,如果没有指定名字,系统会默认起名,规则是:取类名,将第一个字母转换为小写。

使用以上的方法,可以完成将bean在xml的注册。

但是,如果在某一类当中,我们需要使用bean时,怎么将xml当中的bean注入到类当中呢?这里要涉及到bean的自动装配策略。相关的注释主要有@Resource和@Autowired。


Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发。

@Repository 注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。

具体只需将该注解标注在 DAO 类上即可。然后在配置文件中启动自动扫描就可以被扫描到了。


通过在类上使用 @Repository、@Component、@Service 和 @Constroller 注解,

Spring 会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中

。这些类就成了 Spring 受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与 @Repository 是完全相同的。


当一个 Bean 被自动检测到时,会根据那个扫描器的 BeanNameGenerator 策略生成它的 bean 名称。默认情况下,对于包含 name 属性的 @Component、@Repository、 @Service 和 @Controller,会把 name 取值作为 Bean 的名字。如果这个注解不包含 name 值或是其他被自定义过滤器发现的组件,默认 Bean 名称会是小写开头的非限定类名。


自动装配是指,Spring 在装配 Bean 的时候,根据指定的自动装配规则,将某个 Bean 所需要引用类型的 Bean 注入进来。<bean> 元素提供了一个指定自动装配类型的 autowire 属性,该属性有如下选项:

  • no -- 显式指定不使用自动装配。
  • byName -- 如果存在一个和当前属性名字一致的 Bean,则使用该 Bean 进行注入。如果名称匹配但是类型不匹配,则抛出异常。如果没有匹配的类型,则什么也不做。
  • byType -- 如果存在一个和当前属性类型一致的 Bean ( 相同类型或者子类型 ),则使用该 Bean 进行注入。byType 能够识别工厂方法,即能够识别 factory-method 的返回类型。如果存在多个类型一致的 Bean,则抛出异常。如果没有匹配的类型,则什么也不做。
  • constructor -- 与 byType 类似,只不过它是针对构造函数注入而言的。如果当前没有与构造函数的参数类型匹配的 Bean,则抛出异常。使用该种装配模式时,优先匹配参数最多的构造函数。
  • autodetect -- 根据 Bean 的自省机制决定采用 byType 还是 constructor 进行自动装配。如果 Bean 提供了默认的构造函数,则采用 byType;否则采用 constructor 进行自动装配。

当使用 byType 或者 constructor 类型的自动装配的时候,自动装配也支持引用类型的数组或者使用了泛型的集合,这样,Spring 就会检查容器中所有类型匹配的 Bean,组成集合或者数组后执行注入。对于使用了泛型的 Map 类型,如果键是 String 类型,则 Spring 也会自动执行装配,将所有类型匹配的 Bean 作为值,Bean 的名字作为键。

SqlsessionFactory设置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
<property name="typeAliasesPackage" value="mybatis.modle.User" /> 
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/UserMapper.xml"/>
</bean>


<!--Mybatis的Mapper接口识别-->配置扫描器,扫描指定路径的mapper生成数据库操作代理类

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="mybatis.mapper"/>

</bean>

从base 包中搜索所有下面所有 interface,并将其注册到 Spring Bean容器中,其注册的class bean是MapperFactoryBean。

1: 了解Spring初始化Bean过程的人可能都知道,Spring 首先会将需要初始化的所有class先通过BeanDefinitionRegistry进行注册,并且将该Bean的一些属性信息(如scope、className、beanName等)保存至BeanDefinitionHolder中;Mybatis这里首先会调用Spring中的ClassPathBeanDefinitionScanner.doScan方法,将所有Mapper接口的class注册至BeanDefinitionHolder实例中,然后返回一个Set,其它包含了所有搜索到的Mapper class BeanDefinitionHolder对象。

#2: 首先,由于 #1 中注册的都是接口class, 可以肯定的是接口是不能直接初始化的;实际 #2 中for循环中替换当前所有 holder的 className为 MapperFactoryBean.class,并且将 mapper interface的class name setter 至 MapperFactoryBean 属性为 mapperInterface 中,也就是 #3 代码所看到的。

再看 MapperFactoryBean,它是直接实现了 Spring 的 FactoryBean及InitializingBean 接口。其实既使不看这两个接口,当看MapperFactoryBean的classname就知道它是一个专门用于创建 Mapper 实例Bean的工厂。

至此,已经完成了Spring与mybatis的整合的初始化配置的过程。

出自Mybatis+Spring 基于接口编程的原理 http://ju.outofmemory.cn/entry/62780


另一篇原理 https://www.jianshu.com/p/fc23c94fc439

明天计划的事情:(一定要写非常细致的内容) 

提交任务1
遇到的问题:(遇到什么困难,怎么解决的) 
收获:(通过今天的学习,学到了什么知识)


返回列表 返回列表
评论

    分享到