您的位置:首页 > 编程语言 > Java开发

mybatis与spring整合配置文件

2015-03-18 20:49 429 查看
<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: