目录
其他网址
参考链接:
搭建springboot的ssm的maven项目
IDEA下用SpringBoot搭建SSM框架项目,支持JSP页面(图文步骤)
初建工程
1. File=> New=> Project
2. 项目类型、包名称等
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
4.Idea项目名和位置
5.创建后的结构
创建相关包(非必须)
创建以下包:
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
详见:maven使用_maven_feiying0canglang的博客-CSDN博客
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>
最终结构
配置文件
路径:src\main\resources
文件:application.properties或application.yml
application.properties
配置大全
中文:application.properties详解 --springBoot配置文件
英文:application.properties 常见配置
配置示例
# 服务器配置
server.port=8080 #端口号设置
# 数据库配置
spring.datasource.url = jdbc:mysql://localhost:3306/DatebaseName(自己数据库名)
spring.datasource.username = root
spring.datasource.password = 123
spring.datasource.driverClassName = com.mysql.jdbc.Driver
application.yml
其他网址:
配置参数application.yml
YAML(application.yml)基础语法
介绍
YAML 是一种简洁的非标记语言,文件名后缀为yml,java中经常用它描述配置文件。
YAML以数据为中心,比json/xml等更适合做配置文件。
使用空白,缩进,分行组织数据,从而使得表示更加简洁易读。
基本规则
YAML有以下基本规则:
1、大小写敏感
2、使用缩进表示层级关系
3、禁止使用tab缩进,只能使用空格键
4、缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。
5、使用#表示注释
6、字符串可以不用引号标注
基本语法
k:(空格)v:表示一对键值对(空格必须有),以空格的缩进来控制层级关系
值的写法
a) 字面量:(空格)普通的值(数字,字符串,布尔)
i. 字符串默认不用加上单引号或者双引号
ii. “”:双引号;不会转义字符串里面的特殊字符,特殊字符会作为本身想表示的意思,例如:
name: “zhangsan \n lisi” 输出 zhangsan 换行 lisi
iii. ‘’:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’ 输出 zhangsan \n lisi
b) 对象/Map(属性和值)(键值对)(k: v)语法示例如下:
friends:
lastName: zhangsan
age: 20
另一种写法:friends: {lastName: zhangsan,age: 18}
c) 数组(List,Set)(用-值表示数组中的一个元素)语法示例如下:
pets:
- cat
- dog
- pig
另一种写法:pets: [cat,dog,pig]
分段
可以使用---来分段,例如:
spring:
profiles:
#激活开发环境
active: dev
---
#开发环境配置
spring:
profiles: dev
server:
port: 8080
---
#生产环境配置
spring:
profiles: pro
server:
port: 8082
配置示例
# 服务器配置
server:
port: 8082
# spring配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/DatebaseName
username: root
password: 123
driverClassName: com.mysql.jdbc.Driver