SSM整合的关键就是spring整合Mybatis,而整合Mybatis的关键就是获取sqlSession对象
## 继承SqlSessionDaoSupport类来获取sqlSession对象
public class AccountDaoImpl extends SqlSessionDaoSupport implements IAccountDao {
@Override
public List<Account> findAll() {
//继承SqlSessionDaoSupport来获取SqlSession
SqlSession session = super.getSqlSession();
//获得dao接口的代理对象
IAccountDao accountDao = session.getMapper(IAccountDao.class);
//执行操作
return accountDao.findAll();
}
//之后要将此类注入到ioc容器中,再将SqlSessionFactory注入到该Bean
}
配置文件db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456789
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载applicationContext.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器,创建该Servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--解决方法:设置配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern><!--拦截所有-->
</filter-mapping>
</web-app>
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描器-->
<context:component-scan base-package="controller"></context:component-scan>
<!--增加对注解的支持-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置要跳转文件对应的路径-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--配置后缀为.jsp的文件为要跳转的文件-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--使前端控制器不拦截静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
spring.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描器:扫描dao包和service包-->
<context:component-scan base-package="dao;service" >
</context:component-scan>
<!--加载配置文件-->
<context:property-placeholder location="classpath:properties/db.properties"></context:property-placeholder>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--spring整合mybatis的核心代码->基于sql映射文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置sql的映射文件-->
<property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
</bean>
<!--生成单个mapper对象-->
<bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--mapper接口-->
<property name="mapperInterface" value="dao.AccountDao"></property>
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
/*********************************同时生成多个mapper对象**********************/
<!--生成多个mapper对象-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"></property>
/********************基于使用mybatis注解的mapper接口的整合********************/
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--批量生成生成mapper对象-->
<bean id="accountDao" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定哪个包批量生成mapper对象-->
<property name="basePackage" value="com.it.dao"></property>
</bean>
</beans>