目录html
一、整合什么东西?web
二、须要的jar包spring
一、开启注解扫描tomcat
二、配置C3p0链接池session
三、配置SqlSessionFactorymybatis
四、配置MapperScannerConfigurerapp
二、提供BaseServlet在该类继承HttpServlet,而且重写init方法,在init方法中获取IOC容器,而且提供一个方法getBean()用来获取IOC容器中的bean
把mybatis里面涉及到的对象交给spring管理:链接池,SqlSessionFactory,管理生成dao成 接口实现类
(1) Mybatis核心
(2) 数据驱动包
(3) C3p0链接池jar
(4) Spring核心
(5) springAOP
(6) springWeb
(7) Spring事务管理
(8) Spring和mybatis整合jar
(9) ..........
(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>
<!-- 开启注解扫描 --> <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>
<?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>
由于service层的全部对象都放在IOC容器中,那么咱们在servlet中使用service对象时必须从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>
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容器中获取