ssm分别是spring(springframework)、springmvc、mybatis。
1、在pom.xml中引入依赖,共计13个。根据需求或多或少。
测试 | Junit、Spirng-test |
工具 | lombok、druid、jackson、jstl、javax-servlet.api |
框架 | spring-webmvc、spring-aop、mybatis-spring、mabatis |
数据库 | spring-jdbc、mysql-connection-java |
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring.version>5.3.14</spring.version>
<mybatis-spring.version>2.0.6</mybatis-spring.version>
<mybatis.version>3.5.7</mybatis.version>
<druid.version>1.2.8</druid.version>
<mysql.version>8.0.27</mysql.version>
<jackson.version>2.13.1</jackson.version>
<servlet.version>4.0.1</servlet.version>
<lombok.version>1.18.20</lombok.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.13.2</junit.version>
</properties>
<dependencies>
<!-- spring mvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring - jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis - spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--jackson解码-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!--spring - test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
2、配置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_4_0.xsd"
version="4.0">
<!-- 为监听器提供初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 避免出现内存溢出-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Context容器装载-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解码问题-->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定mvc配置文件路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 服务器启动阶段初始化-->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3、逻辑层配置文件mybatis.xml。首先实例化逻辑层和数据层,添加数据库连接池和基于数据源的事务管理器。实例化SqlSessionFactory对象,配置数据源、各sql语句包以及实体类对象。然后整合spring和mybatis,通过调用数据层接口的方法,就可以实现调用SqlSessionFactory对象,然后找到对应的配置文件和数据源,通过数据库连接池对象从数据库获得数据返回。
<?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:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描并实例化,排除控制层-->
<context:component-scan base-package="com.zhang.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 读取配置文件,获得数据库连接参数-->
<context:property-placeholder location="classpath:datasource.properties"/>
<!-- 创建durid数据库连接池-->
<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}"/>
<property name="maxActive" value="${druid.maxActive}"/>
<property name="minIdle" value="${druid.minIdle}"/>
<property name="initialSize" value="${druid.initialSize}"/>
</bean>
<!-- 基于数据源的事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 传入数据源-->
<constructor-arg ref="dataSource"/>
</bean>
<!-- 为基于注解型事务提供注解支持-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 把mybatis下的容器交由ioc容器管理-->
<bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描指定路径下的所有配置文件-->
<property name="mapperLocations" value="classpath:/mappers/*.xml"/>
<!-- 为实体类包类型提供简化别名-->
<property name="typeAliasesPackage" value="com.zhang.ssm.*.entity"/>
<!-- 提供配置-->
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!-- 启用驼峰命名-->
<property name="mapUnderscoreToCamelCase" value="true"/>
<!-- 向控制台执行输出sql-->
<property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>
</bean>
</property>
</bean>
<!-- Spring 与 mybatis整合-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描指定路径,实现匹配数据层接口-->
<property name="basePackage" value="com.zhang.ssm.*.dao"/>
<!-- 注入mybatis sqlsessionfactory-->
<property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"/>
</bean>
</beans>
4、mvc配置文件,只需要实例化控制层接口即可。
<context:component-scan base-package="com.zhang.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>