一、配置pom.xml文件
依赖如下:
<!-- 统一管理jar包的版本号 -->
<properties> <spring-version>4.3.2.RELEASE</spring-version> <spring-aspectj>1.9.2</spring-aspectj> <mybatis-version>3.4.6</mybatis-version> <myabtis-spring-version>1.3.2</myabtis-spring-version> <mysql-version>5.1.38</mysql-version> <c3p0-version>0.9.5.2</c3p0-version> <slf4j-log4j-version>1.7.25</slf4j-log4j-version> <jsp-api-version>2.1</jsp-api-version> <servlet-version>3.1.0</servlet-version> <jstl-version>1.2</jstl-version> <jackson-version>2.9.9</jackson-version> <junit-version>4.12</junit-version> <pagehelper-version>5.1.2</pagehelper-version> </properties> <!-- 依赖jar包 --> <dependencies> <!-- ssm --> <!-- spring web mvc(会自动依赖spring的四大核心包,日志,aop) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> <!-- spring:jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-version}</version> </dependency> <!-- spring:tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-version}</version> </dependency> <!-- aspectj --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${spring-aspectj}</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis-version}</version> </dependency> <!-- mybatis-spring的整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${myabtis-spring-version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-version}</version> </dependency> <!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${c3p0-version}</version> </dependency> <!-- slf4j-log4j:日志实现 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j-log4j-version}</version> </dependency> <!-- jsp --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api-version}</version> <!-- provided:表示如果出现包的冲突,maven会自动忽略我们引入的jar包 --> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl-version}</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson-version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson-version}</version> </dependency> <!-- spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> <scope>test</scope> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit-version}</version> <scope>test</scope> </dependency> <!--pageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper-version}</version> </dependency> </dependencies> <!-- 添加tomcat插件运行 --> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <!-- 设置端口号 --> <port>8080</port> <!-- 设置访问的url,如果是斜线,表示访问路径中不带项目名 --> <path>/</path> <!-- get请求编码格式是utf-8 --> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> <build>
当出现java目录下的资源文件无法访问时,在pom.xml中 <build> 标签内的再加上资源文件读取的配置:
<!-- 通过tomcat插件启动项目,需要将mapper.xml也加载进去 如果配置文件(xml还是properties文件)都放在classpath目录下是不会有问题的,一旦放在src目录下,必须手动定义读取位置 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources>
二、配置mybatis的配置文件
<?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> <!--打印日志可以看执行的sql语句--> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!-- 配置分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> </configuration>
其中日志文件的配置如下:
log4j.rootLogger=DEBUG,A1 log4j.logger.org.mybatis=DEBUG log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
三、配置Spring的applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 引入数据库属性配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置连接池 --> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 创建SqlSessionFactory,交给spring管理 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="myDataSource"></property> <!-- 加载mybatis的核心配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- mapper接口通过自动扫描的方式交给spring管理 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描的包地址 --> <property name="basePackage" value="cn.kgc.mapper"></property> <!-- 注入sqlSeessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 配置spring的事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据源 --> <property name="dataSource" ref="myDataSource"></property> </bean> <!-- 定义传播行为/事务的策略 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*"/> <tx:method name="update*"/> <tx:method name="delete*"/> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="select*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 定义切面 --> <aop:config> <!-- 切入点 --> <aop:pointcut expression="execution(* cn.kgc.service.impl.*.*(..))" id="myPointcut"/> <!-- 切面 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/> </aop:config> </beans>
在使用context方式引用数据库配置文件时,配置文件最好使用多层命名,否则容易与内部命令重名导致无法读取:
my.driver=com.mysql.cj.jdbc.Driver my.url=jdbc:mysql://localhost:3306/XXX?serverTimezone=UTC&useSSL=false my.username=root my.password=root
四、配置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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <!-- 开启注解 --> <mvc:annotation-driven> <mvc:message-converters> <ref bean="stringHttpMessageConverter"/> <ref bean="mappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <!-- 配置扫描器 --> <context:component-scan base-package="org.kgc"></context:component-scan> <!-- 当接受到的请求没有对应的@RequestMapping时,交由Tomcat默认的servlet处理,一般用于访问静态资源 --> <mvc:default-servlet-handler/> <!-- 配置数据格式化 注解 所依赖的bean,在该bean中也可以用来配置类型转换器 --> <bean id="formattingConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean> <!-- 配置视图解析器 前缀为/views/ 后缀为.jsp 该解析器渲染成jsp页面 如果是包含jstl的视图会自动转换为对应的JstlView --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 解决浏览器使用json文件的转换异常问题 --> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"></bean> </beans>
五、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>MavenWeb</display-name> <welcome-file-list> <welcome-file>index</welcome-file> </welcome-file-list> <!-- needed for ContextLoaderListener --> <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> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <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>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring自带了一个过滤器HiddenHttpMethodFilter,支持GET、POST、PUT、DELETE请求 --> <filter> <filter-name>HiddenHttpMethod</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethod</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
至此,ssm所需的配置基本完成。
来源:https://www.cnblogs.com/709539062rao/p/12557838.html