Spring整合hibernate的applicationContext.xml配置

1.注解方式的实现

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xmlns:aop="http://www.springframework.org/schema/aop"

     xmlns:tx="http://www.springframework.org/schema/tx"

     xmlns:context="http://www.springframework.org/schema/context"

     xsi:schemaLocation="

         http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://www.springframework.org/schema/tx

         http://www.springframework.org/schema/tx/spring-tx.xsd

         http://www.springframework.org/schema/aop

         http://www.springframework.org/schema/aop/spring-aop.xsd

         http://www.springframework.org/schema/context

         http://www.springframework.org/schema/context/spring-context.xsd">

    

    

      

        

            classpath:jdbc.properties

        

      

    

    

        class="com.mchange.v2.c3p0.ComboPooledDataSource">

        

        

        

        

        

        

        

        

                    

    

        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        

        

            

                com.test.bean

            

        

        

            

                ${dialect}

                ${show_sql}

                ${format_sql}

                ${use_sql_comments}

                ${hbm2ddl.auto}

            

        

    

    

        class="org.springframework.orm.hibernate4.HibernateTransactionManager">

        

    

    

        

            

            

        

    

    

        

        

    

2.xml方式的实现

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xmlns:aop="http://www.springframework.org/schema/aop"

  xmlns:tx="http://www.springframework.org/schema/tx"

  xsi:schemaLocation="跟上面一样">          

    

    

      

      

    

    

      

      

      

      

      

      

      

      

         

    

    

      

      

          

            /com/cdzg/spring/bean/User.hbm.xml

          

      

        

                ${dialect}

                ${hbm2ddl.auto}

                ${show_sql}

                ${format_sql}

                ${use_sql_comments}

            

      

     

    

    

      

       

    

    

      

        

        

      

         

        

     

      

      

             

    

        

    

    

        

    

    

        

    

Spring整合mybatis的applicationContext.xml配置

  1. xml version="1.0" encoding="UTF-8"?>  

  

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  

    xmlns:cache="http://www.springframework.org/schema/cache"  

    xsi:schemaLocation="  

    http://www.springframework.org/schema/context  

    http://www.springframework.org/schema/context/spring-context.xsd  

    http://www.springframework.org/schema/beans  

    http://www.springframework.org/schema/beans/spring-beans.xsd  

    http://www.springframework.org/schema/tx  

    http://www.springframework.org/schema/tx/spring-tx.xsd  

    http://www.springframework.org/schema/jdbc  

    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  

    http://www.springframework.org/schema/cache  

    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  

    http://www.springframework.org/schema/aop  

    http://www.springframework.org/schema/aop/spring-aop.xsd  

    http://www.springframework.org/schema/util  

    http://www.springframework.org/schema/util/spring-util.xsd">  

  

      

      

  

      

      

          

              

                classpath*:jdbc.properties  

              

          

      

  

      

      

          

          

          

          

  

          

          

          

          

  

          

          

  

          

          

  

          

          

  

          

          

          

          

  

          

          

          

  

          

          

      

  

      

      

  

      

      

  

      

      

  

      

      

  

      

      

  

      

      

      

      

  

 

注解的整体把握 

1、 作用

Spring 容器初始化的时候,会扫描 com.eduoinfo.finances.bank.web下 标有 (@Component,@Service,@Controller,@Repository) 注解的 类 纳入spring容器管理

在类上 ,使用以***解,实现bean 的声明

@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@Service 用于标注业务层组件

@Controller 用于标注控制层组件(如srping mvc的controller,struts中的action)

@Repository 用于标注数据访问组件,即DAO组件

示例:

@Controller
@RequestMapping(value = "/test")
public class TestController {

}

------------------------------------------------------------------------------------------------------------------

在类的成员变量上,使用以***解,实现属性的自动装配

@Autowired : 按类 的 类型进行装配

@Resource (推荐) : 1 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

    2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 

    3.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常 

    4.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。 

示例:

@Resource
private TestServiceImpl testServiceImpl;