Spring和mybatis整合、spring和servlet整合

1、Spring和mybatis整合

1、整合什么东西?

把mybatis里面涉及到的对象交给spring管理:连接池,SqlSessionFactory,管理生成dao成 接口实现类

2、需要的jar包

(1) Mybatis核心

(2) 数据驱动包

(3) C3p0连接池jar

(4) Spring核心

(5) springAOP

(6) springWeb

(7) Spring事务管理

(8) Spring和mybatis整合jar

3、mybatis配置文件内容

(1) 对mybatis整体配置:settings

(2) 起别名

(3) 引入mapper映射文件

<?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.sunwenxu.bean"></package>
    </typeAliases>

    <!--<mappers>-->
        <!--<mapper resource="com/sunwenxu/dao/User.xml"></mapper>-->
    <!--</mappers>-->

</configuration>

4、spring配置文件内容

1、开启注解扫描
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com/sunwenxu"></context:component-scan>
2、配置C3p0连接池
	<!-- 引入外部资源文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<!--  配置数据源(连接池)-->
	<bean id="dataSouser" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 用户名 -->
		<property name="user" value="${jdbc.username}"></property>
		<!-- 密码 -->
		<property name="password" value="${jdbc.password}"></property>
		<!-- 驱动地址 -->
		<property name="driverClass" value="${jdbc.driver}"></property>
		<!-- 数据库连接地址 -->
		<property name="jdbcUrl" value="${jdbc.url}"></property>
	</bean>
3、配置SqlSessionFactory
	<!-- 配置sqlsessionfactory -->
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 把连接池交给该对象 -->
		<property name="dataSource" ref="dataSouser"></property>
		<!-- 设置mybatis主配置文件 -->
		<property name="configLocation" value="classpath:sqlMapconfig.xml"></property>
	</bean>
4、配置MapperScannerConfigurer
	<!--
        Mapper扫描:从Mapper包中扫描出Mapper接口,自动创建代理对象并在spring容器中注册
        遵循规范:Mapper.java接口和Mapper.xml映射文件,文件名一致,并且在同一个目录下
        扫描出来的Mapper的bean的名字为Mapper的类名,首字母小写
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--指定扫描的包,如果扫描多个包,每个包之前用半角的逗号分隔-->
        <property name="basePackage" value="com.sunwenxu.mapper"></property>
    </bean>
5、代码汇总:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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-4.2.xsd
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com/sunwenxu"></context:component-scan>
    <!--C3P0连接池-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <bean class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactory">
        <property name="configLocation" value="classpath:sqlMapconfig.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--
        Mapper扫描:从Mapper包中扫描出Mapper接口,自动创建代理对象并在spring容器中注册
        遵循规范:Mapper.java接口和Mapper.xml映射文件,文件名一致,并且在同一个目录下
        扫描出来的Mapper的bean的名字为Mapper的类名,首字母小写
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!--指定扫描的包,如果扫描多个包,每个包之前用半角的逗号分隔-->
        <property name="basePackage" value="com.sunwenxu.mapper"></property>
    </bean>
</beans>

2、spring和servlet整合

1、整合什么

因为service层的所有对象都放在IOC容器中,那么我们在servlet中使用service对象时必须从IOC容器中取

2、整合步骤

1、由tomcat帮我们创建IOC容器

在web.xml中如下配置:

	<context-param>
		<!-- 通过该标签配置的键值对最终是会放到ServletContext(application)中 -->
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value> 
	</context-param>
	<!-- 配置启动IOC容器的监听器:当ServletContext对象创建的时候启动IOC容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 配置过滤器,利用所提供jar包里面的资源 org.springframework.web.filter.CharacterEncodingFilter -->
	<filter>
		<filter-name>charSetFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>charSetFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
2、提供BaseServlet在该类继承HttpServlet,并且重写init方法,在init方法中获取IOC容器,并且提供一个方法getBean()用来获取IOC容器中的bean
package com.zl.servlet;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
 
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
 
public class BaseServlet extends HttpServlet {
 
	private ApplicationContext app;
 
	@Override
	public void init() throws ServletException {
		app = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	}
 
	public Object getObject(Class c) {
		return app.getBean(c);
	}
}

之后所有的servlet继承BaseServlet,servlet里面用到service的时候通过getBean()方法从IOC容器中获取

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值