spring总结5——Spring和mybatis整合、spring和servlet整合

目录

1、Spring和mybatis整合

1、整合什么东西?

2、需要的jar包

3、mybatis配置文件内容

4、spring配置文件内容

1、开启注解扫描

2、配置C3p0连接池

3、配置SqlSessionFactory

4、配置MapperScannerConfigurer

5、代码汇总:

2、spring和servlet整合

1、整合什么

2、整合步骤

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

2、提供BaseServlet在该类继承HttpServlet,并且重写init方法,在init方法中获取IOC容器,并且提供一个方法getBean()用来获取IOC容器中的bean


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

(9) ..........

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>
	<settings>
		<!-- 设置对mybatis全局的配置 -->
		<!-- 允许插入null -->
		<setting name="jdbcTypeForNull" value="NULL" />
	</settings>
	<!-- 起别名 -->
	<typeAliases>
		<typeAlias type="wendi.entity.User" alias="user" />
	</typeAliases>
	<mappers>
		<mapper resource="wendi/dao/UserDaoMapper.xml" />
	</mappers>
</configuration>

4、spring配置文件内容

1、开启注解扫描

	<!-- 开启注解扫描 -->
	<context:component-scan base-package="wendi"></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:mybatis.xml"></property>
	</bean>

4、配置MapperScannerConfigurer

	<!-- 配置 MapperScannerConfigurer -->
	<!-- 让spring自动帮我们生成dao层接口实现类,并且放到IOC容器中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
		<!-- 指定接口所在的位置 -->
		<property name="basePackage" value="com.zl.dao"></property>
	</bean>

5、代码汇总:

<?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-4.1.xsd">
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="wendi"></context:component-scan>

	<!-- 引入外部资源文件 -->
	<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>

	<!-- 配置sqlsessionfactory -->
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 把连接池交给该对象 -->
		<property name="dataSource" ref="dataSouser"></property>
		<!-- 设置mybatis主配置文件 -->
		<property name="configLocation" value="classpath:mybatis.xml"></property>
	</bean>

	<!-- 配置 MapperScannerConfigurer -->
	<!-- 让spring自动帮我们生成dao层接口实现类,并且放到IOC容器中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
		<!-- 指定接口所在的位置 -->
		<property name="basePackage" value="com.zl.dao"></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:spring*.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容器中获取

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值