ssm框架文件配置

1 简介

  • 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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.nf147</groupId>
<artifactId>bookstore-ssm</artifactId>
<version>0.01</version>
<packaging>war</packaging>

<name>bookstore-ssm Webapp</name>
<url>http://nf147.github.io</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>

<!--spring 所需要的 jar 包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>

<!--spring mvc 的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<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>

<!--用来单元测试的 jar 包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>bookstore-ssm</finalName>
</build>
</project>

4 之后,增加配置文件

基本只需要下面几个配置文件:
  • 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 示例:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd">

<!--@Component/@Service/@Repository/@Controller-->
<context:component-scan base-package="com.nf147.bookstore_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/lagou" />
<property name="user" value="vip" />
<property name="password" value="vip" />
</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:mapper/*.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.nf147.bookstore_ssm.dao" />
</bean>

<!--配置事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven proxy-target-class="true" />

<!-- AspectJ -->
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>

spring-web.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--扫描包下面所有的 @Controller 注解的类,并将其实例化,放入容器-->
<context:component-scan base-package="com.nf147.bookstore_ssm.web" />

<!--启用 mvc 的常用注解-->
<mvc:annotation-driven />

<!--将所有的静态资源,交给 Servlet 处理-->
<mvc:default-servlet-handler />

<!--配置视图解析器,使用 jsp 来渲染视图-->
<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:

<?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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

<display-name>ssm demo</display-name>

<!--配置 Spring 的容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-root.xml</param-value>
</context-param>
<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-web.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 以及以上级别的日志
log4j.logger.com.nf147.bookstore_ssm.dao=error

# 定义名字为 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
  • 然后,创建相关的目录结构,如图:

 

 下载地址:https://github.com/nongzihong/SSM.git

 

posted @ 2018-11-21 14:45  随★风  阅读(431)  评论(0编辑  收藏  举报