Mybatis与Spring整合后的配置与使用

Mybatis与Spring整合后

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

通过Spring将mybatis整合后大大减少了我们的工作量,我们看一看其中mybatis的配置文件在哪里

1.mybatis的配置文件

众所周知mybatis是针对数据库进行封装的持久层框架,所对应操作的层面也就是我们的Dao层,所以我们在Spring的配置文件中也是在dao.xml中进行mybatis的配置.

<?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"
   xsi:schemaLocation="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.xsd">
<!-- 配置整合mybatis过程 -->

<!-- 1.配置数据库相关参数properties的属性:设置在spring-properties中 -->
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>

<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.comment.george.entity" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>

<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.comment.george.dao" />

</bean>
</beans>

这是mybatis的设置,我们针对其中的连接池属性, 也就是我们的数据库属性,出于我们平时管理数据库的需求,我们将其中数据库的属性单拿出来形成一个JDBC.properties文件进行存放,在spring的xml文件中通过EL表达式对其中的数值进行获取.

同样我们还需要设置mybatis的全局环境, 需要另外一个文件 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>
        <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列别名替换列名 默认:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
<plugins>
    <plugin interceptor="com.comment.george.dao.interceptor.PageInterceptor"/>
</plugins>

</configuration>

在这里面我们队全局属性进行了配置, 开启相关的服务, 并且配置我们自己所编写的拦截器.

2.mybatis的使用

通过上方设置好的配置文件,我们就可以对myabtis进行使用

2.1mybatis的注解

@param:通过@Param("id")进行参数制定 可以在mapper.xml的映射文件中使用正确的参数.

2.2mybatis的xml文件.

在mybatis中最重要的就是我们所要配置的映射文件mapper.xml.

看一看xml文件中的标签和其中都有什么属性:

  • namespace:用于指定类路径
  • select:用于编写select的SQl语句
  • update:用于编写update的sql语句
  • delete:用于编写delete的sql语句
  • insert:用于编写insert的sql语句

返回结果:

  • resultType:
    我们通过resultType可以指定基本数据类型或者简单的bean作为返回值进行设定,简单的bean是指在bean内部不包含其他bean的情况.

  • resultMap:
    当resultType不能满足需求的时候,我们需要自己指定自己需要的类型并且告诉Mybatis让其解析.

要在返回结果中使用resultMap,我们需要先定义一个自己的resultMap其结构如下:

<resultMap id="businessMap" type="Business">//id为我们在resultmap中所要填写的名称 type为我们所指定的复杂bean
    <id property="id" column="id"/>//property为我们bean内的属性名称, column为数据库中列的名称 id是设为主键,提高查找效率
    <result property="title" column="title"/>
    <result property="subTtile" column="sub_title"/>
    <association property="cityDic" javaType="Dic">//association为联合,我们要为主Bean中的子Bean的属性进行赋值,需要通过这个标签进行指定, javaType为子bean的属性名称
        <result property="cityDic" column="city_name"/> 子bean的属性名称以及bean的属性名
    </association>
    <association property="categoryDic" javaType="Dic">
        <result property="categoryDic" column="category_name"/>
    </association>
</resultMap>

resultmap中常用的有两个标签:
association与collection:

  • association通常用在单体的bean当中.

  • collection通常用在ListBean数组中.

2.3mybatis拦截器的介绍与使用

我们平时要通过前端页面传回来的一些设定进行查询,我们如果针对每一个需求去进行模糊查询以及属性修改, 实在是太麻烦了,于是我们的Mybatis拦截器就上场了,我们通过Mybatis拦截器可以将还未执行的Sql语句进行拦截,对其进行过滤,符合需求的Sql语句对其进行修改,达到分页或者条件查询的目的.

我们要实现自己的拦截器接口就需要继承interceptor接口并且实现

interceptor接口中有三个方法:

intercept方法: 我们拦截后的正确结果就在这个方法里面, 我们通过在这个方法对sql语句进行改造

plugin方法:通过动态代理的方式返回正确的sql对象交与intercept

setProperties方法: 设置属性值 让拦截器按照属性值进行行动.

使用拦截器的方法分为两步:
1.对拦截器的接口进行继承实现
2.对拦截器在相应的DAO.xml文件中进行注册.

猜你喜欢

转载自www.cnblogs.com/Curry-Rice/p/8973765.html
今日推荐