整合ssm实现简单的增删改查。。。
整合ssm一般不笼统的将三个框架一块整合,首先将Spring+mybatis整合。
第一步:新建maven项目,导入jar包依赖
第二步:需要pojo实体类,dao层接口,dao层映射文件,控制层,业务层,以及业务层的实现类
资源文件,需要有mybatisConfig.xml、applicationContext-mybatis.xml,applicationContext-user.xml,springmvc.xml。
第三步:创建User:
package pers.llj.model;
public class User {
private int id;
private String username;
private String age;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String username, String age) {
super();
this.id = id;
this.username = username;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", age=" + age + "]";
}
}
第四步:创建UserMapper.java
package pers.llj.mapper;
import java.util.List;
import pers.llj.model.User;
/**
* 作为dao接口
* @author lenovo
*
*/
public interface UserMapper {
void insert(User user);
boolean update(User user);
boolean delete(int id);
User findById(int id);
List<User> findAll();
}
第五步:实现UserMapper
即UserMapper.xml(补充说明,以下的namespace必须与对应的接口名一致,UserMapper.java,id要与UserMapper中的方法同名)
<?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">
<!-- namespace: 必须与对应的接口类名一致,UserMapper.java
id 必须与对应接口的某个对应的方法名一致,必须要和UserMapper.java中的 方法同名
-->
<mapper namespace="pers.llj.mapper.UserMapper">
<!-- 增加用户 -->
<insert id="insert" parameterType="User">
insert into t_user(user_name,user_age) values(#{username},#{age})
</insert>
<!-- 删除用户 -->
<delete id="delete" parameterType="int">
delete from t_user where user_id=#{id}
</delete>
<!-- 修改用户 -->
<update id="update" parameterType="User">
update t_user set user_name=#{username},user_age=#{age} where user_id=#{id}
</update>
<!-- 根据id查用户 -->
<select id="findById" parameterType="int" resultType="User">
select user_id id,user_name username,user_age age from t_user where user_id=#{id}
</select>
<!-- 查询全部 -->
<select id="findAll" resultType="User">
select user_id id,user_name username,user_age age from t_user
</select>
</mapper>
第六步:然后对mybatis和spring进行整合
首先新建appliactionContext-mybatis.xml文件,此文件中需要创建数据源,配置数据库连接基本信息,配置数据库连接池相关信息,创建sqlSession,配置事务管理器,在事务管理器中注入数据源,使用声明式事务,启用注解实现事务管理,扫描dao层接口
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- mybatis和spring整合 -->
<!-- 第一步:加载数据库配置文件 -->
<!-- <context:property-placeholder location="db.properties" /> -->
<!-- 第2步:创建数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 配置数据库连接基本信息 -->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=Asia/Shanghai"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<!-- 配置数据库连接池相关信息 -->
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="5" />
<property name="minIdle" value="2" />
<property name="maxActive" value="10" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="10000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
<!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
<property name="defaultAutoCommit" value="true" />
<!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
<property name="validationQuery" value="select 1" />
</bean>
<!-- 第三步创建sqlSession -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatisConfig.xml"></property>
<property name="mapperLocations" value="classpath:pers/llj/mapper/*.xml"></property>
</bean>
<!-- 第四步:配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 第五步:使用声明式事务 ,启用注解实现事务管理 -->
<!-- 定义事务通知 -->
<tx:advice id="userAdvice" transaction-manager="transactionManager">
<!-- 定义方法对应的事务属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" isolation="READ_COMMITTED"
read-only="false" timeout="-1" rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED"
read-only="false" timeout="-1" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED"
read-only="false" timeout="-1" rollback-for="Exception" />
<tx:method name="find*" propagation="SUPPORTS" read-only="false"
rollback-for="Exception" />
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"
read-only="false" timeout="-1" rollback-for="Exception" />
<tx:method name="" />
</tx:attributes>
</tx:advice>
<!-- 扫描dao层接口 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="pers.llj.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
</bean>
</beans>
第七步:创建service接口以及实现类,将service注入
package pers.llj.service;
import java.util.List;
import pers.llj.model.User;
public interface UserService {
void insert(User user);
boolean update(User user);
boolean delete(int id);
User findById(int id);
List<User> findAll();
}
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.llj.mapper.UserMapper;
import pers.llj.model.User;
import pers.llj.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public void insert(User user) {
userMapper.insert(user);
}
@Override
public boolean update(User user) {
return userMapper.update(user);
}
@Override
public boolean delete(int id) {
return userMapper.delete(id);
}
@Override
public User findById(int id) {
return userMapper.findById(id);
}
@Override
public List<User> findAll() {
List<User> findAllList = userMapper.findAll();
return findAllList;
}
}
新建applicationContext-user.xml将service注入
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- service -->
<bean id="userServiceImpl" class="pers.llj.service.impl.UserServiceImpl">
<!-- 将dao注入进来 -->
<property name="userMapper" ref="userMapper"></property>
</bean>
</beans>
第八步:配置mybatis的配置文件起别名
<?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>
<!-- 设置别名 -->
<typeAliases>
<typeAlias type="pers.llj.model.User" alias="User"/>
</typeAliases>
</configuration>
此时,可以进行,mybatis和spring整合完的测试,看能否通过
新建测试的包,编写测试类
package pers.llj.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pers.llj.model.User;
import pers.llj.service.UserService;
public class SpringMybatisTest {
@Test
public void Demo1(){
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext-mybatis.xml","applicationContext-user.xml"});
UserService userService = (UserService) context.getBean("userServiceImpl");
List<User> list = userService.findAll();
for (User user : list) {
System.out.println(user);
}
}
@Test
public void Demo2(){
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext-mybatis.xml","applicationContext-user.xml"});
UserService userService = (UserService) context.getBean("userServiceImpl");
User user=new User();
user.setUsername("llj");
user.setAge("21");
user.setId(10);
boolean b = userService.update(user);
System.out.println(b);
}
@Test
public void Demo3(){
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext-mybatis.xml","applicationContext-user.xml"});
UserService userService = (UserService) context.getBean("userServiceImpl");
boolean b = userService.delete(10);
System.out.println(b);
}
}
增删改查,全部通过说明没有问题,至此,spring 和 mybatis整合完成。进行下一步:和springmvc进行整合。
第九步:新建springmvc.xml,放到resources路径下
配置注解扫描,以及视图解析器
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="pers.llj.controller"></context:component-scan>
<!-- <context:component-scan base-package="pers.llj.service"></context:component-scan> -->
<!-- 视图解析器 ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
第十步:配置web.xml文件(此处的contextConfigLocation是必须配置的,否则无法加载到ioc容器中)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>SSM01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置监听器 -->
<!-- 在servlet上下文中配置ioc容器文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 通过监听器加载ioc容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置过滤器 -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第十一步:编写控制层,并启用注解
package pers.llj.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pers.llj.model.User;
import pers.llj.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
/**
* 获取所有的用户列表
*
* @param request
* @param model
* @return
*/
@RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request, Model model) {
List<User> user = userService.findAll();
model.addAttribute("userList", user);
request.setAttribute("userList", user);
return "allUser";
}
/**
* 跳转到添加用户界面
*
* @param request
* @return
*/
@RequestMapping("/toAddUser")
public String toAddUser() {
return "addUser";
}
/**
* 添加用户并重定向
*
* @param user
* @param request
* @return
*/
@RequestMapping("/addUser")
public String addUser(User user, Model model) {
userService.insert(user);
return "redirect:/user/getAllUser";
}
/**
* 编辑用户
*
* @param user
* @param request
* @return
*/
@RequestMapping("/updateUser")
public String updateUser(User user, HttpServletRequest request, Model model) {
if (userService.update(user)) {
user = userService.findById(user.getId());
request.setAttribute("user", user);
model.addAttribute("user", user);
return "redirect:/user/getAllUser";
} else {
return "error";
}
}
/**
* 根据id查询单个用户
*
* @param id
* @param request
* @return
*/
@RequestMapping("/getUser")
public String getUser(int id, HttpServletRequest request, Model model) {
request.setAttribute("user", userService.findById(id));
model.addAttribute("user", userService.findById(id));
return "editUser";
}
/**
* 删除用户
*
* @param id
* @param request
* @param response
*/
@RequestMapping("/delUser")
public String delUser(int id) {
userService.delete(id);
return "redirect:/user/getAllUser";
}
}
至此,springmvc基本整合完成,此时编写前端页面
第十一步:前端首页的编写
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath%>">
<title>首页</title>
</head>
<body>
<h5>
<a href="<%=basePath%>user/getAllUser">进入用户管理页</a>
</h5>
</body>
</html>
第十二步:前端用户列表页面编写
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
<title>用户列表</title>
</head>
<body>
<h6>
<a href="<%=basePath%>user/toAddUser">添加用户</a>
</h6>
<table border="1">
<tbody>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:if test="${!empty userList }">
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.username }</td>
<td>${user.age }</td>
<td><a href="<%=basePath%>user/getUser?id=${user.id}">编辑</a>
<%-- <a href="javascript:del('${user.id }')">删除</a></td> --%>
<a href="<%=basePath%>user/delUser?id=${user.id}">删除</a></td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>
</body>
</html>
第十三步:前端页面修改页面编写:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>编辑用户</title>
<script type="text/javascript">
function updateUser(){
var form = document.forms[0];
form.action = "<%=basePath%>user/updateUser";
form.method = "post";
form.submit();
}
</script>
</head>
<body>
<h1>编辑用户</h1>
<form action="" name="userForm">
<input type="hidden" name="id" value="${user.id }" /> 姓名:<input
type="text" name="username" value="${user.username }" /> 年龄:<input
type="text" name="age" value="${user.age }" /> <input type="button"
value="编辑" onclick="updateUser()" />
</form>
</body>
</html>
至此:整合完成,注意如果使用到jquery的话,一定要引入库。
数据库的话,sql语句自行添加,此处将数据表粘上来。
最后,还有一些,我整合过程中遇到的问题
在测试完mybatis和spring整合结果后,没有问题,但是添加springmvc之后会出现如下错误:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ... 21 more 严重: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170) at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:908) at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:884) at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836) at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579) at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115) at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4774) at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5411) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:226) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1403) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1393) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
类似于这种问题,或者说无法加载你的db.properties的问题,此时,请进行以下三步:
第一:请查看你的web.xml中是否在servlet上下文中配置ioc容器文件,即如下四行
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
此文件在配置的时候要写到配置文件的上面。
第二:查看你UserMapper上的@Autowired或者@Resource是否有加,如下图:
第三:将你的db.properties换成传统的土方法直接配置到applicationContext-mybatis.xml中。
如果以上三个方法均不行,那么请检查代码,不能加载配置文件的问题,本人还未彻底摸清,有结果了会第一时间更贴,此贴写给迷茫的初学者。
以下配本工程的页面截图:
以上!!