SSM配置简化版

  1. 在pom.xml中导入需要的jar包。
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.38</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>5.0.8.RELEASE</version>
	</dependency>

	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-aspects</artifactId>
	    <version>5.0.8.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis</artifactId>
	    <version>3.4.6</version>
	</dependency>
	<dependency>
	    <groupId>org.mybatis</groupId>
	    <artifactId>mybatis-spring</artifactId>
	    <version>2.0.2</version>
	</dependency>
	<dependency>
	    <groupId>com.mchange</groupId>
	    <artifactId>c3p0</artifactId>
	    <version>0.9.5.2</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-tx</artifactId>
	    <version>5.0.8.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-jdbc</artifactId>
	    <version>5.0.8.RELEASE</version>
	</dependency>
  </dependencies>
  1. 在SSM项目启动时,会首先读取web.xml文件,接下来配置web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	>

   <!-- 监听并加载spring配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <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>
  	<init-param>
  		<param-name>forceResponseEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>characterEncodingFilter</filter-name>
  	<url-pattern>/</url-pattern>
  </filter-mapping>
  
  <!-- 配置springMvc前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>configLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
  1. 根据web.xml的配置,接下来会加载spring的配置文件。
<?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
        https://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
        ">
    <!-- 注解扫描 -->
    <context:component-scan base-package="com.tpblog">
    	<context:exclude-filter type="annotation" expression="org.springframework.web.servlet.mvc.Controller"/>
    </context:component-scan>
    <!-- 配置c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
    	<property name="user" value="root"></property>
    	<property name="password" value=""></property>
    </bean>
    <!-- 配置mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="configLocation" value="classpath:mybatis.xml"></property>
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 扫描mapper接口 -->
    <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<property name="basePackage" value="com.tpblog.mapper"></property>
    	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
</beans>
  1. 配置springmvc文件。其实spring和springmvc可以配置在一个文件中,但随着项目功能的增加,配置文件中的内容也会随之增多,因此,为了使配置文件更清晰,方便阅读,springmvc将单独配置。配置文件中只做了一些主要配置,如果需要更多的功能(例如文件上传等),可以自行配置。
<?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
        https://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
        ">
    <!-- 注解扫描 -->
 	<context:component-scan base-package="com.tpblog.controller">
 		<context:include-filter type="annotation" expression="org.springframework.web.servlet.mvc.Controller"/>
 	</context:component-scan>
 	 <!-- 配置视图解析器 -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/views"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
 	<!-- 释放静态文件 -->
 	<mvc:default-servlet-handler/>
 	<!-- 开启注解驱动 -->
    <mvc:annotation-driven/>
</beans>
  1. 配置mybatis。在spring的配置文件中做了mybatis的相关配置,如指明mybatis.xml的位置,扫描mapper接口等。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="mapUnderscoreToCamelCase" value="true"/>
  </settings>
  <!-- 为实体配置别名 -->
  <typeAliases>
  	<package name="com.tpblog.entity"/>
  </typeAliases>
  <mappers>
  	<!-- 指定mapper.xml文件所在的包,本项目中mapper接口和对应的xml文件放在相同包下 -->
    <package name="com.tpblog.mapper"/>
  </mappers>
</configuration>

你可能感兴趣的:(java,spring,后端,mysql,mybatis)