zoukankan      html  css  js  c++  java
  • Spring与Mybatis整合配置

    Mybatis核心配置文件:

    配置文件内可以不写内容

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
    
        <!--<settings>
            <setting name="" value=""/>
        </settings>-->
    
    </configuration>

    Spring核心配置文件

    <?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">
    
        <!--引入db.properties配置文件-->
        <context:property-placeholder location="db.properties"></context:property-placeholder>
        <!--注解扫描包-->
        <context:component-scan base-package="com.xxx"></context:component-scan>
        <!--数据源-->
        <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!--驱动类名-->
            <property name="driverClass" value="${driver}"></property>
            <!--url-->
            <property name="jdbcUrl" value="${url}"></property>
            <!--用户名-->
            <property name="user" value="${name}"></property>
            <!--密码-->
            <property name="password" value="${pwd}"></property>
            <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数-->
            <property name="acquireIncrement" value="5"></property>
            <!--初始连接池大小-->
            <property name="initialPoolSize" value="10"></property>
            <!--连接池中连接最小个数-->
            <property name="minPoolSize" value="5"></property>
            <!--连接池中连接最大个数-->
            <property name="maxPoolSize" value="20"></property>
        </bean>

        Mybatis与Spring整合配置:

        SqlSessionFactoryBean:Mybatis整合Spring核心API之一

        <!--Mybatis相关配置-->
        <!--工厂对象-->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--数据源-->
            <property name="dataSource" ref="ds"></property>
            <!--别名包-->
            <property name="typeAliasesPackage" value="com.xxx.pojo"></property>
            <!--映射文件:加载映射文件(xxxMapper.xml)-->
            <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
            <!--读取mybatis核心配置文件:其它mybatis核心配置文件中定义的内容-->
            <property name="configLocation" value="mybatis-comfig.xml"></property>
        </bean>

        MapperScannerConfigurer:MyBatis整合Spring核心API之一

        扫描接口:

        <!--将所有的接口对象注入到容器中-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
            <!--接口所在位置-->
            <property name="basePackage" value="com.xxx.mapper"></property>
        </bean>
    
    </beans>
  • 相关阅读:
    Session的使用与Session的生命周期
    Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别与使用
    十九、详述 IntelliJ IDEA 之 添加 jar 包
    十八、IntelliJ IDEA 常用快捷键 之 Windows 版
    十七、IntelliJ IDEA 中的 Maven 项目初体验及搭建 Spring MVC 框架
    十六、详述 IntelliJ IDEA 创建 Maven 项目及设置 java 源目录的方法
    十五、详述 IntelliJ IDEA 插件的安装及使用方法
    十四、详述 IntelliJ IDEA 提交代码前的 Code Analysis 机制
    十三、IntelliJ IDEA 中的版本控制介绍(下)
    十二、IntelliJ IDEA 中的版本控制介绍(中)
  • 原文地址:https://www.cnblogs.com/peng-1234/p/14589722.html
Copyright © 2011-2022 走看看