欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用IntelliJ IDEA搭建SSM框架的图文教程

程序员文章站 2023-12-14 23:31:04
1.使用idea新建项目 2.选择创建maven工程 3.填写groupid和artifactid 4.填写项目名称,与上一步的artifacti...

1.使用idea新建项目

使用IntelliJ IDEA搭建SSM框架的图文教程

2.选择创建maven工程

使用IntelliJ IDEA搭建SSM框架的图文教程

3.填写groupid和artifactid

使用IntelliJ IDEA搭建SSM框架的图文教程

4.填写项目名称,与上一步的artifactid一致即可,然后点finish

使用IntelliJ IDEA搭建SSM框架的图文教程

5.刚建好的目录只是一个maven的目录结构,如下

使用IntelliJ IDEA搭建SSM框架的图文教程

6.完善目录结构,添加webapp、web-inf目录,以及web.xml文件

使用IntelliJ IDEA搭建SSM框架的图文教程

7.修改idea的maven设置,maven默认的本地仓库会保存在c盘,为了方便以后使用,尽量修改本地仓库的位置,因为这是框架整合,所以具体的修改方式在这里不多做说明。快捷键ctrl+alt+s,在搜索框中搜索maven,如下


使用IntelliJ IDEA搭建SSM框架的图文教程

9.修改maven的核心配置文件pom.xml,添加框架所需要的依赖

