本文目录
前言
踏入Java全栈开发的广阔世界,从基础环境的搭建到首个Spring Web项目的成功部署,每一步都是通往技术精进的必经之路。本指南旨在为新晋开发者提供一条清晰的学习路径,从Java JDK的下载开始,逐步深入到Mac M1芯片上Maven的安装与配置,再到IntelliJ IDEA这一强大IDE的引入与个性化设置。我们将特别关注如何在IDEA中正确指定Maven版本,确保开发环境的顺畅运行。紧接着,通过创建Maven项目,您将学会项目结构的基础搭建与管理。最终,我们将携手步入Spring框架的殿堂,从零开始搭建一个Spring Web工程,为您的全栈开发之旅奠定坚实的基础。无论您是初出茅庐的编程爱好者,还是寻求技术转型的职场人士,本指南都将是您不可或缺的启航伙伴。
1、java jdk下载
下载地址。我这边下载的是17:
2、Mac m1安装maven
下载安装地址。这里我下载的是3.8.5版本:
下载完之后配置.bash_profile
或者.zshrc
:
# maven
export MAVEN_HOME=/Users/justin/apache-maven-3.8.5
export PATH=$PATH:$MAVEN_HOME/bin
# maven end
路径换成你自己的路径。
测试:mvn -v
:
3、安装IntelliJ IDEA
下载安装IntelliJ IDEA
。选择适合自己电脑的版本安装即可。
4、配置IntelliJ IDEA中的maven版本
换成你本地安装的maven
。
5、创建maven项目
在pom.xml
中加入如下代码:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
</dependency>
</dependencies>
终端链接你本地mysql
服务:
mysql -uroot -p
并创建数据库tuling
:
create database tuling;
在tuling数据库下创建表:
create table t1(
a int not null,
b int not null,
c int not null,
d int not null,
e varchar(20) not null
) engine=innodb;
在resources
文件夹下新建application.properties
文件,写入:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tuling
spring.datasource.username=root
spring.datasource.password=你数据密码
在src\main\java
下新建文件夹com.justin
,新建文件如下:
UserMapper
写入代码:
package com.justin.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
@Insert("insert into t1 values(1,1,1,1,'1')")
void insertOne();
}
UserService
写入代码:
package com.justin.service;
import com.justin.mapper.UserMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Component
public class UserService {
@Resource
private UserMapper userMapper;
@Transactional
public void test() {
userMapper.insertOne();
throw new NullPointerException();
}
}
MyApplication
写入代码:
package com.justin;
import com.justin.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(MyApplication.class);
UserService userService = applicationContext.getBean(UserService.class);
userService.test();
}
}
点击运行
可以看到报错了,数据库也并没有插入任何数据。不过这个报错正是我们想要的,事务回滚了,我们接着把UserService
里的这行代码throw new NullPointerException();
注释掉:
运行:
可以看到正常,并且数据库也插入了数据:
6、spring搭建web工程
我们将pom.xml
的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
的依赖改为:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如下图目录:
新建UserController
写入:
package com.justin.controller;
import com.justin.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("/test")
public void test() {
userService.test();
}
}
那么我们在MyApplication
中修改代码如下:
package com.justin;
import com.justin.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
在这里直接去运行就行了。
运行项目,我们在浏览器中访问http://localhost:8080/test
,就可以看到访问一次,数据库就插入一条数据:
在学习
springboot
的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。