使用IDEA搭建ssm框架

使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架css

开源git源码地址:前端

https://gitee.com/kaisheng100/ssmdemo.git

Spring

Spring框架是整个系统架构的核心,将前端请求数据的处理以及数据库的数据操做整合在一块儿,很是重要。java

SpringMVC

SpringMVC框架用于处理系统中数据的流转及控制操做。 (从哪里来,到哪里去。多么有哲理的一句话。)git

MyBatis

Mybatis框架主要处理业务和数据库之间的数据交互,因此建立对象和管理对象生命周期的职责能够委托Spring框架完成。web

1、建立项目

进入IDEA界面,点击File >>> New >>>Project 按照以下图示操做便可spring

在Maven选项卡里面找到对应的java web选项,而后咱们点下一步

这一步填入组织等信息,这里比较随意,按照本身的需求进行填写 sql

这里我早已配置好本地Maven仓库,所以直接默认便可。若是没进行配置本地默认仓库的话,请网上查找对应的资料进行配置

输入Project name,和须要保存的路径,而后finish
稍等片刻,idea自动建立须要点时间。

注意这里修改下 数据库

建立目录结构不在叙述,建立结果以下图所示,pom依赖配置参照git源码 spring-mvc

配到这里,项目建立部分已经完成!!!

集成Spring框架

Spring框架是整个系统架构的核心,将前端请求数据的处理以及数据库的数据操做整合在一块儿,很是重要。tomcat

在web.xml配置以下

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring核心配置文件 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>

<!-- 处理POST提交乱码问题 -->
<filter>
	<filter-name>encoding</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>encoding</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 指定配置文件位置和名称 若是不设置,默认找/WEB-INF/<servlet-name>-servlet.xml -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/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>
复制代码

ContextLoaderListener 全部监听器主要用于监听对象或服务器状态的变化,侧重于变化

在tomcat服务器启动的时候,监听到后,按照上下文参数的配置初始化spring容器

param-name中的contextConfigLocation不可更改,spring框架会根据这个id找到配置文件,一旦更改容器没法初始化

applicationContext.xml配置的即为spring的初始化策略 SpringMVC环境构建时须要读取servlet初始化参数init-param, 从classpath中读取配置文件spring/springmvc.xml

spring容器 和 springmvc容器初始化完成会将 spring容器设置为springmvc容器的父容器

在applicationContext.xml配置以下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context" 
	   xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:aop="http://www.springframework.org/schema/aop" 
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

   <!-- 加载配置文件 -->
   <context:property-placeholder location="classpath:conf/db.properties" />
   
   <!--druid链接池  -->
   	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${druid.driver}" />
		<property name="url" 			 value="${druid.url}" />
		<property name="username" 	     value="${druid.username}" />
		<property name="password" 		 value="${druid.password}" />
	</bean>
  
	<!-- 配置Mybatis工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	
	<!-- Mapper动态代理开发  扫包  给定包下的接口文件名和映射文件名必须相同  建立接口的实现类-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xiao.mapper" />
	</bean>
	
	<!-- 开启事物 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 事物注解驱动-->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
</beans>
复制代码

在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:p="http://www.springframework.org/schema/p"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
 
 	<!-- 配置扫描注解  @Controller @Service -->
 	<context:component-scan base-package="com.xiao" />
 
 	<!-- SpringMVC使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter -->
 	<mvc:annotation-driven />
 
 	<!-- 配置静态资源映射 -->
	<mvc:resources location="/js/" mapping="/js/**"/>
	<mvc:resources location="/css/" mapping="/css/**"/>
 
 	<!-- 配置视图解析器 -->
 	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 		<!-- 配置逻辑视图的前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 配置逻辑视图的后缀 -->
		<property name="suffix" value=".jsp" />
		
 	</bean>
 	
 	<!-- 定义文件上传解析器 -->
	<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		设定默认编码
		<property name="defaultEncoding" value="UTF-8"></property>
		设定文件上传的最大值5MB,5*1024*1024
		<property name="maxUploadSize" value="5242880"></property>
	</bean> -->
 
  
</beans>
复制代码
相关文章
相关标签/搜索