Spring-Mybatis整合

Spring-Mybatis整合

    一、整合的内容

     1、SqlSessionFactory将放入Spring容器中作为单例存在

    2、Mapper代理,将从Spring容器中直接获取mapper代理对象

     3、数据源和数据库连接池的事务管理都将有由Spring进行管理

     二、整合操作

        数据源:ApplicationContext.xml中加载数据库配置文件db.properties

  jdbc.driver=com.mysql.jdbc.Driver
        jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&useSSL=false
        jdbc.username=root

        jdbc.password=123456
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>
</beans>

        将SqlSessionFactory归于Spring管理

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定Mybatis核心配置文件 凡是Spring配置文件都得加classpath -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
        <!-- 指定回话工厂使用的数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

   三、 简单的实现

    简单的Dao实现
<!-- 原生dao实现
		注意:class必须指定Dao的实现类的全路径 -->
	<bean id="userDao" class="org.lier.dao.UserDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>

    这种方式下实现的条件:

    1、有dao借口,并且还要有相关的实现类,实现类需要继承org.mybatis.spring.support.SqlSessionDaoSupport类。

    2、class的值必须指定dao的实现类的全路径

    public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    @Override
    public List<User> selectUsersByName(String username) {
//        session线程不安全,放入放入方法体中,随用随毁
        SqlSession session = this.getSqlSession();
        List<User> users =session.selectList("test.findByName",username);
        session.commit();
        session.close();
        return users;
    }
}

    测试方法;

private ApplicationContext ctx;
	
	@Before
	public void setUp() throws Exception{
		String configLocation = "ApplicationContext.xml";
		ctx = new ClassPathXmlApplicationContext(configLocation);
		
	}
	
	@Test
	public void selectUsersByNameTest() {
//		获取UserDao对象,getBean中的字符串是在ApplicationContext.xml中声明的
		UserDao userdao = (UserDao) ctx.getBean("userDao");
		List<User> users  = userdao.selectUsersByName("王五");
		System.out.println(users.toString());
	} 

        问题以及注意:

        上面的实现类中手动的commit了,但是报错了

  java.lang.UnsupportedOperationException:Manual commit is not allowed over a Spring managed SqlSession

    大致意思是在和Spring整合之后,是不允许手动的Commit的。将手动commit的语句去掉就可以正常的执行了。完美!加油W~!W

    Mapper代理的方式:单个以及扫描方式
Mapper动态代理的方式
	<!-- 动态代理方式Mapper方式实现 -->
	<bean id="userDaoMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<!-- 配置Mapper接口的全路径  -->
		<property name="mapperInterface" value="org.lier.mapper.UserDaoMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	<!-- 使用包扫描的方式批量引入Mapper -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定要扫描的包的全路径,如果有多个包,用英文状态下的逗号进行分割 -->
		<property name="basePackage" value="org.lier.mapper"></property>
	</bean>
@Test
	public void findOrderToUser2Test() {
//		获取UserDao对象,getBean中的字符串是在ApplicationContext.xml中声明的
		UserDaoMapper mapper = (UserDaoMapper) ctx.getBean("userDaoMapper");
		List<Orders> orders = mapper.findOrderToUser2();
		System.out.println(orders.toString());
		
	}

    总结下啊!

        1、整合中数据源不在mybatis控制,内容大概一致,不难理解。

        2、重要的一点SqlSessionFactory由Spring进行注入(单例),在Mybatis中,必须获取了这个factory才能获取session对象,执行sql。

        3、实现方式:

        在单个Mybatis中dao实现手动加载配置文件,创建factory,获取session,执行,然后提交关闭,整合的时候,提供实现类,通过获取bean获取相关的实现类进行操作,这其中,SQLSessionFactory已经被Spring接管了注入了,直接获取session,执行,不需要commit。

       代理模式下:都省去了实现类了,Mybatis指定Mapper接口两者在同一目录下,在整合版中指定mapper接口、会话工厂。综上就是说就是将Mybatis中需哟啊手工创建的factory让Spring接管了。然后在Spring中指定相关的mapper接口,就可以通过bean获取相关的接口的代理类了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring-MyBatis整合是一种常见的开发方式,其中spring-mybatis.xml文件是用来配置SpringMyBatis框架集成的配置文件。在这个文件中,我们会定义数据源、事务管理器、扫描Mapper接口等配置信息。同时,我们还需要将MyBatisSqlSessionFactory注入到Spring容器中,以便其他组件可以使用它来执行数据库操作。以下是一个简单的spring-mybatis.xml文件示例: ```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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="com.example.entity" /> <property name="mapperLocations" value="classpath*:com/example/mapper/*.xml" /> </bean> <!-- 配置Mapper扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启Spring的注解功能 --> <context:annotation-config /> <!-- 开启Spring的AOP功能 --> <aop:aspectj-autoproxy /> <!-- 开启Spring的事务管理功能 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans> ``` 在这个配置文件中,我们使用了Spring的注解功能、AOP功能和事务管理功能,以及MyBatis的Mapper扫描和SqlSessionFactory配置。其中,数据源、事务管理器和Mapper扫描的配置信息需要根据实际情况进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值