首先创建一个Maven项目:
File->New->Project->Maven->Maven Project
首先我们对pom.xml进行配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zzf.SSM</groupId>
<artifactId>SSMProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SSMProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--上面部分是自动生成的无须改动-->
<properties>
<commons-lang-version>2.6</commons-lang-version>
<commons-io-version>2.5</commons-io-version>
</properties>
<!-- 阿里云maven仓库 -->
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<!-- 只在编译时和测试时运行 -->
<scope>provided</scope>
</dependency>
<!-- 补全项目依赖 -->
<!-- 1.日志 java 日志
使用slf4j+logback
-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 实现slf4j借口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 2.数据库相关依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- DAO 框架:MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- mybatis实现的spring整合yl -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 3.Servlet web相关依赖 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--4. spring依赖 -->
<!--1)spring 核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 2)springDAO层依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 3)spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 4)spring text依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- 新增 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io-version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang-version}</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>SSMProject</finalName>
</build>
</project>
保存后右击项目选择Maven->updata Maven……成功后可以看到jar成功导入进去
SSM框架主要分三层Dao层Service层和Controller层,所以我先在创建文件夹src/main/java(放源码)
然后创建三个包(dao,service,controller)
在src/main/resources文件夹下创建mybatis-config.xml和jdbc.properties(全局mybatis配置和jdbc连接配置:ps:username不要用)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8
user=用户名
password=密码
<?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>
<!-- 使用jdbc的getGenerated -->
<setting name="useGeneratedKeys" value="ture" />
<!--使用列别名替换列名 默认值为true
select name as title(实体中的属性名是title) form table;
开启后mybatis会自动帮我们把表中name的值赋到对应实体的title属性中
-->
<setting name="useColumnLabel" value="true"/>
<!--开启驼峰命名转换Table:create_time到 Entity(createTime)-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
创建文件夹
src/main/resources/mapper实现org.seckill.dao接口的映射xml配置(实现接口)
创建文件夹src/main/resources/spring实现spring-dao.xml与数据库的连接与mybatis的整合
<?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.xsd">
<!-- <tx:annotation-driven/> 就是支持事务注解的(@Transactional) -->
<!--配置整合mybatis过程
1.配置数据库相关参数 properties的属性 :${url}-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--配置连接池属性-->
<property name="driverClass" value="${driver}" />
<!-- 基本属性 url、user、password -->
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<!--c3p0私有属性-->
<property name="maxPoolSize" value="30"/><!-- 最多保存30个连接对象 -->
<property name="minPoolSize" value="10"/>
<!--关闭连接后不自动commit-->
<property name="autoCommitOnClose" value="false"/>
<!--获取连接超时时间-->
<property name="checkoutTimeout" value="1000"/>
<!--当获取连接失败重试次数-->
<property name="acquireRetryAttempts" value="20"/>
</bean>
<!--约定大于配置-->
<!--3.配置SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--往下才是mybatis和spring真正整合的配置-->
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置mybatis全局配置文件:mybatis-config.xml-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--扫描entity包,使用别名,多个用;隔开这里我们没有用实体类-->
<!--<property name="typeAliasesPackage" value="org.seckill.entity"/>-->
<!--扫描sql配置文件:mapper需要的xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!--4:配置扫描Dao接口包,动态实现DAO接口,注入到spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入SqlSessionFactory,当启动时有可能连接池未加载导致加载错误所以采用后加载的方式-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描的Dao接口-->
<property name="basePackage" value="org.ssm.dao"/>
</bean>
</beans>
Service层
src/main/java->org.seckill新建一个org.seckill.service包用来存储service接口(业务接口)
/*
*业务接口:站在使用者(程序员)的角度设计接口
* 三个方面:1.方法定义粒度,方法定义的要非常清楚2.参数,要越简练越好
* 3.返回类型(return 类型一定要友好/或者return异常,我们允许的异常)
*/
在org.seckill.service包下新建impl包(org.seckill.service.impl)用来存放业务接口实现类
配置spring-service.xml来进行对事物的控制
<!--扫描service包下所有使用注解的类型 包括子包-->
<!--需要对其该service包下的接口实现类添加 @service注解 如果有Dao对象则加 @Autowired -->
如@Service
<?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"
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">
<!--扫描service包下所有使用注解的类型 包括子包-->
<!--需要对其该service包下的接口实现类添加 @service注解 如果有Dao对象则加 @Autowired -->
<context:component-scan base-package="org.ssm.service" />
<!--配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据库连接池 ref:引用的是spring-dao.xml-->
<property name="dataSource" ref="dataSource" />
</bean>
<!--配置基于注解的声明式事务 默认使用注解来管理事务行为 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
修改/src/main/webapp/WEB-INF/web.xml文件如:
<web-app 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"
version="3.0"
metadata-complete="true">
<!-- 使用该方法配置时需要加上下面的监听器和dispatcher-service.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring-*.xml
</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param> -->
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 过滤中文乱码 mvc:过滤器-->
<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>
同时需要在src/main/resources/spring实现spring-web.xml对spring跟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/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">
<!--配置spring mvc 只需要1.2.3三步够了-->
<!--1,开启springmvc注解模式(简化配置)
a.自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
b.默认提供一系列的功能:数据绑定,数字和日期的format@NumberFormat,@DateTimeFormat
c:xml,json的默认读写支持-->
<mvc:annotation-driven/>
<!-- servlet-mapping 映射路径:“/” -->
<!--2.静态资源默认servlet配置-->
<!--
1).加入对静态资源处理:js,gif,png
2).允许使用 "/" 做整体映射
-->
<mvc:default-servlet-handler/>
<!--3:配置JSP 显示ViewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<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="maxUploadSize" value="209715200"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<property name="resolveLazily" value="true"></property>
</bean>
<!--4:扫描web相关的controller 放到IOC容器当中-->
<context:component-scan base-package="org.ssm.controller"/>
<!-- 注册拦截器 -->
<!--
<mvc:interceptors>
<mvc:interceptor>
拦截所有以admin为路径的请求
<mvc:mapping path="/admin/*"/>
<bean class="org.seckill.common.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
-->
</beans>
添加Dispatcher-Servlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
</beans>
到此我们的配置已经完成了,如果还需要需要什么配置只需要在对应的xml文件中添加。
下面我简单的用一个接口返回一个json数据验证这个框架的搭建是否成功
dao层
package org.ssm.dao;
import java.util.List;
import java.util.Map;
public interface TextDao {
public List<Map<String,Object>> all(Map<String,Object> map);
public Map<String,Object> one(Map<String,Object> map);
}
mapper映射文件
<?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="org.ssm.dao.TextDao"><!--对应的dao-->
<select id="all" resultType="Map" parameterType="Map">
SELECT * FROM seckill;
</select>
<select id="one" resultType="Map" parameterType="Map">
SELECT * FROM seckill where seckill_id=#{id};
</select>
</mapper>
Service层及其实现
package org.ssm.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ssm.dao.TextDao;
import org.ssm.service.TextService;
@Service//加上Service注解
public class TextServiceImpl implements TextService {
@Autowired//自动注入
TextDao text;
public List<Map<String, Object>> all(Map<String, Object> map) {
return text.all(map);
}
@Override
public Map<String, Object> one(Map<String, Object> map) {
return text.one(map);
}
}
Controller层
package org.ssm.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ssm.service.TextService;
@Controller
public class TextController {
@Autowired
TextService text;
//限制为get请求
@RequestMapping(value = "/all")
@ResponseBody//返回为json
public Map<String,Object> list()
{
Map<String,Object> map=new HashMap<String,Object>();
Map<String,Object> res=new HashMap<String,Object>();
List<Map<String,Object>>list= new ArrayList<Map<String,Object>>();
list=text.all(map);
res.put("data", list);
return res;
}
@RequestMapping(value = "/one")
@ResponseBody
public Map<String,Object> one(String id)
{
Map<String,Object> map=new HashMap<String,Object>();
Map<String,Object> res=new HashMap<String,Object>();
map.put("id", id);
res.put("data",text.one(map));
return res;
}
}
可以看到ssm框架是正确搭建成功的~~~~~
*如果有大佬发现错误请在下方评论留言撒……