从Java环境搭建到Spring Web工程部署

前言

踏入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的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你华还是你华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值