美文网首页
笔记搭建ssm环境配置

笔记搭建ssm环境配置

作者: 忞触动心灵 | 来源:发表于2019-05-11 11:03 被阅读0次

使用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>

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配置

<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.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>

作者:吕小凯
链接:https://www.jianshu.com/p/3abb5e438139
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

相关文章

网友评论

      本文标题:笔记搭建ssm环境配置

      本文链接:https://www.haomeiwen.com/subject/gotxaqtx.html