最简单易懂的Idea搭建SSM项目过程和配置(文末有demo哦)
一、新建maven项目
File-> new -> project..
(一) 1、选择maven; 2、选择jdk版本,我的是1.8.0_45; 3、勾选Create from archetype; 4、选择maven-archetype-webapp; 5、Next
(二)1、创建项目名称; 2、项目包名; 3、Next;
(三) 1、maven所在的路径; 2、maven的配置文件位置; 3、maven本地仓库地址; 4、Finish
下面是创建好的maven项目架构:
二、添加Spring、SpringMVC和MyBatis框架支持
(一)、添加pom文件依赖(在这儿添加了spring的依赖项和一些必须的依赖)
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>4.3.7.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</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>6.0.6</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <!-- 3.DAO层框架依赖: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.2.3</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- 4.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.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- 5.Spring依赖 --> <!-- 5.1Spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- 5.2Spring dao依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- 5.3Spring web依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- 5.4Spring test依赖:方便做单元测试和集成测试 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- 6.文件上传 --> <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.2.1</version> </dependency> <!-- 7.fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.35</version> </dependency> </dependencies>
(注意1.0:因为依赖项中已经添加了Spring的依赖,咱们先到项目配置中删掉Spring,为下一步添加框架支持做准备)
File --> Project Structore
(二)、在项目名称上 右键 --> Add Framework Support..
(1)、勾选Spring ; (2)、勾选SpringMVC ;(3)、OK;
(注意1.1 :此时如果没有SpringMVC的可能是插件没导入,去File-setting-plugins中下载需要的插件)
此时项目中多了两个配置文件:applicationContext.xml和dispatcher-servlet.xml:
(三)、完善项目结构:(我习惯把配置文件放在resources下)
这是现在的目录:
创建完成,给java,resources和test创建标记 File-->Project Structure
(四)、配置文件web.xml、 appplicationContext.xml 和 dispatcher-servlet.xml 以及jdbc.properties
(1)、web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <!--welcome pages--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!----> <!--配置springmvc DispatcherServlet--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--配置dispatcher.xml作为mvc的配置文件--> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--把applicationContext.xml加入到配置文件中--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
(2)、appplicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.ssmDemo"/> <!-- mybatis中的 --> <!--加载properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 注入别名配置 --> <property name="typeAliasesPackage" value="com.ssmDemo.pojo"></property> <!-- 映射资 源路径 --> <property name="mapperLocations" value="classpath*:mappers/*.xml"></property> </bean> <!-- 用于扫描mapper接口的配置(mapper解析器) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssmDemo.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- mybatis中的 --> </beans>
(3)、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" 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"> <!--此文件负责整个mvc中的配置--> <!--启用spring的一些annotation(注解) --> <context:annotation-config/> <!-- 配置注解驱动 可以将request参数与绑定到controller参数上 --> <mvc:annotation-driven/> <!--静态资源映射--> <!--本项目把静态资源放在了webapp的statics目录下,资源映射如下--> <mvc:resources mapping="/css/**" location="/static/css/"/> <mvc:resources mapping="/js/**" location="/static/js/"/> <mvc:resources mapping="/image/**" location="/static/image/"/> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一 个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP--> <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/view/"/><!--设置JSP文件的目录位置--> <property name="suffix" value=".jsp"/> <property name="exposeContextBeansAsAttributes" value="true"/> </bean> <!-- 自动扫描装配 --> <context:component-scan base-package="com.ssmDemo.controller"/> </beans>
(4)、jdbc.properties
jdbc.username=root jdbc.password=root jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm_demo?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC jdbc.initPoolSize=5 jdbc.maxPoolSize=10
三、配置tomcat,测试项目启动
(一)、添加tomcat并配置
(二)、选择tomcat; 选择浏览器; 选择jdk; 其他的可以用默认,也可以自己配置; 最后点击ok
(三)、启动tomcat,因为在web.xml中配置了启动页面是index.jsp,本来helloworld稳稳的出现,
但是,tomcat启动报错:
解决:把jdbc.properties配置文件第一行空着(原因可以自己去探究,并不是每次创建ssm项目都会这样)
下面就是正常启动的hello world!
四、测试数据库连接和查询数据
(一)、事实上,经过上述配置,tomcat成功启动,就表示数据库已经连接成功,接下来是要测试数据是否能查询到:
这是数据库的一个基础表:
(二)、写controller、service、dao三层的demo
目前的目录结构:
(三)、demo数据:
ExampleMapper.java:
package com.ssmDemo.dao; import org.springframework.stereotype.Component; @Component("exampleMapper") public interface ExampleMapper { String findName(int id); }
ExampleMapper.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.ssmDemo.dao.ExampleMapper" > <select id="findName" parameterType="int" resultType="string"> select username from user where id = #{id} </select> </mapper>
ExampleService.java:
package com.ssmDemo.service; public interface ExampleService { String findName(int id); }
ExampleServiceImpl.java:
package com.ssmDemo.service.impl; import com.ssmDemo.dao.ExampleMapper; import com.ssmDemo.service.ExampleService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service("exampleService") public class ExampleServiceImpl implements ExampleService { @Resource(name = "exampleMapper") private ExampleMapper exampleMapper; @Override public String findName(int id) { return exampleMapper.findName(id); } }
ExampleController.java:
package com.ssmDemo.controller; import com.ssmDemo.service.ExampleService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @Controller @RequestMapping("/example") public class ExampleContreller { @Resource(name = "exampleService") private ExampleService exampleService; @RequestMapping("/name") @ResponseBody public String demoData() { return exampleService.findName(1); } }
(四)、启动tomcat,输入urlhttp://localhost:8080/ssmDemo_war_exploded/example/name:
这是你要的demo
打完收工!
如果测试过程中有什么问题,欢迎下方评论,作者会及时解答!欢迎来稿···