Spring与Mybatis整合

5 篇文章 0 订阅

简单介绍整合原理

spring与mybatis整合需要一个jar包:mybatis-spring.jar
mybatis-spring.jarjar包中提供了一些API,可用于获取资源:
1、SqlSessionFactoryBean:提供SqlSession对象
在单独使用mybatis的时候所有的操作都是有SqlSession对象来完成的,SqlSession对象通过SqlSessionFactory获取,SqlSessionFactory对象通过SqlSessionFactoryBuilder对象创建生成
在spring和mybatis整合中,同样需要SqlSession对象,SqlSessionFactoryBean作用就是通过原SqlSessionFactoryBuilder生成SqlSessionFactory对象,为应用程序提供SqlSession对象。

2、MapperFactoryBean:根据Mapper接口生成bean实例
MapperFactoryBean API中封装了SqlSession.getMapper()方法,在使用时需要注入两个必要属性:
a、SqlSessionFactoryBean对象
b、返回Mapper对象的Mapper接口

3、MapperScannerConfigurer:根据指定包批量扫描Mapper接口并生成实例
MapperScannerConfigurer组件会自动扫描指定包中的Mapper接口类自动注册对应的MapperFactoryBean对象,这样可以解决应用中MapperFactoryBean 需求量很大时,可以大量减少代码量。
1、使用时注入basePackage属性,多个包可以用,号隔开;
2、SqlSessionFactory属性可以不用指定注入,会自动Autowrited方式注入给MapperScannerConfigurer。
3、当指定的包下不完全是Mapper接口时,用annotationClass和markerInterface属性减少收索范围

annotationClass:用于指定一个注解标记,指定了annotationClass时,MapperScannerConfigurer就只会注册注解标记过的Mapper接口
markerInterface:用于指定一个接口,指定了markerInterface时,MapperScannerConfigurer将只会注册继承自markerInterface的接口Mapper

4、SqlSessionTemplate
mybatis-spring.jar还提供了SqlSessionTemplate组件,可以将其注入给程序中DAO,利用DAO中的SqlSessionTemplate对象进行操作数据

下面提供一个实例简单实现整合过程

1、数据准备:

create table
CREATE TABLE `emp` (
   `id` int(10) NOT NULL,
   `name` varchar(20) DEFAULT NULL,
   `age` int(3) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1

INSERT INTO `test`.`emp`(`id`,`name`,`age`) VALUES ( '1','xsx','24');
INSERT INTO `test`.`emp`(`id`,`name`,`age`) VALUES ( '2','mpt','23');

2、实体类和映射接口

package org.xsx.entity;

public class Emp {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }


}

package org.xsx.entity;

import java.util.List;

public interface EmpMapper2{

    public List<Emp> findAll();

}

3、映射文件

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="org.xsx.entity.EmpMapper2">

<select id="findAll" resultType="org.xsx.entity.Emp">
    select id,name,age from emp
</select>

</mapper>

4、applicationContext.xml配置wenjian

<?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"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <property name="username" value="root" />
        <!-- property池启动时的初始值 -->
        <property name="password" value="root" />
        <!-- 连接name="initialSize" value="${initialSize}"/> -->
        <property name="initialSize" value="1" />
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="500" />
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="2" />
        <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="1" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:org/xsx/entity/EmpMapper.xml" />
    </bean>

    <bean id="empMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="org.xsx.entity.EmpMapper2" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>


</beans>

5、测试

package org.xsx.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xsx.entity.Emp;
import org.xsx.entity.EmpMapper2;

public class TestEmpMapper {
    @Test
    public void testFindAll(){
        String conf = "applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        EmpMapper2 mapper = ac.getBean("empMapper", EmpMapper2.class);
        List<Emp> list = mapper.findAll();
        for(Emp e:list){
            System.out.println(e.getName());
        }
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值