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!";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值