Mybatis学习(四)——Mybatis和Spring整合中applicationContext说明
Mybatis与Spring整合推翻前面的配置,使用applicationContext配置SqlMapperConfig.xml里参数
1. 使用SqlSessionFactoryBean对象配置
DataSource(数据源),configLocation(sqlMapperConfig.xml地址),mapperLocations(xxxMapper.xml地址),typeAlisesBase(domain内mapper的类名别名)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-spring.xml"/> <property name="mapperLocations" value="classpath:xyz/javaswing/mapper/*.xml"/> <property name="typeAliasesPackage" value="xyz.javaswing.domain"/> </bean>
2. 使用MapperScannerConfigurer配置
basePackage(mapper包里mapper接口,会自动实例化mapper对象。直接@Autowrited即可)
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="xyz.javaswing.mapper"/> </bean>
使用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"/> <property name="user" value="root"/> <property name="password" value="844597608a"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-spring.xml"/> <property name="mapperLocations" value="classpath:xyz/javaswing/mapper/*.xml"/> <property name="typeAliasesPackage" value="xyz.javaswing.domain"/> </bean> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="xyz.javaswing.mapper"/> </bean> <bean id="roleService" class="xyz.javaswing.service.impl.RoleServiceImpl"> </bean> </beans>

浙公网安备 33010602011771号