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

相关文章
相关标签/搜索