SpringBoot项目创建流程--SpringMVC
Posted 希小木
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot项目创建流程--SpringMVC相关的知识,希望对你有一定的参考价值。
SpringBoot项目创建步骤
1. 创建SpringBoot工程
(1) File → New → Project → Spring Initilizr
(2) Name:MySpringBoot
(3) Group: com.baidu
(4) java: 8
(5) Next
(6) Finish
(7) This Window
(8) 配置maven环境:
① File → Settings → Build,Execution,Deployment → Build Tools → Maven
② Maven Home Path:Maven文件路径
③ User setting files: Maven文件路径 → conf → logging → setting文件
④ Local repository:自建路径(D:\\Java\\repository)
⑤ OK
2. 创建Maven工程
(1) MySpringBoot下右键 → New → moudle → Maven
(2) Next
(3) Name:springboot_demo4
(4) Groupid :com.baidu
(5) Finish
3. 编辑Spring MVC
目录结构如下:
操作步骤:
(1) 编辑pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.jt</groupId> 8 <artifactId>springboot_demo3</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <!--1.parent标签--> 12 <!--集中定义版本号--> 13 <parent> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-starter-parent</artifactId> 16 <version>2.5.1</version> 17 <relativePath/> <!-- lookup parent from repository --> 18 </parent> 19 20 21 <!--2.添加依赖项--> 22 <!--依赖: 项目中需要添加什么功能,则依赖什么jar包.--> 23 <dependencies> 24 <!--引入SpringMVC功能--> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 <!--SpringBoot的测试功能--> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-test</artifactId> 33 <scope>test</scope> 34 </dependency> 35 36 <!--SpringBoot核心机制: "开箱即用" 37 只需导入特定的jar包文件 则可以直接使用其中的功能 38 根本原因: SpringBoot是对框架的简化,内部进行了扩展,无需程序员操作. 39 --> 40 <!--支持热部署 --> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-devtools</artifactId> 44 </dependency> 45 46 <!--引入插件lombok 自动的set/get/构造方法插件 --> 47 <dependency> 48 <groupId>org.projectlombok</groupId> 49 <artifactId>lombok</artifactId> 50 </dependency> 51 52 <!--引入数据库驱动 --> 53 <dependency> 54 <groupId>mysql</groupId> 55 <artifactId>mysql-connector-java</artifactId> 56 <scope>runtime</scope> 57 </dependency> 58 59 <!--springBoot数据库连接 --> 60 <dependency> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-starter-jdbc</artifactId> 63 </dependency> 64 65 <!--spring整合mybatis-plus MP中包含了mybatis的包所以将原来的包删除 --> 66 <dependency> 67 <groupId>com.baomidou</groupId> 68 <artifactId>mybatis-plus-boot-starter</artifactId> 69 <version>3.4.3</version> 70 </dependency> 71 72 </dependencies> 73 74 75 <!--3.添加build标签--> 76 <!--该插件是SpringBoot项目打包时必须添加的. 77 如果没有该插件 则导致jar包不能运行 78 --> 79 <build> 80 <plugins> 81 <plugin> 82 <groupId>org.springframework.boot</groupId> 83 <artifactId>spring-boot-maven-plugin</artifactId> 84 <version>2.5.1</version> 85 </plugin> 86 </plugins> 87 </build> 88 </project>
(2) 编辑主启动类
① @SpringBootApplication
② @MapperScan(“com.jt.mapper”) --> 包扫描
1 package com.jt; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 @MapperScan("com.jt.mapper") 9 public class RunApp { 10 public static void main(String[] args) { 11 SpringApplication.run(RunApp.class, args); 12 } 13 }
(3) 编辑pojo包下的User类
① @Data --> 自动生成setter/getter/to string/构造方法等
② @Accessors(chain = true) --> 启动链式加载(重写set方法)
③ @TableName(“demo_user”) --> 使用MP, 类名与表名映射
1 package com.jt.pojo; 2 3 import com.baomidou.mybatisplus.annotation.TableName; 4 import lombok.Data; 5 import lombok.experimental.Accessors; 6 7 import java.io.Serializable; 8 9 @Data 10 @Accessors(chain = true) 11 @TableName("demo_user") 12 //规定:POJO实体一般都需要实现序列号接口 13 public class User implements Serializable { 14 private Integer id; 15 private String name; 16 private Integer age; 17 private String sex; 18 }
(4) 编辑mapper层下的UserMapper接口
1 package com.jt.mapper; 2 3 import com.baomidou.mybatisplus.core.mapper.BaseMapper; 4 import com.jt.pojo.User; 5 import org.apache.ibatis.annotations.Mapper; 6 7 //@Mapper 每个接口都要加一次,麻烦,所以提取到主启动类RunApp上加@MapperScan("com.jt.Mapper")注解 8 public interface UserMapper extends BaseMapper<User> { 9 10 }
(5) 编辑resources--mappers包下的UserMapper.xml映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!--语法:namespace="接口的路径" --> 6 <mapper namespace="com.jt.mapper.UserMapper"> 7 8 <!-- <select id="findAll" resultType="User"> 9 select * from demo_user 10 </select> 11 --> 12 </mapper>
(6) 编辑resources包下的application.yml核心配置文件
server: port: 8090 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true username: root #如果密码以数字0开头,则使用""号包裹 "0123456" password: root #SpringBoot整合MybatisPlus mybatis-plus: #设置别名包 type-aliases-package: com.jt.pojo #加载映射文件 mapper-locations: classpath:/mappers/*.xml #开启驼峰映射 configuration: map-underscore-to-camel-case: true #打印Sql语句 logging: level: #指定包路径,日志输出 com.jt.mapper: debug
(7) 编辑service层下的UserService接口
1 package com.jt.service; 2 3 import com.jt.pojo.User; 4 5 import java.util.List; 6 7 public interface UserService { 8 List<User> findAll(); 9 }
(8) 编辑Service层下的UserServiceImpl实现类
① @Service
② @AutoWired --> 依赖注入
③ @Override --> 重写接口方法
1 package com.jt.service; 2 3 import com.jt.mapper.UserMapper; 4 import com.jt.pojo.User; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import java.util.List; 9 10 @Service 11 public class UserServiceImpl implements UserService { 12 @Autowired 13 public UserMapper userMapper; 14 15 @Override 16 public List<User> findAll() { 17 return userMapper.selectList(null); 18 } 19 }
(9) 编辑controller层下的UserController类
① @RestController
② @CrossOrigin --> 解决跨域问题
③ AutoWired --> 依赖注入
④ @RequestMapping(“findAll”)
1 package com.jt.controller; 2 3 import com.jt.pojo.User; 4 import com.jt.service.UserService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.CrossOrigin; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import java.util.List; 11 12 @CrossOrigin 13 @RestController 14 public class UserController { 15 @Autowired 16 private UserService userService; 17 18 @RequestMapping("findAll") 19 public List<User> find() { 20 return userService.findAll(); 21 } 22 }
以上是关于SpringBoot项目创建流程--SpringMVC的主要内容,如果未能解决你的问题,请参考以下文章