首先,我们需要导如相关jar包,并且build Path一下
看一下我们demo的总体结构
接下来配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID"
version="2.5"
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">
<display-name>ssm2</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>
<!-- 导入spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置编码过滤器-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器-->
<servlet>
<servlet-name>ssm</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>ssm</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
导入log4j日志文件
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
给出数据库链接配置文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=1234
接下来首先配置dao层
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 事务 -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引入连接池-->
<property name="dataSource" ref="dataSource" />
<!-- 配置数据源 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean>
<!-- 配置mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置mapper扫描包 -->
<property name="basePackage" value="com.it.lpp.mapper"></property>
</bean>
</beans>
然后是service层
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置扫描service -->
<context:component-scan base-package="com.it.lpp.service"></context:component-scan>
</beans>
sqlMapconfig开启别名
<?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>
<package name="com.it.lpp.pojo"/>
</typeAliases>
</configuration>
接下来配置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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!--静态资源放行 -->
<mvc:default-servlet-handler />
<!--配置controller扫描 -->
<context:component-scan base-package="com.it.lpp.controller" />
<mvc:annotation-driven />
<context:property-placeholder location="classpath:resources.properties" />
<!--配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--异常处理器 -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!--设置错误视图 -->
<property name="defaultErrorView" value="error"></property>
<!-- 在错误页面,获取异常信息对象变量名称。缺省exception 前端使用e{ex}取得 -->
<property name="exceptionAttribute" value="ex"></property>
</bean>
<!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven />
</beans>
配置大致完成,接下来我们开始一个测试
在mapper.xml文件中书写好sql语句,并且绑定到mapper接口中去
service层接口
package com.it.lpp.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.it.lpp.mapper.UserInformationDao;
import com.it.lpp.pojo.UserVo;
@Service
public class UserInformationServiceImpl implements UserInformationService {
@Autowired
private UserInformationDao userInformationDao;
public List<UserVo> selectUserInformationByAll(){
return userInformationDao.selectUserInformationByAll();
}
public void updateUserInformation(UserVo user) {
userInformationDao.updateUserInformation(user);
}
public void deleteInformationByName(UserVo user) {
// TODO Auto-generated method stub
userInformationDao.deleteInformationByName(user);
}
}
实现接口,使用自动装配一个UserInformationDao对象
在controller层中创建一个处理类
装配一个UserInformationService对象
调用我们写好的查询方法并输出
浏览器中输入http://localhost:8080/ssm2/user/list
查看控制台
可以看到 查询完成
在此过程中遇到的一个问题,
ssm框架使用5.0版本jar包链接数据库的时候,会报无法链接,但单独不使用框架,或者单个框架的时候,没用出现这种情况,或许和你的数据库版本有关,换一个匹配的数据库版本的sql包试试。
总结:
1,配置的时候,首先需要配置dao层,也就是mybatis的mapper文件以及sqlMapconfig.xml文件
使用spring开启扫描,并且在spring中配置 数据库,事务,sqlSessionFactory
2.在Service层的配置中 开启扫描注解
3.在mvc.xml中配置controller层,例视图解析器,处理器映射器,处理器适配器,处理器,异常处理器,以及文件上传,拦截器等。
看似复杂,但只要弄清了脉络,瞬间就能掌握