Spring-Mybatis整合

1.spring-mybatis之原始dao層開發spring

(1)Mybais-dao開發:sql

            

(2)spring-mybatis-dao開發:數據庫

思想就是spring來管理sqlsession。項目中須要使用的jar包,須要添加的約束、添加的日誌能夠參考Mybatis的介紹。apache

須要的jar包:session

須要的包mybatis

  1. spring的jar包
  2. Mybatis的jar包
  3. Spring+mybatis的整合包。
  4. Mysql的數據庫驅動jar包。
  5. 數據庫鏈接池的jar包。

也能夠查看源碼:app

SqlMapConfig.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>
    <!-- 設置別名,避免映射配置文件中類要寫全名的狀況 -->
    <typeAliases>
        <package name="cn.hu.pojo"/>
    </typeAliases>
</configuration>工具

數據庫鏈接資源文件(放在src下):測試

applicationContext.xml文件:

dao層:

    而後Dao中的方法就能夠測試使用了。

2.spring-mybatis-Mapper代理

SqlMapConfig.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>
    <!-- 設置別名,避免映射配置文件中類要寫全名的狀況 -->
    <typeAliases>
        <package name="cn.hu.pojo"/>
    </typeAliases>
</configuration>

下面是mapper接口和映射配置文件的掃描:

applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
    <!-- c3p0鏈接池 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 配置Mybatis的工廠 -->
    <bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
    </bean>
   

//有多少個Mapper接口就要配置多少個,很麻煩
spring管理數據庫、sqlsession、Mapper。Mapper接口和Mapper.xml映射配置文件都是須要的。

測試Mapper:

3.spring-mybatis-mapper加強掃描版

與上一個版本的修改的地方:

(1)SqlMapConfig.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>
    <!-- 設置別名,避免映射配置文件中類要寫全名的狀況 -->
    <typeAliases>
        <package name="cn.hu.pojo"/>
    </typeAliases>
</configuration>
(2)applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
    <!-- c3p0鏈接池 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 配置Mybatis的工廠 -->
    <bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
    </bean>
    <!-- 直接掃描Mapper接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="cn.hu.mapper"></property>
    </bean>
</beans>
Mapper接口和Mapper.xml映射配置文件都是須要的。這個時候Mapper接口中的方法就能夠直接使用了。

4.逆向工程

    一個工具工程根據數據庫表生成pojo、Mapper接口、Mapper.xml文件。前提是數據庫中已經有表了。

    

    上面的是執行的主類,後面的是配置文件。下面介紹配置文件(將數據庫表映射到項目的pojo、Mapper、Mapper.xml):

    

    

    

    

    

    

    

    執行逆向工程的main,獲得pojo、Mapper、Mapper.xml:

    

    測試使用Mapper接口中的方法:

    

    Mapper接口中的方法都是寫好的,對應的sql語言在對應的Mapper.xml映射配置文件中。

    使用逆向工程的注意點:若是數據庫表中的字段是帶有下劃線的,那麼生成的pojo中字段是沒有下劃線的。因此使用的時候還要修改生成的pojo、Mapper。

總結

       (1) 操做數據庫能夠直接使用Mybatis框架,實際上使用的就是SqlSession來執行sql。SqlSession能夠封裝進dao層,也能夠封裝進Mapper(動態代理)。

        (2)Spring與Mybatis的整合思路就是:Spring來負責建立管理數據庫鏈接、SqlSession

                                        若是使用dao執行sql:spring管理dao 

                                        若是使用Mapper執行sql:spring管理mapper

相關文章
相關標籤/搜索