Spring与Mybatis整合配置

时间:2022-03-28 05:20:19

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>