java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > IEAD+Maven搭建SSM项目

通过IEAD+Maven快速搭建SSM项目的过程(Spring + Spring MVC + Mybatis)

作者:weixin_41583535

这篇文章主要介绍了通过IEAD+Maven快速搭建SSM项目的过程(Spring + Spring MVC + Mybatis),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

项目效果预览:

通过访问控制层的test/dologin方法进入index.jsp,展示jsp中的“hello,ssm”内容

一、新建项目:

1.打开IDEA,新建一个Maven项目

2.点击next,新建一个为ssmDemo的Maven项目(自己定义项目GroupId和ArtifactId)

3.配置本地Maven路径

4.点击Next,确认项目信息,配置项目名称和项目路径,点击Finish按钮即可创建项目

5.项目创建完成,目录结构如下:

注:如项目目录中未包含src,则项目还在初始化,注意网络连接,稍等片刻即可

二、补充项目结构:

1.在main文件夹下面创建java和resources文件夹

2.使java文件夹为项目资源文件夹(Sources root)

3.使resources文件夹为Resources root

4.在src/java目录下面创建com包,在com包创建controller、dao、po、service、service.impl包

三、完善pom.xml(添加项目依赖--完整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.demo</groupId>
 <artifactId>ssmDemo02</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>
 
 <name>ssmDemo02 Maven Webapp</name>
 <!-- FIXME change it to the project's website -->
 <url>http://www.example.com</url>
 
 <properties>
  <!--编译编码-->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <!--maven版本管理-->
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <!-- spring版本管理
  当对spring进行版本控制后,
  maven导入的jar包就不需要关心版本的问题,
  因为会自动匹配符合当前版本控制版本的jar包
   ,导入方式如下:${spring.version}-->
  <spring.version>5.0.2.RELEASE</spring.version>
  <!-- log4j日志文件管理包版本 -->
  <slf4j.version>1.6.6</slf4j.version>
  <log4j.version>1.2.12</log4j.version>
  <shiro.version>1.2.3</shiro.version>
  <!-- mysql连接驱动版本号 -->
  <mysql.version>5.1.6</mysql.version>
  <!-- mybatis版本号 -->
  <mybatis.version>3.4.5</mybatis.version>
  <spring.security.version>5.0.1.RELEASE</spring.security.version>
 </properties>
 
 
 <dependencies>
  <!-- Mybatis 和 mybatis 与 spring 的整合 -->
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>${mybatis.version}</version>
  </dependency>
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>1.3.0</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <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>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.6.8</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <!--junit测试-->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
  <!--mysql-->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>${mysql.version}</version>
  </dependency>
  <!--servlet-->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.0</version>
   <scope>provided</scope>
  </dependency>
  <!--jstl-->
  <dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  <!-- log -->
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>${log4j.version}</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>${slf4j.version}</version>
  </dependency>
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>${slf4j.version}</version>
  </dependency>
  <!--alibaba连接池-->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.9</version>
  </dependency>
 </dependencies>
 
 <build>
  <finalName>ssmDemo02</finalName>
  <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
   <plugins>
    <plugin>
     <artifactId>maven-clean-plugin</artifactId>
     <version>3.1.0</version>
    </plugin>
    <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
    <plugin>
     <artifactId>maven-resources-plugin</artifactId>
     <version>3.0.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.8.0</version>
    </plugin>
    <plugin>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.22.1</version>
    </plugin>
    <plugin>
     <artifactId>maven-war-plugin</artifactId>
     <version>3.2.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-install-plugin</artifactId>
     <version>2.5.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-deploy-plugin</artifactId>
     <version>2.8.2</version>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>
</project>

效果图:

注1:代码复制完成后,选择右下角的Import Changes

注2:如Import Changes后External还未出现jar包,则选中项目,右键,选择maven,Generate Sources...

四、为项目导入jar包

1.选择项目右键 -- open Module Setting

2.选择Artifact

3.选择项目名,选择outPut LayOut,选择Available Elements -- 选择项目名称 -- 右键 -- put into...

效果图(WEB-INF中添加了lib目录):

五、完善target文件夹

1.选择右侧侧栏的maven

2.选择Lifecycle -- 选择clean(双击)

3.选择Lifecycle -- 选择install(双击)

4.效果图

六、测试项目

1.添加tomcat(run --> editConfigurations --> 点击+ --> 选择tomcat-- local)

2.配置Deployment(点击Deployment -- 选择右下角+ -- 选择Artifact --选择Artifa)

3.配置访问路径:/ssmDemotest

4.启动tomcat,访问:http://localhost:8080/ssmDemotest

七、编写ssm框架所需xml

1.在src/resources文件夹下面添加mybatis包、spring包

2.在src/resources文件夹下面添加log4j.properties、db.properties

3.在mybatis包下添加:mybatis-config.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.po" />
  </typeAliases>
</configuration>

4.在spring包下添加:spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 
  <!-- 1.配置component-scan 扫描service,mapper包 -->
  <!-- 如不写,则在控制层的@Resource注解则指定不到对应的service类 -->
  <context:component-scan base-package="com.service" />
  <context:component-scan base-package="com.dao" />
 
  <!-- 2.配置 读取properties文件 jdbc.properties -->
  <context:property-placeholder location="classpath:db.properties" />
 
  <!-- 3.配置 数据源 -->
  <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>
 
  <!-- 4.配置SqlSessionFactory对象 -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 4.1.注入数据库连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 4.2.配置MyBaties全局配置文件:mybatis-config.xml -->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    <!-- 4.3.扫描entity包 使用别名 -->
    <property name="typeAliasesPackage" value="com.po" />
    <!-- 4.4.扫描sql配置文件:mapper需要的xml文件
    <property name="mapperLocations" value="classpath:com/dao/*.xml" /> -->
  </bean>
 
  <!-- 5.配置Mapper扫描 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 5.1.注入sqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <!-- 5.2.配置Mapper扫描 -->
    <property name="basePackage" value="com.dao" />
  </bean>
</beans>

5.在spring包下添加:spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  <!-- 配置Service扫描 -->
  <context:component-scan base-package="com.service" />
</beans>

6.在spring包下添加:spring-mvc.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:p="http://www.springframework.org/schema/p"
    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.0.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
 
  <!-- 1.配置Controller扫描 -->
  <context:component-scan base-package="com.controller" />
 
  <!-- 2.配置注解驱动 -->
  <mvc:annotation-driven />
 
  <!-- 3.配置视图解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 4.1.前缀 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 4.2.后缀 -->
    <property name="suffix" value=".jsp" />
  </bean>
 
</beans>

7. 完善web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
     version="2.5">
 
 <display-name>Archetype Created Web Application</display-name>
 
 <!-- 1.加载spring容器 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/spring-dao.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <!-- 2.springmvc前端控制器 -->
 <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:spring/spring-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>
 
 <!-- 3.设置匹配所有的url -->
 <servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.js</url-pattern>
 </servlet-mapping>
 
</web-app>

八、启动tomcat,测试xml文件是否正确

1.如启动日志没有错误信息,则xml添加无误

2.如网址:http://localhost:8080/ssmDemotest 正常展示,则xml添加无误

注:由于大部分代码均为复制,可以重启IDEA

九、controller层

1.在controller包新建类:TestController

2.在类顶部(导包语句下面)添加注解:@Controller,@RequestMapping(value = "test")

3.编写方法:

@RequestMapping("dologin")
public String dologin() {
  System.out.println("====test====");
  return "index";
}

十、JSP

1.在web-inf下创建目录:jsp

2.在jsp文件下创建:index.jsp

3.在index.jsp下添加:hello,ssm

十一、测试

http://localhost:8080/ssmDemotest/test/dologin

到此这篇关于通过IEAD+Maven快速搭建SSM项目的过程(Spring + Spring MVC + Mybatis)的文章就介绍到这了,更多相关IEAD+Maven搭建SSM项目内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文