zoukankan      html  css  js  c++  java
  • spring与mybatis的整合

    spring与mybatis整合步骤:

      1、创建maven项目 并导入所需依赖

      2、创建mybatis主配置文件(mybatis-config.xml)

      3、配置mybatis与spring的一个整合包

        创建方式:new--->XML Configuration File------> Spring Config

        如果XML Configuration File下没有Spring Config 那么原因很可能是未导入Spring的依赖

    mybatis与spring的一个整合配置文件(applicationContext-mybatis.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: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/context
                    http://www.springframework.org/schema/context/spring-context.xsd
            ">
        <!--配置dbcp数据源-->
        <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode&characterEncoding=UTF-8"/>
            <property name="username" value="root"/>
            <property name="password" value="123"/>
        </bean>
        <!--配置sqlSessionFactoryBean-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--引用数据源组件-->
            <property name="dataSource" ref="dataSource"/>
            <!--引用mybatis配置文件位置-->
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <!--配置和接口对应的映射文件-->
            <property name="mapperLocations">
                <list>
                    <value>classpath:com/smbms/mapper/**/*.xml</value>
                </list>
            </property>
        </bean>
    
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
    
        <bean id="userMapper" class="com.smbms.mapper.user.UserMapperImpl">
            <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
        </bean>
    
        <bean id="userService" class="com.smbms.service.UserServiceImpl">
            <property name="userMapper" ref="userMapper"/>
        </bean>
    

    4、编写业务逻辑代码并测试

      今天在完成spring与mybatis的整合的作业的时候遇到一个问题 就是 编译时类路径下找不到该mapper.xml映射文件 其实这个问题

      在mybatis中就遇到过 其中有两种解决方案 第一种呢 就是将你的mapper映射文件放在resources资源包下这样编译的之后就能检索

      到你的映射文件

      我平时喜欢直接写在mapper包下 但是这样的话编译的时候检索不到 所以我们就可以使用第二种解决方案:

        只需要在pom.xml文件中加入代码

    <build>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
            </resources>
        </build>
    
    即可

        
      
  • 相关阅读:
    AsyncTask的用法
    关于 android setAnimationStyle 的问题
    android有关bluetooth常用api简介
    Android调用系统Activity选取图像部分区域
    Android 关于ListView几个特别的属性
    Android应用界面动画切换(主要Tabhost中跳转出来的动画效果解决[转]
    ✿Android 3.1 久违的 USB、mtp、rtp
    读《C++沉思录》心得:拷贝构造函数
    Cloud Foundy入门
    HDFS读文件详解
  • 原文地址:https://www.cnblogs.com/javaBoy-ahua/p/14038974.html
Copyright © 2011-2022 走看看