mybatis整合spring的两种实现方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lycyl/article/details/88553202

前言

mybatis整合spring就是让mybatis中要用到的核心组件交给spring容器管理,因此主要工作就是把mybatis框架使用过程中所涉及的核心组件配置到Spring容器中,交给Spring来管理。

javaweb中业务层主要依赖mybatis提供的DAO对象来对数据库进行操作,也就是sqlSession实例,再往上追溯就是sqlSessionFactory,而sqlSessionFactory是依靠SqlSessionFactoryBuilder读取mybatis全局配置文件来构建的。所以,这些工作现在都要交给spring。

开头的像引入依赖的jar包,什么的步骤在这里就不赘述了,大家去查阅资料。我们主要来讲讲mybatis整合spring,DAO基类的实现以及整合的相关配置文件的配置。

一、配置mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
        <setting name="cacheEnabled" value="true"/>
        <!-- 全局映射器启用缓存 -->
        <setting name="useGeneratedKeys" value="false"/>
        <setting name="defaultExecutorType" value="REUSE"/>
    </settings>

	<!-- 设置别名 -->
    <typeAliases>
        <typeAlias type="test.User" alias="user"/>
    </typeAliases>

</configuration>

看, 配置文件一下子简化了,因为与spring整合后,environments、mapper都可以交给spring管理。当然如果你还要用到其它功能可以扩充(缓存配置也不是必须的,不配置的话mybatis也会设置默认值)。

二、配置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: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.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:test/config/dbconfig.properties</value>
            </list>
        </property>
    </bean>

    <!-- 第一步,配置数据源 阿里 druid数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 数据库基本信息配置 -->
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
        <property name="driverClassName" value="${db.driverClassName}"/>
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="${db.maxActive}"/>
        <!-- 初始化连接数量 -->
        <property name="initialSize" value="${db.initialSize}"/>
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${db.maxWait}"/>
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${db.minIdle}"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/>
        <property name="validationQuery" value="${db.validationQuery}"/>
        <property name="testWhileIdle" value="${db.testWhileIdle}"/>
        <property name="testOnBorrow" value="${db.testOnBorrow}"/>
        <property name="testOnReturn" value="${db.testOnReturn}"/>
        <property name="maxOpenPreparedStatements" value="${db.maxOpenPreparedStatements}"/>
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="${db.removeAbandoned}"/>
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="${db.removeAbandonedTimeout}"/>
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="${db.logAbandoned}"/>

        <property name="filters" value="${db.filters}"/>
        <property name="connectionProperties" value="${db.connectionProperties}" />
    </bean>

    <!-- 第二步,配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:resources/mybatis/mybatis-config.xml"></property>
        <!-- mapper扫描 -->
        <property name="mapperLocations" value="classpath:mybatis/*/*.xml"></property>
    </bean>


    <!--第三步,配置SqlSessionTemplate-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>

    <!--第四步,装配业务组件-->

    <bean id="studentService" class="com.Blog.ServiceImpl.StudentServiceImpl">
        <property name="sqlSessionTemplate" ref="sqlSessionTemplate" />
    </bean>

</beans>

A.第一种整合方式---采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate 

 基于上述的配置,我们可以编写DAO的基类。代码如下,至此第一种整合就已经实现了。

package com.im.framework.dao;

import java.util.List;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BaseDaoSuport {
	private static ApplicationContext context = new ClassPathXmlApplicationContext("test/ApplicationContext.xml");
	private SqlSessionTemplate sqlSessionTemplate;
	
	public BaseDaoSuport() {
		sqlSessionTemplate = (SqlSessionTemplate)context.getBean("sqlSessionTemplate");
	}

	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	
	public List findAll(String arg1,Object arg2){
		return sqlSessionTemplate.selectList(arg1, arg2);
	}
}

 

猜你喜欢

转载自blog.csdn.net/lycyl/article/details/88553202