zoukankan      html  css  js  c++  java
  • mybatis-spring

    *********************jdbc.properties********************************************
    driver=oracle.jdbc.driver.OracleDriver
    url=jdbc:oracle:thin:@localhost:1521:orcl
    username=fang
    password=fang
    
    
    <?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:p="http://www.springframework.org/schema/p"
        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.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        
        <!-- 读取jdbc.properties文件 -->
        <!-- 这个不行<context:property-placeholder location="classpath:jdbc.properties"/> -->
        <bean id="propertiesConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties"/>
        </bean>
        <!-- 获得数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
            <property name="driverClassName" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </bean>
        <!-- 事务管理 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- sqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="dataSource" ref="dataSource"/>
            <property name="mapperLocations" value="classpath:com/gym/entity/*.xml"/>
        </bean>
        <!--sqlSessionTemplate  -->
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
        <!--bean注入  -->
        <bean id="deptDaoimpl" class="com.gym.deptDaoimpl">
            <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
        </bean>
    </beans>
    
    
    
    
    ==============================另一种方式==============================================
    
    <?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:p="http://www.springframework.org/schema/p"
        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.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        
        <!-- 这个不行<context:property-placeholder location="classpath:jdbc.properties"/> -->
             <!-- 读取priperties文件 -->
        <bean id="propertiesConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties"/>
        </bean>
        <!-- JNDI获得数据源 -->
        <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
            <property name="driverClassName" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </bean>
            <!-- 事务管理 -->
        <bean id="transacionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="datasource"/>
        </bean>
        <!-- sqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="dataSource" ref="datasource"/>
        </bean>
        <!-- teacherMapper接口映射与工厂 -->
        <bean id="teacherMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <property name="mapperInterface" value="com.gym.dao.TeacherMapper"/>
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
        <!-- service -->
        <bean id="teacherServiceImpl" class="com.gym.service.impl.TeacherServiceImpl">
            <property name="teacher" ref="teacherMapper"></property>
        </bean>
    </beans>
  • 相关阅读:
    EM
    解决使用bootstrap modal时,icon-picker组件被遮挡问题
    记一次cpu100%问题排查过程
    docker安装nexus搭建maven私库
    记一次bootstrap table使用中的遭遇
    数据的逻辑结构与存储结构的基本概念(数据结构巩固一)
    前端js函数中直接获取springmvc后台model中传值
    springboot+vue部署后提示找不到css
    将springboot的jar包添加到windows服务及遇到的问题及其解决思路
    springboot打包jar后读取资源文件
  • 原文地址:https://www.cnblogs.com/gym2017/p/7300073.html
Copyright © 2011-2022 走看看