mybatis的应用

 Mybatis是一个优秀的持久层框架,封装了jdbc操作数据库的过程,支持存储过程、sql定制化、映射,开发人员不用处理jdbc过程。总的来说,它主要做根据 JDBC 规范建立与数据库的连接;实现 Java对象与关系数据库之间相互转化。

 Mybatis原理图

      Mybatis架构主要分为三层:API接口层、数据处理层、基础支撑层。其中各层的作用如下:接口层,从字面含义可以其作用和项目开发中的接口类似,提供给外部使用的接口API,开发人员通过这些本地API来操作数据库;接口层收到调用请求就会调用数据处理层来完成数据库的处理。

      数据处理层,负责具体的sql查询、sql解析、sql执行和执行结果的映射处理等,主要的作用是根据调用请求来完成一次数据库的操作。

      基础支持层,有点类似项目中共同的方法功能提出出来做为tool或common,准确地说负责最基础的功能支撑,包括连接管理、事务管理、配置加载、缓存处理等,共同的方法功能抽取出来做基础组件。

maven 依赖:

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.3</version>
        </dependency>

Mybatis与Hibernate

      与mybatis类似,hibernate也是一个优秀的持久化框架:

      通过比较我们可以发现,mybatis的优势在于容易掌握、可进行更为细化的sql优化,hibernate的优势还是比较明显的。项目框架选择中,根据项目的实际情况确定选择。

项目中如何使用

      以maven聚合为例,在父工程中定义依赖的jar包的版本信息,子工程依赖父工程,这样变更获取到mybatis的相关信息:
            <!-- Mybatis -->  
            <dependency>  
                <groupId>org.mybatis</groupId>  
                <artifactId>mybatis</artifactId>  
                <version>${mybatis.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.mybatis</groupId>  
                <artifactId>mybatis-spring</artifactId>  
                <version>${mybatis.spring.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>com.github.miemiedev</groupId>  
                <artifactId>mybatis-paginator</artifactId>  
                <version>${mybatis.paginator.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>com.github.pagehelper</groupId>  
                <artifactId>pagehelper</artifactId>  
                <version>${pagehelper.version}</version>  
            </dependency>  


  交给spring管理,需要与spring进行整合:

1.数据库连接:加载配置文件获得连接数据库的信息;

2.让spring管理sqlsessionfactory 使用mybatis和spring整合包中的:进行sqlsessionfactory 、sqlsession的创建;

3.配置扫描包,加载mapper代理对象:


    <!-- 数据库连接池 -->  
    <!-- 加载配置文件 -->  
    <!-- <context:property-placeholder location="classpath:properties/*.properties" /> -->  
    <context:property-placeholder location="classpath:resource/db.properties" />  
    <!-- 数据库连接池 -->  
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
        destroy-method="close">  
        <property name="url" value="${jdbc.url}" />  
        <property name="username" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
        <property name="driverClassName" value="${jdbc.driver}" />  
        <property name="maxActive" value="10" />  
        <property name="minIdle" value="5" />  
    </bean>  
    <!--配置sqlSessionFactory-->  
    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <!-- 加载mybatis的全局配置文件 -->  
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>  
        <!-- 数据库连接池 -->  
        <property name="dataSource" ref="dataSource"></property>  
    </bean>  
    <!--配置扫描包,加载mapper代理对象-->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.xxx.mapper"></property>  
    </bean>  


  建立mybatis的全局配置文件SqlMapConfig.xml配置相关的信息,数据库的具体连接信息交给db.properties配置文件与spring隔离管理,然后在mapper.xml文件写sql语句即可,例如:
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.taotao.pojo.TbUserExample" >  
    select  
        <if test="distinct" >  
          distinct  
        </if>  
        <include refid="Base_Column_List" />  
    from tb_user  
    <if test="_parameter != null" >  
      <include refid="Example_Where_Clause" />  
    </if>  
    <if test="orderByClause != null" >  
      order by ${orderByClause}  
    </if>  
  </select>  

DataSource数据源配置:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

time Friend

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值