spring整合Mybatis
一、导 入 mybatis 所 有 jar 和 spring 基 本包,spring-jdbc,spring-tx,spring-aop,spring-web,mybatis-spring等包
配置applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 <!-- 获取数据库连接信息存入DriverManagerDataSource中,命名为dataSource --> 7 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 8 <property name="url" value="jdbc:mysql://localhost:3306/bjsxt"></property> 9 <property name="username" value="root"></property> 10 <property name="password" value="155251"></property> 11 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> 12 </bean> 13 <!-- 获取session对象,数据库连接信息来源于dataSource,命名为factory --> 14 <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> 15 <property name="dataSource" ref="dataSource"></property> 16 </bean> 17 <!-- 扫描器配置,相当于mybatis.xml中的mappers下面的package标签 --> 18 <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 19 <!-- 定义要扫描的包 --> 20 <property name="basePackage" value="com.bjsxt"></property> 21 <!-- 与factory产生关联 --> 22 <property name="sqlSessionFactory" ref="factory"></property> 23 </bean> 24 <!-- 由spring管理serivce实现类 --> 25 <bean id="impl" class="com.briup.serivce.impl.AirPortSerivceImpl"> 26 <property name="ariport" ref="airPortMapper"></property> 27 </bean> 28 </beans>