0
点赞
收藏
分享

微信扫一扫

Idea项目--以SpringBoot创建ssm工程

求索大伟 2022-03-22 阅读 83


其他网址

参考链接:

​​搭建springboot的ssm的maven项目​​

​​IDEA下用SpringBoot搭建SSM框架项目,支持JSP页面(图文步骤)​​

初建工程

1. File=> New=> Project

Idea项目--以SpringBoot创建ssm工程_spring

2. 项目类型、包名称等

Idea项目--以SpringBoot创建ssm工程_spring_02

Group:pom.xml的<groupId> xxx </groupId>标签

Artifact:pom.xml的<artifactId> xxx </artifactId>标签

Type:maven。(选择grable差别也不大)

name:

    1. pom.xml的<name> xxx </name>标签

    2. 作为spring的启动类的前缀。本处对应:src\main\java\SsmShopApplication.java

    3. maven的名字

Package:会作为工程的src\main\java\下的源码路径

3.选择依赖

本处Spring Web一般是必选的,因为用它生成启动等。其他如果本处不选,则需要手动修改pom.xml文件。

版本问题:对于比较老的Springboot版本,Web:选择Web,SQL:选Mybatis和MySQL

Idea项目--以SpringBoot创建ssm工程_spring_03

4.Idea项目名和位置

Idea项目--以SpringBoot创建ssm工程_xml_04

5.创建后的结构

Idea项目--以SpringBoot创建ssm工程_java_05

创建相关包(非必须)

创建以下包:

controller

        类。常用注释:@Controller、@RestController

dao

        接口。常用注释:@Mapper   (mybatis的注释)。

        对应mapper包。

mapper

        xml文件。常用标签:<mapper namespace="​xxx​"> </mapper>

        对应dao和pojo包。

        1.​xxx​必须对应dao包下的名字空间。例如:com\example\sale\dao下有ProductDao.java文件,文件内容为:           

            @Mapper

            public interface ProductDao {      .......     }

            则xxx必须为:com.example.sale.dao.ProductDao

        2.application.yml配置

            mybatis:

                  mapper-locations: classpath:com/example/sale/mapper/*.xml

                  type-aliases-package: com.example.sale.pojo

pojo

        类。常用注解:@Alias("xxx")。需实现Serializable

        对应mapper包。

service

        接口,类。常用注解:@Service。类需实现本包的接口

task​(不必需)

        接口,类。常用注解:@Service。类需实现本包的接口

配置web(非必须)

    对于web项目,一般会有一个路径用于前端的显示和配置,例如:jsp,web.xml等。

    这个路径一般是:src\main\webapp\WEB-INF

    步骤如下:

(1)src\main\下创建webapp目录、

(2)添加web模块

    File=> Project Structure=> Project Settings=> Modules=> 点开项目名=> Web=> 

        Deployment Descriptors:点右侧加号把路径改为:项目路径\src\main\webapp\WEB-INF\web.xml

                                                                                   (默认路径是项目路径\WEB-INF\web.xml)

        Web Resource  Directories:点右侧加号把路径改为:

                Web Resouce Directory:项目路径\src\main\webapp\

                Path Relative to Deployment Root:/

    =>Apply => OK

创建之后会自动生成文件:项目路径\src\main\webapp\WEB-INF\web.xml

配置web.xml

如果用到了web.xml,一般要修改此处:在项目中的web.xml文件的</web-app>标签内添加此标签:<absolute-ordering />

否则会报错:​“More than one fragment with the name [spring_web] was found”

例:

<web-app>

    ...

    <servlet-mapping>

        <servlet-name>chapter2</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

   ​ <absolute-ordering />

</web-app>

配置pom.xml

mybatis相关

Idea默认不会编译src\main\java下的xml文件

解决方法:在pom.xml中配置xml资源路径

<project xxxxxx>
...
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
...
</build>
</project>

详细示例

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sale</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sale</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<!-- 本处不使用lettuce,使用jedis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<!--不使用Redis的异步客户端lettuce -->
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

最终结构

Idea项目--以SpringBoot创建ssm工程_xml_06

配置文件

路径:src\main\resources

常用文件:application.properties或application.yml


举报

相关推荐

0 条评论