栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

sprintboot项目创建及运行demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

sprintboot项目创建及运行demo

本文目录
  • 一、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中加入如下代码:


        org.springframework.boot
        spring-boot-starter-parent
        2.6.7



        
            org.springframework.boot
            spring-boot-starter
        


            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
            mysql
            mysql-connector-java
            8.0.29
        
        
            org.apache.ibatis
            ibatis-core
        

终端链接你本地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=你数据密码

在srcmainjava下新建文件夹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的:


     org.springframework.boot
     spring-boot-starter

的依赖改为:


     org.springframework.boot
     spring-boot-starter-web

如下图目录:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850834.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号