<?xml version="1.0" encoding="utf-8"?> 
<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/xsd/maven-4.0.0.xsd"> 
 <modelversion>4.0.0</modelversion> 
 <groupid>com.xiao</groupid> 
 <artifactid>ssm</artifactid> 
 <version>1.0-snapshot</version> 
 <packaging>war</packaging> 
 <!-- 提取版本属性 --> 
 <properties> 
  <spring.version>4.2.4.release</spring.version> 
  <mybatis.version>3.2.8</mybatis.version> 
  <mybatis.spring.version>1.2.2</mybatis.spring.version> 
  <slf4j.version>1.6.4</slf4j.version> 
  <log4j.version>1.2.17</log4j.version> 
  <jackson.version>2.4.2</jackson.version> 
  <druid.version>1.0.9</druid.version> 
  <mysql.version>5.1.32</mysql.version> 
  <jstl.version>1.2</jstl.version> 
  <servlet-api.version>2.5</servlet-api.version> 
  <jsp-api.version>2.0</jsp-api.version> 
  <commons-io.version>1.3.2</commons-io.version> 
  <commons-fileupload.version>1.3.1</commons-fileupload.version> 
 </properties> 
 <!-- 版本锁定 --> 
 <dependencymanagement> 
  <dependencies> 
   <!-- spring --> 
   <dependency> 
    <groupid>org.springframework</groupid> 
    <artifactid>spring-context</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-webmvc</artifactid> 
    <version>${spring.version}</version> 
   </dependency> 
   <dependency> 
    <groupid>org.springframework</groupid> 
    <artifactid>spring-jdbc</artifactid> 
    <version>${spring.version}</version> 
   </dependency> 
   <dependency> 
    <groupid>org.springframework</groupid> 
    <artifactid>spring-aspects</artifactid> 
    <version>${spring.version}</version> 
   </dependency> 
   <dependency> 
    <groupid>org.springframework</groupid> 
    <artifactid>spring-jms</artifactid> 
    <version>${spring.version}</version> 
   </dependency> 
   <dependency> 
    <groupid>org.springframework</groupid> 
    <artifactid>spring-context-support</artifactid> 
    <version>${spring.version}</version> 
   </dependency> 
   <!-- mybatis --> 
   <dependency> 
    <groupid>org.mybatis</groupid> 
    <artifactid>mybatis</artifactid> 
    <version>${mybatis.version}</version> 
   </dependency> 
   <dependency> 
    <groupid>org.mybatis</groupid> 
    <artifactid>mybatis-spring</artifactid> 
    <version>${mybatis.spring.version}</version> 
   </dependency> 
   <!-- 日志处理 --> 
   <dependency> 
    <groupid>org.slf4j</groupid> 
    <artifactid>slf4j-log4j12</artifactid> 
    <version>${slf4j.version}</version> 
   </dependency> 
   <dependency> 
    <groupid>log4j</groupid> 
    <artifactid>log4j</artifactid> 
    <version>${log4j.version}</version> 
   </dependency> 
   <!-- jackson json处理工具包 --> 
   <dependency> 
    <groupid>com.fasterxml.jackson.core</groupid> 
    <artifactid>jackson-databind</artifactid> 
    <version>${jackson.version}</version> 
   </dependency> 
   <!-- 连接池 --> 
   <dependency> 
    <groupid>com.alibaba</groupid> 
    <artifactid>druid</artifactid> 
    <version>${druid.version}</version> 
   </dependency> 
   <!-- mysql --> 
   <dependency> 
    <groupid>mysql</groupid> 
    <artifactid>mysql-connector-java</artifactid> 
    <version>${mysql.version}</version> 
   </dependency> 
   <!-- io包 --> 
   <dependency> 
    <groupid>org.apache.commons</groupid> 
    <artifactid>commons-io</artifactid> 
    <version>${commons-io.version}</version> 
   </dependency> 
   <!-- 文件上传组件 --> 
   <dependency> 
    <groupid>commons-fileupload</groupid> 
    <artifactid>commons-fileupload</artifactid> 
    <version>${commons-fileupload.version}</version> 
   </dependency> 
   <!-- jsp相关 --> 
   <dependency> 
    <groupid>jstl</groupid> 
    <artifactid>jstl</artifactid> 
    <version>${jstl.version}</version> 
   </dependency> 
   <dependency> 
    <groupid>javax.servlet</groupid> 
    <artifactid>servlet-api</artifactid> 
    <version>${servlet-api.version}</version> 
    <scope>provided</scope> 
   </dependency> 
   <dependency> 
    <groupid>javax.servlet</groupid> 
    <artifactid>jsp-api</artifactid> 
    <version>${jsp-api.version}</version> 
    <scope>provided</scope> 
   </dependency> 
  </dependencies> 
 </dependencymanagement> 
 <!-- 添加依赖 --> 
 <dependencies> 
  <!-- spring --> 
  <dependency> 
   <groupid>org.springframework</groupid> 
   <artifactid>spring-context</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework</groupid> 
   <artifactid>spring-beans</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework</groupid> 
   <artifactid>spring-webmvc</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework</groupid> 
   <artifactid>spring-jdbc</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework</groupid> 
   <artifactid>spring-aspects</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework</groupid> 
   <artifactid>spring-jms</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework</groupid> 
   <artifactid>spring-context-support</artifactid> 
  </dependency> 
  <!-- mybatis --> 
  <dependency> 
   <groupid>org.mybatis</groupid> 
   <artifactid>mybatis</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>org.mybatis</groupid> 
   <artifactid>mybatis-spring</artifactid> 
  </dependency> 
  <!-- 日志处理 --> 
  <dependency> 
   <groupid>org.slf4j</groupid> 
   <artifactid>slf4j-log4j12</artifactid> 
  </dependency> 
  <!-- jackson json处理工具包 --> 
  <dependency> 
   <groupid>com.fasterxml.jackson.core</groupid> 
   <artifactid>jackson-databind</artifactid> 
  </dependency> 
  <!-- 连接池 --> 
  <dependency> 
   <groupid>com.alibaba</groupid> 
   <artifactid>druid</artifactid> 
  </dependency> 
  <!-- mysql --> 
  <dependency> 
   <groupid>mysql</groupid> 
   <artifactid>mysql-connector-java</artifactid> 
  </dependency> 
  <!-- io包 --> 
  <dependency> 
   <groupid>org.apache.commons</groupid> 
   <artifactid>commons-io</artifactid> 
  </dependency> 
  <!-- 文件上传组件 --> 
  <dependency> 
   <groupid>commons-fileupload</groupid> 
   <artifactid>commons-fileupload</artifactid> 
  </dependency> 
  <!-- jsp相关 --> 
  <dependency> 
   <groupid>jstl</groupid> 
   <artifactid>jstl</artifactid> 
  </dependency> 
  <dependency> 
   <groupid>javax.servlet</groupid> 
   <artifactid>servlet-api</artifactid> 
   <scope>provided</scope> 
  </dependency> 
  <dependency> 
   <groupid>javax.servlet</groupid> 
   <artifactid>jsp-api</artifactid> 
   <scope>provided</scope> 
  </dependency> 
 </dependencies> 
 <build> 
  <!-- java编译插件 --> 
  <plugins> 
   <plugin> 
    <groupid>org.apache.maven.plugins</groupid> 
    <artifactid>maven-compiler-plugin</artifactid> 
    <version>3.2</version> 
    <configuration> 
     <source>1.8</source> 
     <target>1.8</target> 
     <encoding>utf-8</encoding> 
    </configuration> 
   </plugin> 
  </plugins> 
  <!--配置资源文件映射 --> 
  <resources> 
   <resource> 
    <directory>src/main/java</directory> 
    <includes> 
     <include>**/*.xml</include> 
    </includes> 
    <filtering>false</filtering> 
   </resource> 
   <resource> 
    <directory>src/main/resources</directory> 
    <includes> 
     <include>**/*.properties</include> 
     <include>**/*.xml</include> 
    </includes> 
    <filtering>false</filtering> 
   </resource> 
  </resources> 
 </build> 
</project> 

10.在resources目录下添加配置文件

文件目录如下

使用IntelliJ IDEA搭建SSM框架的图文教程

a)连接数据库配置信息文件-db.properties

druid.driver=com.mysql.jdbc.driver 
druid.url=jdbc:mysql://localhost:3306/all_db?characterencoding=utf-8 
druid.username=root 
druid.password=root 

 b)mybatis核心配置文件-sqlmapconfig.xml

<?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> 
 <!-- 给实体类起别名 --> 
 <typealiases > 
  <package name="com.xiao.pojo"/> 
 </typealiases> 
 </configuration> 

c)spring核心配置文件,整合mybatis,applicationcontext.xml

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xmlns="http://www.springframework.org/schema/beans" 
  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-4.2.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 
 <!-- 加载数据库连接信息配置文件 --> 
 <context:property-placeholder location="classpath: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: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 /> 
</beans> 

 d)springmvc配置文件-springmvc.xml

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xmlns="http://www.springframework.org/schema/beans" 
  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.2.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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/**"/> 
 <mvc:resources location="/imgs/" mapping="/imgs/**"/> 
 <mvc:resources location="/font/" mapping="/font/**"/> 
 --> 
 <!-- 配置视图解析器--> 
 <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> 
  <!-- 配置逻辑视图的前缀 --> 
  <property name="prefix" value="/web-inf/jsp/" /> 
  <!-- 配置逻辑视图的后缀 --> 
  <property name="suffix" value=".jsp" /> 
 </bean> 
</beans> 

11.配置web.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<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_2_5.xsd" 
   version="2.5"> 
 <display-name>ssm</display-name> 
 <welcome-file-list> 
  <welcome-file>index.jsp</welcome-file> 
 </welcome-file-list> 
 <!-- spring监听器 --> 
 <listener> 
  <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> 
 </listener> 
 <!-- 指定spring核心配置文件 --> 
 <context-param> 
  <param-name>contextconfiglocation</param-name> 
  <param-value>classpath: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: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> 

12.完善目录结构,书写实体类、web层、service层、dao层以及测试页面,目录结构如下

使用IntelliJ IDEA搭建SSM框架的图文教程

13.配置tomcat

使用IntelliJ IDEA搭建SSM框架的图文教程

使用IntelliJ IDEA搭建SSM框架的图文教程

使用IntelliJ IDEA搭建SSM框架的图文教程

使用IntelliJ IDEA搭建SSM框架的图文教程

使用IntelliJ IDEA搭建SSM框架的图文教程

使用IntelliJ IDEA搭建SSM框架的图文教程

14.启动tomcat,访问地址: http://localhost:8080/getall   进行测试,结果如下

使用IntelliJ IDEA搭建SSM框架的图文教程

出现以上结果,表示ssm框架搭建成功!

数据库数据如下

使用IntelliJ IDEA搭建SSM框架的图文教程

在jsp页面用el表达式取值

总结

以上所述是小编给大家介绍的使用intellij idea搭建ssm框架的图文教程,希望对大家有所帮助

上一篇:

下一篇: