SSM(Spring+Spring MVC+MyBatis)
1 简介
SSM 包含三大框架:
Spring MVC (web level),采取 MVC 架构,意图取代麻烦的 Servlet 写法,简化 web 层
MyBatis (dao level),意图取代 jdbc 操作数据库,轻量级、灵活、高效
Spring,提供了 IoC(容器,用来管理组装对象) 和 AoP(切面,用来自动增强对象功能) 功能,它用来将对象和其他框架组织在一起.
2 首先,创建项目
推荐使用 Maven 或者 Gradle 创建 ssm 项目。
可以选择在 Idea 下面,创建 Maven 或 Gradle 项目即可。
3 其次,增加依赖(jar 包)
Maven 的配置文件是 pom.xml,想为项目添加依赖,只需要向 pom.xml 增加相关配置即可。
示例:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>147</groupId> <artifactId>my_ssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>my_ssm Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!-- 用来单元测试的jar包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!--spring mvc 的依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.0.RELEASE</version> </dependency> <!--mybatis jar 包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!--数据库相关 jar 包--> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--其他--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Jackson架包--> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.7</version> </dependency> <!--日志架包log4j--> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <finalName>my_ssm</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
点击IDEA刷新工具即自动下载jar包并配置,或点击Import Changes
基本只需要下面几个配置文件:
- resources/mybatis.xml, mybatis 的核心配置文件
- resources/spring-root.xml, spring 的核心配置文件
- resources/spring-web.xml, spring mvc 的配置文件,可以配置 springmvc 相关的东西
- web.xml, 因为是 web 项目,必须要配置 web.xml,让服务器启动的时候加载 spring 和 spring mvc
- log4j.properties,用来定义日志输出
mybatis.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"> <configuration> <settings> <setting name="logImpl" value="LOG4J"/> </settings> </configuration>
spring-root.xml 示例:
ps:本示例命名为spring_root.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 http://www.springframework.org/schema/context/spring-context.xsd"> <!--@Component/@Service/@Repository/@Controller--> <context:component-scan base-package="com.gdnf.ssm.service" /> <!--创建数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="org.mariadb.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mariadb://localhost:3306/bookdb?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true" /> <property name="user" value="root" /> <property name="password" value="dz520123" /> </bean> <!--配置 mybatis--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis.xml" /> <property name="mapperLocations" value="classpath:com.gdnf.ssm.dao/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="com.gdnf.ssm.dao" /> </bean> </beans>
spring-web.xml 示例:
ps:本示例的命名为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:contxt="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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描包下面所有的 @Controller 注解的类,并将其实例化,放入容器--> <contxt:component-scan base-package="com.gdnf.ssm.web" /> <!--启用 mvc 的常用注解--> <mvc:annotation-driven /> <!--将所有的静态资源,交给 Servlet 处理--> <mvc:default-servlet-handler /> <!--配置 jsp view解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
web.xml 示例,用来驱动 spring 和 spring mvc:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--全局,注意各标签的顺序--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring_root.xml</param-value> </context-param> <!--编码过滤器--> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置 Spring 的容器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置 MVC 容器--> <!--将所有的请求,都交给 Spring MVC 处理--> <servlet> <servlet-name>app</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> </servlet> <servlet-mapping> <servlet-name>app</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
log4j.properties,用来配置日志:
# 全局配置: 只显示错误级别的日志,输出为名字为 stdou 的日志 log4j.rootLogger=ERROR, stdout # MyBatis 的日志配置,只输出 com.nf147.bookstore_ssm.dao 包下产生 INFO 以及以上级别的日志 #TRACEINFO log4j.logger.com.gdnf.ssm.dao=TRACE # 定义名字为 stdout 的日志,将日志输出到控制台 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
目录结构图
然后就可以创建服务器,启动之了
之后就可以在 idea 里面创建应用服务器,将项目部署到上面,运行之。