使用maven快速构建ssm环境
1.pom依赖包引入
<dependencies> <!--导入Spring,SpringMVC基本包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!--导入jdbc驱动包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!--导入aspectj驱动包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.1.5.RELEASE</version> </dependency> <!--导入Mybatis驱动包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!--导入Mybatis整合Spring驱动包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--导入连接池驱动包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.3</version> </dependency> <!--导入数据库驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!--导入jstl驱动包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--导入servlet驱动包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!--导入junit驱动包--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--导入Spring整合junit驱动包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.5.RELEASE</version> <scope>test</scope> </dependency> </dependencies>
2.web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 配置spring上下文参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置编码过滤器(只对post方法) --> <filter> <filter-name>charactorEncoding</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>charactorEncoding</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>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
3.配置springmvc.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" >xmlns:p="http://www.springframework.org/schema/p" 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"> <!-- 1、配置自定控制器扫描路径 --> <!--这里配置的是controller的路径--> <context:component-scan base-package="com.qf.*"> </context:component-scan> <!-- 2、配置静态资源处理 --> <mvc:default-servlet-handler /> <!-- 3、配置注解驱动 --> <mvc:annotation-driven> <mvc:message-converters> <bean >class="org.springframework.http.converter.StringHttpMessageConverter>"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--4、配置视图解析器--> <bean >class="org.springframework.web.servlet.view.InternalResourceViewRes>olver"> <property name="prefix" value="/"></property> <!--添加后缀--> <property name="suffix" value=".html"></property> </bean> </beans>
4.spring-service.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->4.3.xsd"> <!-- 扫描包并将这个包下面所有的类托管到spring容器 --> <context:component-scan base-package="com.qf.service">></context:component-scan> </beans>
5.spring-mybatis.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:tx="http://www.springframework.org/schema/tx" >xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans >http://www.springframework.org/schema/beans/spring-beans-4.3.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"> <!-- 1、配置加载数据源参数文件; --> <context:property-placeholder >location="classpath:mysql.properties"/> <!-- 2、配置数据源连接池 --> <bean id="dataSource" >class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}">></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"> </property> <property name="password" value="${jdbc.password}"> </property> </bean> <!-- 3、配置sessionFactoryBean --> <bean id="sessionFactory" >class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" >value="classpath:mapper/*.xml"></property> <property name="typeAliasesPackage" value="com.qf.pojo"> </property> </bean> <!-- 4、配置mybatis接口扫描器 --> <bean >class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.qf.mapper"> </property> <property name="sqlSessionFactoryBeanName" >value="sessionFactory"></property> </bean> <!-- 5、配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionMa>nager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 6、注解配置事务扩散机制 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
6.mysql.properties配置
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/demo?>useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B>8&useSSL=false jdbc.username=root jdbc.password=root
7.UserMapper.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qf.mapper.UserMapper"> </mapper>
完成上述配置后环境就搭配好了(以上部分存在bug,需要删除>)