spring-mybatis整合:
1,mybatis原先有config.xml配置文件,核心是是通过SqlSessionFactory来操作数据库
2,所以Spring整合MyBatis 其实就是 将MyBatis的SqlSessionFactory 交给Spring
3,config.xml配置文件中配置的是数据库信息,sqlsessionFactory信息,将这些信息搬入spring配置文件中(spring-dao.xml)
4,以上信息配好还不够,sqlsessionFactory中还需要配置信息去操作数据库,与dao层联系
<!--告诉spring扫描映射文件 -->
<property name="mapperLocations" value="classpath:com/briup/dao/*.xml"></property>
spring-dao.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 读取properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!--配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<!-- 配置sqlsessionFactory -->
<!-- 查看源码便可知注入的property的名字 -->
<!-- 可以读取mybatis-config.xml文件,也可以不读取,信息全都在配置在spring中 -->
<!-- <property name="configLocation" value="classpath:mybatis-config.xml"
/> -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.briup.bean"></property>
<property name="configurationProperties">
<props>
<prop key="cacheEnabled">true</prop>
<prop key="lazyLoadingEnabled">false</prop>
</props>
</property>
<!--告诉spring扫描映射文件 -->
<property name="mapperLocations" value="classpath:com/briup/dao/*.xml"></property>
<!-- 分页插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=oracle
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 扫描mapper接口所在的包名,当有多个包的时候,用半角逗号分隔即可,也可以使用*通配符 注意:这里不注入sqlSessionFactory也是可以的 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.briup.dao"></property>
</bean>
</beans>
来源:https://www.cnblogs.com/wskb/p/11507358.html