Spring与Mybatis的整合

4 篇文章 0 订阅
3 篇文章 0 订阅

Spring与Mybatis的整合


一、 回顾MyBatis常规操作流程

//mybatis常规操作流程
public void mybatis_basic() throws Exception {
    // 获取sqlSession工厂 
    // 注意,此处没有mybatis-config.xml
    InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); 
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    // 获取sqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    // 获取接口的实现类对象,然后执行方法完成功能
    AccountDao mapper = sqlSession.getMapper(AccountDao.class);
    mapper.update(1, 1300);
}

观察之前操作mybatis代码可以发现,mybatis操作数据库过程中,最关键的点是获取sqlSessionFactory对象,而想要获取到该对象,必须将mybatis-config.xml配置文件整合进来,所以我们使用spring整合mybatis的关键点就是: 读取mybatis-config.xml配置文件,然后就可以得到一个工厂对象。有了工厂对象后就可以获取session,再进一步获取mapper,完成功能。

其中可以使用mybatis-config.xml文件,也可以不使用,spring可以直接读取mybatis-config.xml文件,也可以直接在spring配置好mybatis内容,从而省去mybatis-config.xml

二、 Spring整合MyBatis

常规的mybatis开发中,一般需要四种文件:

  • mybatis-config.xml整体配置文件
  • XxxMapper.xml映射文件
  • XxxDao.java映射接口
  • 测试文件

现在我们要借助Spring完成一个完整mybatis功能,相关配置如下:

映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
		"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.au.db.AccountDao">
	
	<update id="update">
		update t_account set balance = #{1} where id = #{0}
	</update>
	
</mapper>

映射接口:

// 映射接口
public interface AccountDao {
	//更新余额
	void update(int accountId, double balance);
}

oracle.properties内容:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
user=system
password=123456789

Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 1. 读取src下数据库配置文件oracle.properties,然后下面可以用${key}去引用文件中的value值了 -->
    <context:property-placeholder location="classpath:oracle.properties" />

    <!-- 2.dbcp数据源 -->
    <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>${driver}</value>
        </property>
        <property name="url">
            <value>${url}</value>
        </property>
        <property name="username">
            <value>${user}</value>
        </property>
        <property name="password">
            <value>${password}</value>
        </property>
    </bean>

    <!-- 3.配置sqlSessionFactory -->
    <!-- 3.1不使用mybatis-config.xml配置文件 -->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.au.db"></property>
        <property name="configurationProperties">
            <props>
                <prop key="cacheEnabled">true</prop>
            </props>
        </property>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations"
            value="classpath:com/au/db/mybatis/AccountMapper.xml" />
    </bean>

    <!-- 3.2 使用mybatis-config.xml-->
    <!-- 直接读取mybatis-config.xml文件,里面和之前配置的一样,就是mybatis-config.xml中不用配置数据源 -->
    <!--
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />        
        <property name="configLocation"  value="classpath:mybatis-config.xml"/>
    </bean>
    -->

    <!-- 4.自动扫描映射接口所在的包以及这个包下面子包,找到映射接口,将来可以通过接口的名字首字母小写作为beanName,从spring容器中拿出自动生成的该接口的实现类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.au.db" />
    </bean>

    <!-- 最关键就是第3和4步骤,第3步用来配置mybatis文件,第4步用来处理接口与映射文件的映射,然后直接getBean(接口名首字母小写) -->
</beans>

测试代码:

@Test
public void mybatis_update(){
    try {
        String path = "com/au/db/mybatis/spring_mybatis.xml";
        ClassPathXmlApplicationContext container = new ClassPathXmlApplicationContext(path);
        
        //注意,获取的是 接口的实现类对象,获取后可以通过该对象调用里面的方法
        AccountDao dao = (AccountDao) container.getBean("accountDao");
        dao.update(1, 1260);
        System.out.println(dao.getClass());
        
        container.destroy();
    }catch(Exception e) {
        e.printStackTrace();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值