mybatis与spring整合配置文件

<?xml version="1.0" encoding="utf8"?>
<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:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。
连接池配置如下-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/courseman"/>
<property name="username" value="courseman"/>
<property name="password" value="abc123"/>
</bean>
<bean id="sqlSessionFactory" 
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource"/>
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="resources/configuration.xml"/>
</bean>
<bean id="studentMapper" 
class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" 
value="com.abc.mapper.StudentMapper"/>
</bean>
<bean id="teacherMapper" 
class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" 
value="com.abc.mapper.TeacherMapper"/>
</bean>
<!--解决以上多个mapper配置繁琐问题 -->
<!--MapperScannerConfigurer配置-->
<!--
1.无需指定引用SqlSessionFactory,因为MapperScannerConfigurer在创建映射器时会通过自动装配的方式来引用。
2.映射器接口TeacherMapper被扫描后创建的映射器bean名为teacherMapper
TeacherMapper mapper  =  (TeacherMapper)ctx.getBean("teacherMapper");
3.可以指定名称:@Component
@Component("myTeacherMapper")
public interface TeacherMapper {}
TeacherMapper mapper = (TeacherMapper)ctx.getBean("myTeacherMapper");
4.若映射器接口(如TeacherMapper接口)与相应的映射配置文件(TeacherMapper.xml)同名且在同一目录下,就无需在核心配置文件configuration.xml中使用mappers元素来指定映射配置文件了
-->
<!--
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage指定要扫描的包,在此包之下的映射器都会被
搜索到。可指定多个包,包与包之间用逗号或分号分隔-->
<property name="basePackage" value="com.abc.mapper"/>
</bean>
-->
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值