spring學習 六 spring與mybatis整合
在mybatis學習中有兩種配置文件 :全局配置文件,映射配置文件。mybatis和spring整合,其實就是把mybatis中的全局配置文件的配置內容都變成一個spring容器的一個bean,讓spring容器進行托管.因此整合過程就是把mybatis全局配置文件的內容整合到spring的配置文件中
(一)mybatis全局配置文件 :
根標簽是<configuration>,
子標簽包括:
<typeAliases>配置別名,
<environments> 配置數據庫環境,
<mappers> 配置包掃描
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置log4j --> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <!-- default的值是<environment>的id,代表當前要連接的數據庫環境 【註】default的值一定要是一個<environment>,否則在獲取SqlSession實例時會報空指針錯誤--> <environments default="mySqlJdbc"> <environment id="mySqlJdbc"> <!-- 事物標簽,使用原生jdbc的事物 ,如果是MANAGERD表示交給其他容器管理--> <transactionManager type="JDBC"></transactionManager> <!-- 數據庫連接池技術 --> <dataSourcetype="POOLED"> <!-- property中的name的值,不能隨便寫,有規定的 --> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.153.128:3306/mybaties?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> <environment id="oracleJdbc"> <transactionManager type=""></transactionManager> <dataSource type=""></dataSource> </environment> </environments> <!-- 加載XXXMapper.xml 如果采用代理開發,package批量指定xxxMapper.xml文件已經xxxMapper.java文件 --> <mappers> <mapper resource="com/xxx/mapper/FlowerMapper.xml"/> </mappers> </configuration>
根上面的配置文件,就可以推斷出,spring中必須有
(1)用來描述數據庫鏈接的基本配置內容,對應mybatis配置文件中的<dataSource>標簽
(2)用來獲取sqlSession的sqlSessionFactory,對應mybatis中的下面的代碼
//加載配置文件 InputStream in = Resources.getResourceAsStream("mybatis.xml"); //使用工廠設計模式 【註意】以後sqlSessionFactory都會被spring 管理,不需要我們自己創建 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
(3) 用來指定映射文件位置的配置內容,mybatis全局配置文件中的<mappers>標簽
(二) 把mybatis全局配置文件整合進spring配置文件
第一步:整合數據庫配置
在spring中使用了一個數據源封裝類來替換mybatis全局配置文件中的<dataSource>標簽,這個類就是org.springframework.jdbc.datasource.DriverManagerDataSource,在spring-jdbc.jar包中。在spring配置文件中做如下配置
<!-- 數據源封裝類 數據源就是用來封裝數據庫鏈接的,此處的類就是對JDBC中的DriverManager的封裝 spring對jdbc的封裝在spring-jdbc.jar包中 數據源封裝類的作用就是代替mybatis中的<dataSource>標簽裏面的東西 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="url" value="jdbc:mysql://192.168.153.128:3306/mybaties?characterEncoding=utf-8"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> <!-- 在DriverManagerDataSource類以及父類中,並沒有任何一個driverClassName屬性, 只是有一個public void setDriverClassName(String driverClassName)方法, 在使用<property>標簽時,其實是根據name屬性的值,來確定要調用的setter方法,name屬性的值並不是類中的屬性 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> </bean>
第二步:把用來獲取sqlSession的sqlSessionFactory註冊到spring工廠
<!-- sqlSessionFactory ,註冊sqlSession工廠--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--dataSource的類型是DataSource是一個接口,上面配置的DriverManagerDataSouce類實現了DataSource接口 --> <property name="dataSource" ref="dataSource"></property> </bean>
第三步:配置mybatis要掃描的映射文件的位置,spring在掃描basePackage指定的包時,會創建這個包下的所有接口的實現類的實例,並把這些實例註冊到spring容器,且註冊的名稱為接口名的首字母小寫
<!-- 配置mybatis掃描的mapper包,是替代mybatis全局配置文件中的<mappers>標簽 掃描了com.spring.mapper包下的所有的接口,並且會給這些接口提供實例化,並且實例化的bean命名為首字母小寫的接口名 例如有一個接口名為AirportMapper,在spring中對應的bean的名稱為airportMapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.airplan.mapper" ></property> <property name="sqlSessionFactory" ref="sessionFactory"></property> </bean>
經過上述三步就可以把mybatis需要的基本內容整合進spring
為了下面的代碼測試,再在spring配置文件中配置一個bean
<bean name="airportService" class="com.airplan.service.impl.AirportServiceImpl"> <property name="airportMapper" ref="airportMapper"></property> </bean>
(三)代碼測試
public class TestClass { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); String[] names=ac.getBeanDefinitionNames(); for (String string : names) { System.out.println(string); } AirportService bean = ac.getBean("airportService",AirportService.class); List<Airport> showAirports = bean.showAirport(); System.out.println(showAirports.size()); for (Airport airport : showAirports) { System.out.println(airport); } } }
spring學習 六 spring與mybatis整合