Spring《Spring与Mybatis结合一》

6 篇文章 0 订阅

将Mybatis与Spring结合可以把Service与Servlet分离开来

而且在Mybatis的配置文件中environments和mappers都可以写在Spring-Mybatis的配置文件中

首先导包


第一部分:在之前使用mybatis框架时,需要写一个SqlSessionFactory工厂来获取SqlSession,在工厂里需要ReadSource取读取Mybatis.xml配置文件,其中配置文件的environments标签里有datasource属性。

现在我们通过写一个Spring-Mybatis.xml配置文件将ReadSource和datasource放入

datasource的配置:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		  destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/student_db" />
		<property name="user" value="root" />
		<property name="password" value="a635226968" />
	</bean>

在mybatis-spring-1.2.2.jar包中有一个SqlSessionFactoryBean类


他有两个参数

private Resource configLocation;
private DataSource dataSource;
他的返回值类型是SqlSessionFactory类型(虽然他没有父类,但是在实例化的过程中自动调用的getObject方法,返回值类型就是 SqlSessionFactory),所以我们需要在配置文件中配置他并传参

注:value值里面加“classpath:”表示该文件在编译的根目录下

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:MyBatisConfig.xml"></property>
		<property name="mapperLocations">
			<array value-type="java.lang.String">
				<value>classpath:com/web/map/*.xml</value>
			</array>
		</property>
	</bean>

好了 这时候mybatis.xml的配置文件中的environments标签就可以去掉了

这时可以在Servlet的init方法中写

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
再启动tomcat,查看日志文件可以发现容器已经把刚才的文件加载了,此时通过

SqlSession sqlSessin = ((SqlSessionFactory)context.getBean("sqlSessionFactory")).openSession();
就可以获取SqlSession了
还可以通过监听servletcontext的方法,通过监听器去加载spring-mybatis.xml文件,这时候需要去配置web.xml文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>
配置之后在servlet初始化的时候就不需要这个了,他会自动加载进去,那么如何获取并使用呢?

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
这时候需要用一个工具类WebApplicationContextUtils的静态方法getWebApplicationContext(),传入的参数是ServletContext,为的是获取上下文的内容。

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
第二部分:ok接下来就想想怎么通过spring把mapper.xml和mapper接口加载到容器里面了

还是一样,查看mybatis-spring-1.2.2.jar包,其中有一个MapperScannerConfigurer(mapper的扫描配置)


这个类有两个属性


第一个是含有mapper的包,第二个不用解释了

这时候去spring-mybatis.xml配置进去

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <property name="basePackage" value="com/web/map"></property>
   <property name="SqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
就可以将mybatis.xml里面的mapper标签注释掉了

mybatis.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>

	
	<typeAliases>
	    <package name="com/web/po"/>
        <package name="com/web/bean"/>
	</typeAliases>
    
</configuration>
spring-mybatis.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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:cache="http://www.springframework.org/schema/cache" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	   http://www.springframework.org/schema/aop
	   http://www.springframework.org/schema/aop/spring-aop-4.3.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-4.3.xsd
	   http://www.springframework.org/schema/tx  
       http://www.springframework.org/schema/tx/spring-tx.xsd  
       http://www.springframework.org/schema/cache  
       http://www.springframework.org/schema/cache/spring-cache.xsd
	   ">
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		  destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/student_db" />
		<property name="user" value="root" />
		<property name="password" value="a635226968" />
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:MyBatisConfig.xml"></property>
		<property name="mapperLocations">
			<array value-type="java.lang.String">
				<value>classpath:com/web/map/*.xml</value>
			</array>
		</property>
	</bean>
	<!-- <bean id="sqlSession" factory-bean="sqlSessionFactory" factory-method="openSession"></bean> -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com/web/map"></property>
		<property name="SqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
</beans>
ok基础配置基本完成


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值