使用IDEA搭建 SSM 框架
SSM 框架整合流程
建项目之前你需要先准备好maven和maven的本地仓库
(1)首先需要新建一个Maven项目,然后导入相关的Maven依赖
<!-- spring 核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<!-- mybatis 核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mybatis 和 spring 的整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- spring mvc 核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<!-- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<!-- spring 的事务管理 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<!-- 支持声明式事务管理 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.9.RELEASE</version>
</dependency>
<!-- 数据连接池技术 其他选项: Alibaba dbcp -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- 数据库连接技术,根据自己的MySQL版本来定义 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- 日志打印 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- 省去实体类中 get set 方法 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
(2)创建项目的大体结构
先创建项目整体结构,之后创建dao层(dao)和dao层里的接口,service层,controller层(控制层),pojo层(实体层).在创建resources包,在其中创建db.properties(数据库连接文件)和log4j.properties(日志文件)
(3) 创建mapper文件,连接到dao接口,在里面可以编写sql语句
(4)创建applicationContext.xml文件来配置mybatis和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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启全局注解 -->
<context:component-scan base-package="cn.kgc"/>
<!-- 引入配置文件 -->
<context:property-placeholder location="classpath*:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${mysql.username}"/>
<property name="driverClass" value="${mysql.driver"/>
<property name="password" value="${mysql.password}"/>
<property name="jdbcUrl" value="${mysql.url"/>
</bean>
<!-- 配置会话工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 设置别名 实体类的路径 -->
<property name="typeAliasesPackage" value="cn.kgc.ssm.pojo"/>
<!-- 映射mapper文件 -->
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
</bean>
<!-- 扫描dao层 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.kgc"/>
</bean>
<!-- 事务控制 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 创建声明式事务 -->
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<!-- 针对不同的方法做不同的事务策略 -->
<tx:method name="*" propagation="REQUIRED" timeout="-1"/>
</tx:attributes>
</tx:advice>
<!-- 将控制融入到代码中 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.kgc.ssm.service.*.*(..))"/>
</aop:config>
</beans>
(5)创建applicationContext-mvc.xml文件来配置spring mvc 文件
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启全局注解扫描 -->
<context:component-scan base-package="cn.kgc"/>
<!-- 视图解析器
WEB-INF 下面一般放着只有登录以后才能访问的页面
-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>-->
<!-- 配置多视图解析器 -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- spring mvc 中的视图解析器会按照order来进行按序解析 , 值越小的越在前面 -->
<property name="order" value="2"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图的类 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 前缀 -->
<property name="prefix" value="/statics/html/"/>
<!-- 后缀 -->
<property name="suffix" value=".html"/>
<!-- 为了放置无法找到页面而写的 html* 表示只要逻辑视图名中携带了html开头的页面,将由此解析器捕获 -->
<property name="viewNames" value="html*"/>
<!-- 解析顺序! -->
<property name="order" value="1"/>
</bean>
</list>
</property>
</bean>
<!-- 设置静态资源的访问 -->
<mvc:resources mapping="/statics/**" location="/statics/"/>
<!-- 和上面的静态资源的访问区分开 -->
<mvc:annotation-driven>
<!-- 消息转换器 -->
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- 设置接口生产的任何数据都设置成UTF-8 一次配置,永久搞定 -->
<value>application/json;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置全局的异常处理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 前面是错误的类型,后面是如果发生了此错误前往什么页面 -->
<prop key="java.lang.RuntimeException">error</prop>
</props>
</property>
</bean>
<!-- 设置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 最大上传大小 -->
<property name="maxUploadSize" value="1024000000"/>
<!-- 设置文件上传编码格式 -->
<property name="defaultEncoding" value="UTF-8"/>
</bean>
</beans>
(6)修改web.xml文件,添加spring mvc 配置
web.xml的文件中如果添加spring mvc 配置,需要修改表头
<?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">
<display-name>Archetype Created Web Application</display-name>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--项目启动首先加载的页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<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*:applicationContext-mvc.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>
<!-- 过滤器:解决乱码问题 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
之后你就可以愉快的编写逻辑代码了!!!