SSM之一(使用idea創建一個Spring+SpringMVC的項目)

1. 使用idea創建一個基本的maven項目(Web版本)

  1. 用idea創建一個maven項目
  2. 使用模板maven-archetype-webapp
  3. 填寫groupid和artifactId
  4. 確認項目在磁盤上的位置
  5. Finish,idea開始創建一個項目
  6. 將項目部署在tomcat上,在Deployment中選擇war結尾的那個
  7. 啓動tomcat

此時的項目只有一個web項目最基本結構,能夠部署到tomcat上(有web.xml文件)。但是還需要我們繼續完善項目結構。
在這裏插入圖片描述

2. 完善目錄結構

在src目錄下創建java,resources兩個目錄,並將java設置爲Sources Root,resources設置爲Resources Root。

  • java,Sources Root,存放項目源碼的目錄
  • resources(Resources Root),存放資源的目錄

在java(Sources Root)創建包com.ssm。並且在該包下創建五個包

  • controller,存放controller層的代碼
  • service,存放service層的代碼
  • dao,存放持久層代碼
  • model,存放數據模型
  • utils,一些第三方工具

在resources創建目錄

  • mapper,存放mybatis的mapper文件
  • configure,存放配置文件

最後的目錄結構如下:

3. 相關依賴

需要明確一點的是,針對上述構建的項目,雖然是一個web項目,但是歸根結底是一個maven項目,而且是一個使用了模板maven-archetype-webapp的maven項目。對於一個maven項目而言,其核心是pom.xml文件。

POM的的含義是項目對象模型(Project Object Model)。也就是說,maven通過pom.xml定義了項目的基本信息,用於描述如何構建,聲明項目依賴等等。

pom文件主要分爲以下幾塊
在這裏插入圖片描述

  • 項目的基本信息,
  • 配置信息,比如在這裏定義配置信息<spring-version>5.3.7.RELEASE</spring-version>,在下面的配置依賴配置中,直接使用${spring-version}代表5.3.7.RELEASE,有點我們java代碼中的spring-version=5.3.7.RELEASE,然後在接下來的代碼中使用spring-version一樣。
  • 依賴配置,常用,導入jar包
  • 編譯配置,顧名思義,就是設置編譯的配置信息,具體的等以後完善。

默認依賴

<?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.ssm</groupId>
  <artifactId>ssm_04_full_edition_analysis</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>ssm_04_full_edition_analysis 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.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>ssm_04_full_edition_analysis</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>

主要是在dependencies標籤中配置依賴

  <dependencies>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>

    <!--2. 持久層(mybatis)配置-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.40</version>
    </dependency>

    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.3</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    <!--2. 配置完-->

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

4. 配置spring與springmvc

也就是配置項目接口能夠從網絡訪問。

配置web.xml

  <!--1.配置Spring的IOC容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:configure/spring-context.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--1.完-->

  <!--2.配置dispatcherServlet,並匹配所有的URL-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:configure/spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--2.完-->

spring-context.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"
       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">

    <!--告訴Spring在創建容器需要掃描的包爲com.ssm-->
    <context:component-scan base-package="com.ssm"></context:component-scan>
</beans>

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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.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">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.ssm"></context:component-scan>

</beans>

編寫controller層

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    IUserService userServiceImpl = null;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String str = userServiceImpl.login();
        response.getWriter().print(str);
    }
}

編寫service層

@Service
public class UserServiceImpl implements IUserService {

    @Override
    public String login() {
        System.out.println("This is UserServiceImpl");
        return "hello World!";
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章