zoukankan      html  css  js  c++  java
  • Spring框架整合mybais框架注入映射器實現

    通過上面一個案例,我們能夠看到,每次在執行具體的某個方法的時候,我們都會創建一個映射器,這是非常麻煩的,這就是我們所看到的UserMapperImpl.java,那麽我們能不能將他省略掉了,將創建映射器的方法交給Spring的ioc容器進行管理,答案是肯定的

    aplicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     4     xmlns:p="http://www.springframework.org/schema/p"
     5     xmlns:aop="http://www.springframework.org/schema/aop" 
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xmlns:context="http://www.springframework.org/schema/context"
     8     xsi:schemaLocation="http://www.springframework.org/schema/beans
     9     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    10     http://www.springframework.org/schema/aop
    11     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    12     http://www.springframework.org/schema/tx
    13     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
    14     http://www.springframework.org/schema/context
    15     http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
    16 
    17     <!--配置数据源 -->
    18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    19         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    20         <property name="url"
    21             value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&amp;characterEncoding=utf-8"></property>
    22         <property name="username" value="root"></property>
    23         <property name="password" value="root"></property>
    24 
    25     </bean>
    26     <!--配置SqlSessionFactoryBean -->
    27     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    28         <!--获取到你的数据源 -->
    29         <property name="dataSource" ref="dataSource"></property>
    30         <!--获取到mybatis的配置文件  注意这里使用的是value属性 -->
    31         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    32         <!--使用下面这种方式获取sql文件  -->
    33         <property name="mapperLocations">
    34             <list>
    35                 <value>classpath:cn/smbms/dao/**/*.xml</value>
    36             </list>
    37         </property> 
    38     </bean>
    39     
    40     <!--配置 SqlSessionTemplate  用它来执行数据库的各种操作    使用继承SqlSessionDaoSupport方式的话,就不用获取SqlSessionTemplate类了-->
    41 <!--     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    42        操作数据库的时候,引用数据库的连接  通过这个SqlSessionTemplate类的构造方法
    43        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    44     </bean> -->
    45     
    46     <!--將映射器的實現交給spring的ioc容器進行管理,這時候,可以將UserMapperImpl類刪除掉也是可以的 -->
    47     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    48     <property name="mapperInterface" value="cn.smbms.dao.user.UserMapper"></property>
    <!-- 为什么还需要配置会话工厂,主要的原因是获取sqlSessionTemplate操作数据库 -->
    49 <property name="SqlSessionFactory" ref="sqlSessionFactory"></property> 50 </bean> 51 52 </beans>

    這時候,我們可以將UserMapperImpl.java類進行刪除,刪除之後的項目結構;

     最總的運行結果:

      

  • 相关阅读:
    [Flash开发笔记] ActionScript 生成伪 Guid
    [Flash开发笔记] 如何在as2.0中使用自定义类事件
    vs2005 智能感知不正常的解决办法
    [Flash开发笔记] 自定义ActionScript中的trim函数,取回车函数,字节换算函数
    爱你不容易——ExternalInterface
    软件测试中开发团队和测试团队的职责
    [Flash开发笔记] 正确理解MovieClipLoader的onLoadComplete事件
    ActionScript 中的字符串替换函数
    “hello world”PHP
    使用"类型文件"(typed File),创建自己的"数据库"
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/12152477.html
Copyright © 2011-2022 走看看