亲宝软件园·资讯

展开

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

人气:0

1.使用IDEA新建项目

2.选择创建Maven工程

3.填写GroupId和ArtifactId


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


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

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


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



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目录下添加配置文件

文件目录如下


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层以及测试页面,目录结构如下


13.配置tomcat







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

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

数据库数据如下

在jsp页面用EL表达式取值

总结

以上所述是小编给大家介绍的使用IntelliJ IDEA搭建SSM框架的图文教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论