ssm框架整合的简单实现与常见异常
文章目录
1.环境配置(pom.xml)
mysql 8.0.26
tomcat9.0.37 (实测使用tomcat7.*可能会导致玄学bug)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zjy</groupId>
<artifactId>ssmDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.19.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--日志 start-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<!--日志end-->
<!--Jackson包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<!--fast-json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<finalName>ssmDemo</finalName>
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
2.resources配置
2.1.数据库设置(mysql.properties)
driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306?useSSL=true&userUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username=root
password=123456
2.2框架配置
2.2.1.mybatis-config.xml
<?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 核心配置文件-->
<configuration>
<!--数据源改在spring配置-->
<!-- <environments default="development">-->
<!-- <environment id="development">-->
<!-- <!–事务管理–>-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <!–配置数据源–>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>-->
<!-- <property name="username" value="root"/>-->
<!-- <property name="password" value="123456"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- </environments>-->
<mappers>
<mapper resource="com/zjy/mapper/BookMapper.xml"/>
</mappers>
</configuration>
2.2.2.spring-dao.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:aop="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/dtd/spring-beans.dtd
http://mybatis.org/dtd/mybatis-3-mapper.dtd"
>
<context:annotation-config/>
<context:component-scan base-package="com.zjy.mapper"/>
<context:property-placeholder location="classpath*:properties/mysql.properties"/>
<!-- spring-jdbc整合包集成的DataSource -->
<bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="Url" value="${jdbc.url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!--C3P0 DataSource-->
<bean id="C3P0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"/>
<property name="password" value="${password}"/>
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="acquireRetryAttempts" value="0"/>
</bean>
<bean name="sessionFactoryBuilder" class="org.apache.ibatis.session.SqlSessionFactoryBuilder">
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactoryBean">
<property name="sqlSessionFactoryBuilder" ref="sessionFactoryBuilder"/>
<property name="dataSource" ref="C3P0dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean name="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactoryBean"/>
</bean>
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.zjy.mapper"/>
</bean>
</beans>
2.2.3.spring-service.xml
<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:aop="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/dtd/spring-beans.dtd
http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<context:component-scan base-package="com.zjy.service"/>
<context:annotation-config/>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" name="dataSourceTransactionManager">
<property name="dataSource" ref="C3P0dataSource"/>
</bean>
<bean name="bookServiceImpl" class="com.zjy.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"/>
</bean>
</beans>
2.2.4.spring-servlet.xml(springmvc配置)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:annotation-config/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.zjy.controller"/>
<context:component-scan base-package="com.zjy.service"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" name="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2.2.5.将配置文件整合为一个ApplicationContext.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:aop="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/dtd/spring-beans.dtd
http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.zjy"/>
<!--开启注解的支持,注册驱动-->
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="springmvc-servlet.xml"/>
</beans>
3.java目录
3.1.pojo
Book.java
package com.zjy.pojo;
public class Book {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
public Book() {
}
public Book(int bookID) {
this.bookID = bookID;
}
public Book(int bookID, String bookName, int bookCounts, String detail) {
this.bookID = bookID;
this.bookName = bookName;
this.bookCounts = bookCounts;
this.detail = detail;
}
public int getBookID() {
return bookID;
}
public void setBookID(int bookID) {
this.bookID = bookID;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookCounts() {
return bookCounts;
}
public void setBookCounts(int bookCounts) {
this.bookCounts = bookCounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
3.2.mapper
3.2.1.BookMapper接口
package com.zjy.mapper;
import com.zjy.pojo.Book;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public interface BookMapper {
@Insert("INSERT INTO`book`" +
"VALUES(#{bookID},#{bookName},#{bookCounts},#{detail});")
Book addBook(Map map);
@Delete("DELETE FROM`book`WHERE`bookID`=#{id};")
void deleteBookById(int bookID);
@Update("UPDATE `books`.`book`" +
"SET `bookName`=,`bookCounts`=,`detail`= WHERE`bookID`=5;")
void updateBook(HashMap map);
@Select("SELECT*FROM`books`.`book`;")
List<Book> getAll();
@Select("SELECT*FROM`books`.`book`WHERE`bookID`=#{id};")
List<Book> queryBookById(@Param("bookID") int id);
}
3.2.2.BookMapper.xml(备用,用于后续添加复杂CRUD方法)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zjy.pojo.BookMapper" >
</mapper>
3.3.service类
3.3.1.BookService接口
package com.zjy.service;
import com.zjy.mapper.BookMapper;
import com.zjy.pojo.Book;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface BookService {
BookMapper bookMapper = null;
Book addBook(Map map);
void deleteBookById(int bookID);
void updateBook(HashMap map);
List<Book> getAll();
List<Book> queryBookById(int id);
}
3.3.2.BookServiceImpl
package com.zjy.service;
import com.zjy.mapper.BookMapper;
import com.zjy.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BookServiceImpl implements BookService{
@Autowired
@Qualifier(value = "bookMapper")
private BookMapper bookMapper;
public BookMapper getBookMapper() {
return bookMapper;
}
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public Book addBook(Map map) {
return bookMapper.addBook(map);
}
@Override
public void deleteBookById(int bookID) {
bookMapper.deleteBookById(bookID);
}
@Override
public void updateBook(HashMap map) {
bookMapper.updateBook(map);
}
@Override
public List<Book> getAll() {
return bookMapper.getAll();
}
@Override
public List<Book> queryBookById(int id) {
return bookMapper.queryBookById(id);
}
}
3.4.controller方法
package com.zjy.controller;
import com.zjy.pojo.Book;
import com.zjy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
@Qualifier("bookServiceImpl")
private BookService bookService;
@RequestMapping("/allBooks")
public String getAll(Model model) {
List<Book> list= bookService.getAll();
model.addAttribute("bookList",list);
return "/allBooks";
}
}
4.简单的前端测试页面
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<h3>
<a herf="http://localhost:8080/ssmDemo_war_exploded/book/allBooks"target="_blank">进入书籍页面</a>
<form target="_blank" action="http://localhost:8080/ssmDemo_war_exploded/book/allBooks">
<button type="submit">
跳转
</button>
</form>
</h3>
</body>
</html>
allBooks.jsp
<%--
Created by IntelliJ IDEA.
User: 13373273827
Date: 2022/5/25
Time: 21:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍展示</title>
</head>
<body>
</body>
</html>
5.数据库
6.脱离tomcat的简单测试方法
import com.zjy.mapper.BookMapper;
import com.zjy.pojo.Book;
import com.zjy.service.BookService;
import com.zjy.service.BookServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class AppTest {
@Test
public void test01() throws IOException {
//当dao层和service层配置存在问题时,无需部署服务器即可在控制台看到报错提示
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
BookServiceImpl bookService= (BookServiceImpl) applicationContext.getBean("bookServiceImpl");
SqlSessionFactory sqlSessionFactory= (SqlSessionFactory) applicationContext.getBean("sqlSessionFactoryBean");
SqlSession sqlSession=sqlSessionFactory.openSession();
BookMapper bookMapper=sqlSession.getMapper(BookMapper.class);
List<Book> list;
list=bookMapper.getAll();
System.out.println(list.toString());
sqlSession.close();
}
}
7.过程中可能遇到的问题
7.1Could not autowire. No beans of ‘xxxx’ type found
控制台报错如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through field 'bookMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zjy.mapper.BookMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier("bookMapper")}
原因:bean无法自动装配
解决方案:(建议在spring-dao.xml中)加入如下bean:
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.zjy.mapper"/>
</bean>
或按控制台提示追溯具体类的配置。
7.2.java.sql.SQLException
控制台报错:
java.sql.SQLException: Access denied for user 'HP'@'localhost' (using password: YES)
如即在properties文件中用户名使用了’root’且密码正确的情况下仍遇到此问题,可将用户名直接在xml里写死成"root",具体原因不明,此处存疑。
7.3.本地方法查询正常,跳转到测试页面时提示404
原因:基本可以确定是mvc配置问题,如将web.xml中的DispatcherServlet配置成:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:mybatis-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
或将bean配置成:
<bean class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactoryBean">
<property name="sqlSessionFactoryBuilder" value="sessionFactoryBuilder"/>
<property name="dataSource" ref="C3P0dataSource"/>
<property name="configLocation" value="mybatis-config.xml" />
</bean>
正确的配置:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--在不导致循环依赖的情况下优先选择整合好的xml更加稳妥 -->
<param-value>classpath*:ApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactoryBean">
<!-- 调用上文配置好的bean应用“ref”属性 -->
<property name="sqlSessionFactoryBuilder" ref="sessionFactoryBuilder"/>
<property name="dataSource" ref="C3P0dataSource"/>
<!-- 路径前应加"classpath:"或"classpath*:" -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
ybatis-config.xml" />
正确的配置:
```xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--在不导致循环依赖的情况下优先选择整合好的xml更加稳妥 -->
<param-value>classpath*:ApplicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" name="sqlSessionFactoryBean">
<!-- 调用上文配置好的bean应用“ref”属性 -->
<property name="sqlSessionFactoryBuilder" ref="sessionFactoryBuilder"/>
<property name="dataSource" ref="C3P0dataSource"/>
<!-- 路径前应加"classpath:"或"classpath*:" -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>