网页左边,向下滑有目录索引,可以根据标题跳转到你想看的内容 |
---|
1、整合介绍
- springMVC框架主要用于实现MVC设计模式,MyBatis负责持久层业务操作数据库,而spring负责管理springMVC和MyBatis
- 但是springMVC是spring的一个子模块,所以spring和springMVC本身不用整合,我们需要考虑的仅仅是如何整合spring和MyBatis
- 而myBatis官方以经为我们提供了整合使用的jar包mybatis-spring
2、环境搭建(Maven)
- 使用Maven导入相关jar包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- SpringMvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--Spring JDBC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--Spring AOP和Spring aspects-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!-- Mabatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--Spring & MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.2</version>
</dependency>
<!--C3P0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
3、配置web.xml
- web.xml文件配置信息
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置spring-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springMvc的DispatchServlet 前端控制器-->
<servlet>
<servlet-name>dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatchServlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符编码过滤器-->
<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>
<!--加载静态资源-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
</web-app>
- 配置spring
- 配置SpringMvc的dispatchServlet前端控制器
- 配置characterEncodingFilter字符编码过滤器
- 配置静态资源
4、整合spring和MyBatis
- 整合spring和mybatis就是将mybatis交给spring管理,而mybatis提供的整合包mybatis-spring,可以帮我们轻松完成整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--spring整合myBatis-->
<!--配置数据源,这里用连接池c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root" />
<property name="jdbcUrl" value="jdbc:mysql//localhost:3306/mywebsite?userUnicode=true&characterEncoding=UTF-8" />
<property name="password" value="123456" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="initialPoolSize" value="5" />
<property name="maxPoolSize" value="10" />
</bean>
<!--获取SqlSession-->
<!--1、获取SqlSessionFactory 通过整合包mybatis-spring下的SqlSessionFactoryBean类-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config.xml"/>
<property name="mapperLocations" value="classpath:com/yzpnb/dao/*.xml" />
</bean>
<!--2、将接口扫描到spring-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yzpnb.dao" />
</bean>
</beans>
- 配置mybatis的核心配置文件,可以不配,因为主要内容全部配置给spring管理了,而mybatis的核心配置文件只需要配置一些辅助信息即可
5、配置前端控制器
6、idea配置pom.xml
<!--使idea的maven可以扫描到xml-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
7、测试查询方法
- 根据数据库建立三层模型
8、常见错误
-
1、SSM框架遇到异常Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0’意思是找不到cache
- 这是因为idea在配置前端控制器时,标签 < !–开启注解驱动–>< mvc:annotation-driven></mvc:annotation-driven>自动导入了错误信息,将此标签删除,并重新导入即可
-
2、com/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z is abstract4
- 表示c3p0依赖出错,重新配置pom.xml
-
3、所有数据源信息都正确,但还是连接不上数据库
- 如果确定不是你的信息有问题,可以检测下你的数据库版本,是否和你所配置的jar包匹配,比如5.0版本数据库,使用了和我一样的8.0的jar包,就会无法连接
-
4、Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid b,找不着接口
- 意思就是no find ,没有发现你的mapper映射文件,一般会说你的dao层接口怎么这么没找到,比如no find com.XXX.dao.SsmDao.selAll()这样的
- 这是因为你的xml配置文件放在了pom.xml找不到的地方,idea必须配置xml文件的位置