记录一次搭建简单SSM框架的较详细过程。共本人学习记录交流使用,如有错误或建议恳请批评指正。
整体思路:
- 创建maven的webapp工程;
- 为pom文件添加依赖,如:springmvc,spring-context,spring-mybatis 和 druid 等;
- 编写webapp.xml,配置:dispatcherServlet,编码过滤器 和 spring容器监听器;
- 在resources资源文件夹根目录下创建并编写spring_mvc_config.xml配置文件,配置:静态资源放行,视图解析器,包扫描Controller 等;
- 同一目录创建并编写spring_config.xml配置文件,配置:druid,Mybatis,MapperScannerConfiger 等;
- 在Mapper扫描的路径下(我一般为Dao的相关包)添加XxxDaoMapper.xml文件
搭建步骤:
一、 pom文件
创建maven,webapp工程,在pom文件中添加的依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring 相关一套!-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!--jackson jason转换 重要,@ResponseBody json转换时默认jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<!--mybatis整合spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--阿里,德鲁伊-->
<dependency>
<groupId>com.alibaba.druid</groupId>
<artifactId>druid-wrapper</artifactId>
<version>0.2.8</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
注意:在build标签中添加代码,当程序打包部署时,让maven在指定路径下中搜索配置文件,不然打包好的jar/war中缺少配置文件。一般我习惯将数据库映射文件与dao接口放在一起,maven打包和build项目时可能不去找java下的配置文件,这里提供一种解决方式。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
二、web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--springMVC前端核心控制器,拦截所有请求-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_mvc_config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--编码过滤器,springmvc已经封装好了,用就完了-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置一个监听器,在tomcat启动时创建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_config.xml</param-value>
</context-param>
</web-app>
三、springMVC_config.xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/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">
<!-- 静态资源放行,dispatcherServlet无法完成的请求交给tomcat,
使用默认的servlethandler处理-->
<mvc:default-servlet-handler/>
<!-- 注解开发-->
<mvc:annotation-driven/>
<!-- 扫描包,只扫描Controller组件-->
<context:component-scan base-package="com.qf.ssmpro" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 视图解析器,简化return的视图名-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
四、spring_config.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描除Controller以外的其他Spring组件,比如:Service-->
<context:component-scan base-package="com.qf.ssmpro" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 扫描Druid配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!-- Druid数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="validationQuery" value="select 1"/>
<property name="testOnBorrow" value="false"/>
<property name="testWhileIdle" value="true"/>
</bean>
<!-- mybatis的会话工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.qf.ssmpro.entity"/>
<property name="mapperLocations">
<list>
<value>classpath:com/qf/ssmpro/dao/*.xml</value>
</list>
</property>
</bean>
<!-- mapper扫描器-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.ssmpro.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 事务管理器-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务注解开发-->
<tx:annotation-driven transaction-manager="tx" />
</beans>
此外需要一个properties文件作为druid的配置文件(略),被Spring_config.xml扫描到 。其中配置了驱动名,url,数据库用户名密码等信息。
五、XxxDaoMapper.xml配置文件
此文件中映射了dao接口的方法。
<?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.qf.ssmpro.dao.EmpDao">
<select id="selectAll" resultType="Emp">
select * from test.t_emp
</select>
</mapper>
测试:
项目目录
浏览器请求,dispatcherServlet拦截,通过Controller调用service,mapper映射dao将查询到的数据库数据返回,最后jackson将数据转换后返回给浏览器
<a href="emp/findAllEmp">测试</a>
Controller
@RequestMapping("/findAllEmp")
@ResponseBody
public List<Emp> findAllEmp(){
return empService.findAllEmps();
}
Service
@Override
public List<Emp> findAllEmps(){
return empDao.selectAll();
}
Dao
public interface EmpDao {
List<Emp> selectAll();
}
Mapper文件上方已经贴了
点击 “测试” , 结果:
输出了数据库的真实信息