sprintboot项目创建及运行demo

sprintboot项目创建及运行demo,第1张

本文目录
  • 一、java jdk下载
  • 二、Mac m1安装maven
  • 三、安装IntelliJ IDEA
  • 四、配置IntelliJ IDEA中的maven版本
  • 五、创建maven项目
  • 六、spring搭建web工程

一、java jdk下载

下载地址。我这边下载的是17:

二、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

三、安装IntelliJ IDEA

下载安装IntelliJ IDEA。选择适合自己电脑的版本安装即可。

四、配置IntelliJ IDEA中的maven版本



换成你本地安装的maven


五、创建maven项目


pom.xml中加入如下代码:

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.7version>
parent>

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

<dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.2.2version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.29version>
        dependency>
        <dependency>
            <groupId>org.apache.ibatisgroupId>
            <artifactId>ibatis-coreartifactId>
        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();注释掉:

运行:

可以看到正常,并且数据库也插入了数据:

六、spring搭建web工程

我们将pom.xml的:

<dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starterartifactId>
dependency>

的依赖改为:

<dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-starter-webartifactId>
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的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/795332.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-06
下一篇2022-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存