1. 程式人生 > >MyBatis和Spring整合

MyBatis和Spring整合

ner his not springmvc ole load ade 資源 sco

Spring MyBatis整合包

Spring 解壓

技術分享圖片

然後導入Mybatis包。

最後狀態:

技術分享圖片

上面導入了SSM的相關jar包

另外需要整合包:

技術分享圖片

配置文件:

與web.xml 同級的

技術分享圖片

創建mvc的配置文件 (它掃描的是controller )

Spring的配置文件:

技術分享圖片

負責數據源的需要導入c3p0:

技術分享圖片

1、開啟掃描業務邏輯掃描

2、配置數據源(需要c3p0jar包)

4、整合mybatis ,IOC容器一啟動就創建出SqlSessionFactory對象 (之前很多在全局配置文件中的配置,成了一些bean的屬性了對於配置來說。全局配置文件可以沒有了哦,但是一般建議留下,做一些比較犄角旮旯的配置用的)

配置數據源

配置 xml的映射文件路徑地址

全局配置文件的路徑地址(建議留下的)

5、spring事務管理 並且開啟基於註解的事務

技術分享圖片

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
> <!-- Spring希望管理所有的業務邏輯組件,等。所以要除去負責頁面跳轉的controller --> <context:component-scan base-package="com.atguigu.mybatis"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 引入數據庫的配置文件 --> <context:property-placeholder location="classpath:dbconfig.properties" /> <!-- Spring用來控制業務邏輯。數據源、事務控制、aop c3p0的數據源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- spring事務管理 管理 ref="dataSource"這個數據源 --> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟基於註解的事務 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> <!-- 整合mybatis 目的:1、spring管理所有組件。mapper的實現類。 service==>Dao @Autowired:自動註入mapper; 2、spring用來管理事務,spring聲明式事務 --> <!--創建出SqlSessionFactory對象 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 指定數據源(是spring提供的哈) --> <!-- configLocation指定全局配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 同時可以結合配置文件哦 --> <!--mapperLocations: 指定mapper .xml文件的位置--> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property> </bean> <!--配置一個可以進行批量執行的sqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean> <!-- 掃描所有的mapper接口的實現,讓這些mapper能夠自動註入(使用@autowired就可以獲取哦);base-package:指定mapper接口的包名 --> <mybatis-spring:scan base-package="com.atguigu.mybatis.dao"/> <!-- 這兩個標簽都可以 --> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.atguigu.mybatis.dao"></property> </bean> --> </beans>

sql映射

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatis.dao.EmployeeMapper">

    <!-- public Employee getEmpById(Integer id); -->
    <select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where id=#{id}
    </select>
    
    <!--public List<Employee> getEmps();  -->
    <select id="getEmps" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee
    </select>
</mapper>

dbconfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
jdbc.username=root
jdbc.password=123456

orcl.driver=oracle.jdbc.OracleDriver
orcl.url=jdbc:oracle:thin:@localhost:1521:orcl
orcl.username=scott
orcl.password=123456

mybatis-config.xml

<?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>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="jdbcTypeForNull" value="NULL"/>
        
        <!--顯式的指定每個我們需要更改的配置的值,即使他是默認的。防止版本更新帶來的問題  -->
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    
    <databaseIdProvider type="DB_VENDOR">
        <property name="MySQL" value="mysql"/>
        <property name="Oracle" value="oracle"/>
        <property name="SQL Server" value="sqlserver"/>
    </databaseIdProvider>
    
</configuration>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!--SpringMVC只是控制網站跳轉邏輯  -->
    <!-- 只掃描控制器 -->
    <context:component-scan base-package="com.atguigu.mybatis" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>   
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 處理動態資源 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 處理動態資源 -->
    <mvc:default-servlet-handler/>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyBatis_06_ssm</display-name>
  
  <!--Spring配置: needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- SpringMVC配置 -->
    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
</web-app>

MyBatis和Spring整合