Maven+Tomcat8+Idea搭建SSM框架
集成Spring、Spring MVC
项目结构如下所示:
Spring部分:
1. web.xml
a. 监听器(在启动web容器时加载)
2. spring.xml
a. 扫描所有除了controller包的其他包
b. 声明式事
web.xml代码: 3.1 version
<?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">
<!-- spring 基于web应用的启动-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--全局参数:spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
</web-app>
spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描所有除了controller包的其他包-->
<context:component-scan base-package="com.vidor">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
Spring MVC部分:
1. web.xml
a. 前端调度器servlet
b. 编码过滤器filter
2. springmvc.xml
a. 扫描controller包
b. 添加<annotation-driver>
c. 视图解析器
d. 静态资源解析
3. 添加控制器类
web.xml springmvc部分 接上面
<!--前端调度器servlet-->
<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.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>
<!--编码过滤器filter -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--设置编码格式 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<servlet-name>dispatcherServlet</servlet-name>
</filter-mapping>
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描controller包 -->
<context:component-scan base-package="com.vidor.controller">
</context:component-scan>
<!-- 添加<annotation-driver>-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 添加视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp" />
</bean>
<!-- <mvc:resources mapping="" location=""/>-->
</beans>
pom.xml
<properties>
<SPRING.VERSION>5.2.7.RELEASE</SPRING.VERSION>
</properties>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${SPRING.VERSION}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${SPRING.VERSION}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${SPRING.VERSION}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${SPRING.VERSION}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${SPRING.VERSION}</version>
</dependency>
集成Mybatis
1. 需要和spring整合
a. 将sqlSessionFactory 配置为spring的bean
i. 数据源 配置为spring的bean
ii. 全局配置文件
iii. 所有mapper映射文件
b. 将mapper接口的包交给spring
2. 加入全局配置文件
加入到spring.xml:
<!--引入外部属性资源文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--创建Druid数据源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="username" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
<property name="url" value="${mysql.url}"></property>
<property name="driverClassName" value="${mysql.driverClassName}"></property>
</bean>
<!--声明式事-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--基于驱动-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--用于声明事务切入的所有方法-->
<aop:config>
<aop:pointcut id="transactionCut" expression="execution(* com.vidor.service.impl.*.*(..))"/>
</aop:config>
<!--用来明确切点匹配到的方法哪些方法需要使用事务-->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!--可以使用通配符-->
<tx:method name="add*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="get*" read-only="true" propagation="SUPPORTS"></tx:method>
<tx:method name="query*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!--SqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 指定spring中的数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:com/vidor/mapper/*.xml"></property>
</bean>
<!--将mapper接口交给spring管理-->
<mybatis:scan base-package="com.vidor.dao"></mybatis:scan>
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">
<!--就是DOCTYPE后面对应的根节点-->
<configuration>
<!--mybatis的设置选项 可以改变mybatis运行时行为-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写-->
<typeAliases>
<package name="com.vidor.entity"/>
</typeAliases>
</configuration>
IDEA tomcat运行
- 需要建一个Artifacts
- 需要将引入的资源包放到war中,可参考: https://blog.csdn.net/chenwen0326/article/details/108288731
- 添加tomcat,如果运行CATALINA_BASE报错,可以做如下配置:
项目总目录结构:
GitHub: https://github.com/vidorchan/ssm