65、SSM框架使用笔记

版权声明:本文为博主原创,转载请注明出处。 https://blog.csdn.net/u011848397/article/details/72833621

目录:

1、Spring扫描组件<context:component-scan base-package=" "/>的作用
2、@Autowired@Resource的区别
3、spring与mybatis三种整合方法


1、Spring扫描组件<context:component-scan base-package=" "/>的作用

http://blog.csdn.net/you18131371836/article/details/53691044
实现bean的自动载入:
在base-package指明一个包:

<context:component-scan base-package="com.liao.model"/>

表明com.liao.model包及其子包中,如果某个类的头上带有特定的注解
@Component,@Repository,@Service,@Controller,就会将这个对象作为Bean注入进spring容器。
<context:component-scan>提供两个子标签:<context:include-filter><context:exclude-filter>各代表引入和排除的过滤。
注:@Component是所有受Spring管理组件的通用形式。而@Repository、@Service和 @Controller则是@Component的细化,用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说,你能用@Component来注解你的组件类,但如果用@Repository、@Service 或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。例如,这些典型化注解可以成为理想的切入点目标。


2、@Autowired@Resource的区别

http://www.cnblogs.com/think-in-java/p/5474740.html
@Resource@Autowired都是做bean的注入时使用
@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入。


3、spring与mybatis三种整合方法

http://www.cnblogs.com/wangmingshun/p/5674633.html
目前使用第一种,在结构上与hibernate最相似:

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件,**表示迭代查找 -->
        <property name="mapperLocations" value="classpath:com/hua/saf/**/*.xml" />
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 ,包下的类需要使用@MapperScan注解,否则容器注入会失败 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hua.saf.*" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

猜你喜欢

转载自blog.csdn.net/u011848397/article/details/72833621