1.3Spring与MyBatis整合

将MyBatis与Spring进行整合,主要解决的问题就是将SqlSessionFactory对象交由Spring来管理。所以,该整合,只需要将SqlSessionFactory的对象生成器SqlSessionFactory的对象生成器SqlSessionFactoryBean注册在Spring容器中,再将其注入给Dao的实现类即可完成整合。

方式:

  1. Mapper动态代理
  2. 支持扫描的Mapper动态代理

 

主要是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" 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"> <!-- bean definitions here -->

    
    <!-- 注册配置属性文件信息,方式二 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
     

    <!-- 注册数据源 : c3p0连接池
    	   什么时候创建的连接池?什么时候销毁?需要常见多少?需要根据业务逻辑进行配置
    -->
     <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
     	<property name="driverClass" value="com.mysql.jdbc.Driver"/> 
    	<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/springdaotest"/> 
    	<property name="user" value="root"/> 
     	<property name="password" value="root"/> 
     </bean> 
    
    <!-- 注册SqlsessionFactory工厂 -->
    <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<!-- 注入配置文件mybatis.mxl -->
    	<property name="configLocation" value="classpath:mybatis.xml"/>
    	<!-- 注入数据源 -->
    	<property name="dataSource" ref="myDataSource"/>
    </bean>
    
	<!-- 生成 Dao的代理对象-->
	<!-- 
		相当于  IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
	 -->
	<!-- 
	<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="mySqlSessionFactory"/>
		<property name="mapperInterface" value="edu.sdut.dao.IStudentDao"/>
	
	</bean>
	 -->
	 <!-- 
	 	在上面的代码中,如果为每一个Dao接口都注册的话,就会使得配置文件变得臃肿,
	 	那个有了下面,按包注册的方式
	 	注意下面这个是没有id的
	  -->
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/>
		<property name="basePackage" value="edu.sdut.dao"/>
	
	</bean>
	  
	<!-- 注册Service -->
	<bean id="studentService" class="edu.sdut.service.StudentServiceImpl">
		<!-- 注入dao, -->
		<!-- <property name="dao" ref="studentDao"/> -->
		<!-- 如果使用按包扫描的话,由于没有了id,
			需要的ref属性为:
				如果Dao的接口的前两个字母是大写,则这了的值为接口的简单类名
				如果Dao的接口的首字母是大写,第二个字母是小写,则这了的值为简单类名,但首字母要小写
			
		 -->
		<property name="dao" ref="IStudentDao"/>
	</bean>
</beans>
<?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="edu.sdut.bean"/>
	</typeAliases>
	
	<!-- 事务管理器用Spring的,所以不用配environments -->
	<!-- 
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
	</environments>
	 -->
	<mappers>
	
		<!-- <mapper resource="edu/sdut/dao/mapper.xml" /> -->
		<!-- 使用包的话,需要将mapper的名字和接口的相同 -->
		<package name="edu.sdut.dao"/>
	</mappers>
</configuration>
<?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="edu.sdut.dao.IStudentDao">
	<insert id="insertStudent">
		insert into student(name,age) values(#{name},#{age})
	</insert>
	
	<delete id="deleteById">
		delete from student where id=#{id}
	</delete>
	
	<update id="updateStudent">
		update student set name='xxx',age=13 where id=#{id}
	</update>
	
	<select id="selectAllStudents" resultType="Student">
		select * from student
	</select>
	
	<select id="selectStudentById" resultType="Student">
		select * from student where id=#{id}
	</select>
</mapper>

Spring与Web整合

在哪里获取Spring容器对象,才能保证整个应用只获取一次(本来是再Servlet中,但是每次请求都会走Servlet导致每次都获取到一个不同的Spring容器对象)?

创建监听器,在监听器中获取Spring容器对象,把对象放到application域中。

 

Spring容器已经创建好了监听器,所以不需要再自己创建。需要引入spring-web.jar。

然后配置web.xml,注册ServletContext监听器,完成两件事:

  1. 在ServletContext被创建时,创建Spring容器对象
  2. 将创建好的Spring容器对象放入到ServletContext的域属性空间中

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

 

注册完监听器后,也就是创建完了Spring容器对象,存放到servletContext中。如果在Servlet中需要,那么就(ApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) ,获得Spring容器对象。

猜你喜欢

转载自blog.csdn.net/txgANG/article/details/81412608