国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

快速搭建一個簡單的SpringBoot項目-詳細步驟

這篇具有很好參考價值的文章主要介紹了快速搭建一個簡單的SpringBoot項目-詳細步驟。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

快速搭建一個簡單的SpringBoot項目

前言

  • 本文章僅供大家參考,如果對大家有起到幫助的話可以點贊支持一下~
  • 主要發(fā)布是為了本人以后能方便的搭建一個SpringBoot項目的框架?。?!
  • 源碼路徑在文章最下方!

第一步新建項目

1.選擇Spring Initializr

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

2.點擊下一步

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

3.修改jdk的版本,再點擊下一步
注意!
springboot項目搭建流程,配置用法,spring boot,后端,java,idea

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

4.選中Spring Web,再下一步

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

5.給項目文件命名,再點擊完成

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

這樣子就會生成一個項目,如下圖所示

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

下圖中這些文件如果沒有需要的情況下一般就直接刪掉就好了!

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

第二步導入依賴

按照上面的步驟完成的打開pom.xml文件的配置依賴應(yīng)該和我的是一樣的!

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

接著我們添加一些需要的依賴

SpringBoot項目需要提供一個接口去拿到數(shù)據(jù)所有在這里我們需要能連接數(shù)據(jù)庫的配置

		<!--springboot+mybatis的依賴-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
		<!--MySQL數(shù)據(jù)庫驅(qū)動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
		<!--druid數(shù)據(jù)庫連接池依賴-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
		<!--Lombok依賴(可以配置也可以不用配置具體看自己)-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

第三步配置Application

新建一個application.yml文件 (使用aplication.properties也是可以的,只是本人一般使用.yml格式的)

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

配置項目需要修改的端口號、datasource、mybatis。

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

server:
  #設(shè)置端口號
  port: 8081 #默認端口是8080
spring:
  datasource:
    #數(shù)據(jù)庫用戶名
    username: root
    #數(shù)據(jù)庫用戶密碼
    password: 123456
    #serverTimezone=UTC 解決市區(qū)的報錯 一般mysql是8.0以上的是必須配置這個
    #userUnicode=true&characterEncoding=utf-8 指定字符編碼、解碼格式
    url: jdbc:mysql://localhost:3306/metest?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8
    #設(shè)置驅(qū)動類
    driver-class-name: com.mysql.cj.jdbc.Driver
    #設(shè)置數(shù)據(jù)源
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默認是不注入這些屬性值的,需要自己綁定
    #druid 數(shù)據(jù)源專有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #配置監(jiān)控統(tǒng)計攔截的filters,stat:監(jiān)控統(tǒng)計、log4j:日志記錄、wall:防御sql注入
    #如果允許時報錯  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #則導入 log4j 依賴即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

# 配置mybatis
mybatis:
  #指定pojo掃描包位置讓mybatis自動掃描到指定義的pojo包下
  type-aliases-package: com.me.test.pojo
  #指定位置掃描Mapper接口對應(yīng)的XML文件 classpath:xml文件位置
  mapper-locations: classpath:mapper/*.xml

第四步創(chuàng)建需要的mapper、service、cotroller層

創(chuàng)建需要的文件夾

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

創(chuàng)建數(shù)據(jù)庫

spl語句代碼

CREATE DATABASE /*!32312 IF NOT EXISTS*/`metest` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `metest`;

/*Table structure for table `userinfo` */

DROP TABLE IF EXISTS `userinfo`;

CREATE TABLE `userinfo` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,
  `authority` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

/*Data for the table `userinfo` */

insert  into `userinfo`(`id`,`username`,`password`,`authority`) values (1,'root','123456','admin'),(2,'me','123456','admin');

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

IDEA連接上Mysql數(shù)據(jù)庫(主要為了方便查看創(chuàng)建pojo類和對于的mapper.xml文件)

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

找到需要的數(shù)據(jù)庫

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

一般pojo類、mapper接口、service接口名字都是按照數(shù)據(jù)庫中表的名字來創(chuàng)建的

創(chuàng)建pojo類

//使用@Data自動生成需要的get、set
@Data
//使用@AllArgsConstructor自動生成有參構(gòu)造
@AllArgsConstructor
//使用@NoArgsConstructor自動生成無參構(gòu)造
@NoArgsConstructor
public class userInfo {
    
    private Integer id;
    private String username;
    private String password;
    private String authority;
}

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

創(chuàng)建mapper接口

@Repository
@Mapper
public interface UserInfoMapper {

    /**
     * 增加一條數(shù)據(jù)
     * @param userInfo 數(shù)據(jù)
     */
    void add(UserInfo userInfo);

    /**
     * 刪除一條數(shù)據(jù)
     * @param id 被刪除數(shù)據(jù)的id
     */
    void delete(Integer id);

    /**
     * 修改一條數(shù)據(jù)
     * @param userInfo 修改的數(shù)據(jù)
     */
    void update(UserInfo userInfo);

    /**
     * 根據(jù)id去查詢一條數(shù)據(jù)
     * @param id 查詢的id
     */
    UserInfo queryById(Integer id);

    /**
     * 查詢?nèi)繑?shù)據(jù)
     * @return
     */
    List<UserInfo> queryAll();
}

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

創(chuàng)建對于mapper接口的xml文件

需要的mapper基本配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.me.test.mapper.UserInfoMapper">


</mapper>

對于接口中的方法在添加需要的增刪改查功能(原配置代碼有問題、目前已修改)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.me.test.mapper.UserInfoMapper">

    <insert id="add" parameterType="UserInfo">
        insert into metest.userinfo (username, password, authority)
         values (#{username},#{password},#{authority});
    </insert>

    <delete id="delete" parameterType="Integer">
        delete from metest.userinfo where id = #{id};
    </delete>

    <update id="update" parameterType="UserInfo">
        update metest.userinfo set username=#{username},password=#{password},authority=#{authority}
        where id=#{id};
    </update>

    <select id="queryById" parameterType="Integer" resultType="UserInfo">
        select * from metest.userinfo where id=#{id};
    </select>

    <select id="queryAll" resultType="UserInfo">
        select * from metest.userinfo;
    </select>

</mapper>

圖中爆紅不用管這個是因為我配置了一個插件的原因,實際在運行時不影響效果!

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

創(chuàng)建service層

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

UserInfoService代碼(其實其中的方法也就是Maper接口中拷貝來的)

public interface UserInfoService {
    /**
     * 增加一條數(shù)據(jù)
     * @param userInfo 數(shù)據(jù)
     */
    void add(UserInfo userInfo);

    /**
     * 刪除一條數(shù)據(jù)
     * @param id 被刪除數(shù)據(jù)的id
     */
    void delete(Integer id);

    /**
     * 修改一條數(shù)據(jù)
     * @param userInfo 修改的數(shù)據(jù)
     */
    void update(UserInfo userInfo);

    /**
     * 根據(jù)id去查詢一條數(shù)據(jù)
     * @param id 查詢的id
     */
    UserInfo queryById(Integer id);

    /**
     * 查詢?nèi)繑?shù)據(jù)
     * @return
     */
    List<UserInfo> queryAll();
}

UserInfoServiceImpl代碼(主要是做業(yè)務(wù)邏輯的)

有需要添加的功能可以直接在這一層添加修改

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void add(UserInfo userInfo) {
        userInfoMapper.add(userInfo);
    }

    @Override
    public void delete(Integer id) {
        userInfoMapper.delete(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.update(userInfo);
    }

    @Override
    public UserInfo queryById(Integer id) {
        return userInfoMapper.queryById(id);
    }

    @Override
    public List<UserInfo> queryAll() {
        return userInfoMapper.queryAll();
    }
}

創(chuàng)建controller層

這里我先去pom中配置一個fastjson依賴這是阿里巴巴開源的,用來轉(zhuǎn)換成JSON和類的格式的。

<!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>

我使用了RestFull風格去實現(xiàn)路徑的請求

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

代碼

//@Controller 控制層需要的注解
//@RestController 使用這個也是可以的,但是使用后他里面所有請求返回的都是字符串!
//一般只需要作為接口放回JSON格式數(shù)據(jù)的話推薦使用@RestController
//@Controller這個是可以與Thymeleaf模板引擎使用時可以返回一個頁面的
@Controller
//@RequestMapping指定路徑名
//@RequestMapping("/test")用這個來指定路徑也是可以的
@RequestMapping(value = "/test")
public class UserInfoController {
    //獲取到UserInfoService
    @Autowired
    private UserInfoService userInfoService;

    //Get請求
    @GetMapping
    //@ResponseBody 注釋后表示放回的是字符串
    @ResponseBody
    public String queryAll(){
        List<UserInfo> userInfoList = userInfoService.queryAll();
        return JSON.toJSONString(userInfoList);
    }

    //使用了RestFull風格
    @GetMapping("/{id}")
    @ResponseBody
    public String query(@PathVariable(value = "id")Integer id){
        UserInfo userInfo = userInfoService.queryById(id);
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(userInfo);
        return JSON.toJSONString(userInfoList);
    }

    //post請求
    //@RequestBody 表示接收請求是JSON格式的數(shù)據(jù)
    @PostMapping
    @ResponseBody
    public String add(@RequestBody UserInfo userInfo){
        userInfoService.add(userInfo);
        return "添加OK";
    }

    //Delete請求
    @DeleteMapping(value = "/{id}")
    @ResponseBody
    public String delete(@PathVariable("id")Integer id){
        userInfoService.delete(id);
        return "刪除成功";
    }

    //Put請求
    @PutMapping("/{id}")
    @ResponseBody
    public String update(@PathVariable("id")Integer id,
            @RequestBody UserInfo userInfo){
        userInfo.setId(id);
        userInfoService.update(userInfo);
        return "修改成功";
    }
}

第五步測試請求

本人測試使用的工具是Postman
Postman下載路徑:https://app.getpostman.com/app/download/win64

查詢測試

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

查詢沒問題

增加數(shù)據(jù)測試

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

此時數(shù)據(jù)庫數(shù)據(jù)也多了一條數(shù)據(jù)

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

修改測試

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

此時數(shù)據(jù)庫的數(shù)據(jù)也發(fā)生了改變

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

刪除測試

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

此時數(shù)據(jù)就被刪除了

springboot項目搭建流程,配置用法,spring boot,后端,java,idea

源碼路徑:https://gitee.com/mehao123/meTest文章來源地址http://www.zghlxwxcb.cn/news/detail-692395.html

到了這里,關(guān)于快速搭建一個簡單的SpringBoot項目-詳細步驟的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【快速解決】使用IDEA快速搭建SpringBoot項目(超詳細)

    【快速解決】使用IDEA快速搭建SpringBoot項目(超詳細)

    前言 ????????Spring Boot是Spring Framework的一款腳手架式框架,可以幫助開發(fā)者快速構(gòu)建基于Spring的企業(yè)級應(yīng)用程序。本篇博客將介紹如何使用IntelliJ IDEA(以下簡稱IDEA)來快速搭建一個Spring Boot項目。 目錄 ?編輯 前言 使用IDEA快速搭建SpringBoot項目 建立步驟 第一步新建一個

    2024年04月09日
    瀏覽(20)
  • 一個簡單的增刪改查Spring boot項目教程(完整過程,附代碼)(從搭建數(shù)據(jù)庫到實現(xiàn)增刪改查功能),Springboot學習,Springboot項目,

    一個簡單的增刪改查Spring boot項目教程(完整過程,附代碼)(從搭建數(shù)據(jù)庫到實現(xiàn)增刪改查功能),Springboot學習,Springboot項目,

    這里將會介紹怎么去搭建一個簡單增刪改查的Springboot項目,認真看完我相信你一定能夠?qū)W會,并且附有完整代碼; 首先要進行增刪改查肯定是要有供操作的數(shù)據(jù)庫; 這里我是用的SQLyog來搭建的,隨便用什么都可以,只要能確保給項目一個配套的數(shù)據(jù)庫就行; 打開IDEA,創(chuàng)建

    2024年02月15日
    瀏覽(98)
  • 用戶登錄前后端開發(fā)(一個簡單完整的小項目)——SpringBoot與session驗證(帶前后端源碼)全方位全流程超詳細教程

    用戶登錄前后端開發(fā)(一個簡單完整的小項目)——SpringBoot與session驗證(帶前后端源碼)全方位全流程超詳細教程

    ??注:不要看我的文件多,那是我的其他項目,這個項目所用的文件我會全部用紅框框起來,沒框的部分不用管,前端兩個文件,后端一個文件??? ??歡迎來到dream_ready的博客,??相信你對這篇博客也感興趣o?(ˉ▽ˉ;) 表白墻/留言墻 —— 初級SpringBoot項目,練手項目前后

    2024年02月06日
    瀏覽(22)
  • OpenStack搭建史上最詳細步驟 (快速入手)

    OpenStack搭建史上最詳細步驟 (快速入手)

    搭建openstack平臺所需要的兩個鏡像包:CentOS-7-X86_64-DVD-1804.iso 和 chinaskill_cloud_iaas.iso鏡像文件。 在VMware上準備兩臺虛擬機,分別作為controller(控制)節(jié)點和compute節(jié)點. 下面是VMware上虛擬機的基礎(chǔ)配置。 computecontroller 雙網(wǎng)卡,NAT模式和僅主機模式,配置硬盤各給50G 多添的一塊

    2024年02月02日
    瀏覽(48)
  • 開發(fā)一個簡單易用的SDK的詳細步驟(超詳細,超適用)

    開發(fā)一個簡單易用的SDK的詳細步驟(超詳細,超適用)

    創(chuàng)建starter步驟 1.新建一個 spring boot 初始化項目 2.添加依賴,Lombok, Spring Configuration Processor Spring Configuration Processor 的作用是自動生成代碼提示 3.修改 pom 文件的版本號,并刪除 build 4.刪除原本自動創(chuàng)建的主類,新建一個類, 并添加需要用到的依賴 5.在 resources 目錄下新建 META-

    2023年04月15日
    瀏覽(13)
  • IDEA快速創(chuàng)建maven項目詳細步驟

    IDEA快速創(chuàng)建maven項目詳細步驟

    目錄 前言必讀: 1.Maven和Maven Archetype區(qū)別? 2.創(chuàng)建maven項目之前的步驟(必看) 一、創(chuàng)建maven 1.打開idea---文件---新建---項目 2.新建項目---自定義項目名稱---選擇Maven--創(chuàng)建 3. 創(chuàng)建成功 ?二、idea里面配置maven 4. 打開IDEA---文件--設(shè)置(小扳手) ?5.搜索欄里面搜索:maven ?6.修改原

    2024年01月25日
    瀏覽(91)
  • Git快速入門篇—— Windows版本淘寶鏡像快速下載安裝詳細步驟及簡單入門教程(附帶圖文教程)

    Git快速入門篇—— Windows版本淘寶鏡像快速下載安裝詳細步驟及簡單入門教程(附帶圖文教程)

    前言:我們平時在整理代碼的時候,尤其是與別人一起開發(fā)項目的時候,常常涉及到代碼的更新,因此代碼版本問題成了一個很頭痛的事。而git正是為了解決這種問題而誕生。本文將詳細介紹如何通過淘寶鏡像進行g(shù)it的安裝以及git的簡單入門技巧。 下一章: git與遠程倉庫的交

    2024年02月03日
    瀏覽(31)
  • 如何快速搭建一個大模型?簡單的UI實現(xiàn)

    如何快速搭建一個大模型?簡單的UI實現(xiàn)

    ??博客主頁: 是dream ??系列專欄: 深度學習環(huán)境搭建、環(huán)境配置問題解決、自然語言處理、語音信號處理、項目開發(fā) ??每日語錄:相信自己,一路風景一路歌,人生之美,正在于此。 ??感謝大家點贊??收藏?指正?? 前言:本文章純屬是自己無聊,調(diào)用了星火認知大模

    2024年02月05日
    瀏覽(22)
  • Linux部署web項目/springboot項目(詳細步驟)

    Linux部署web項目/springboot項目(詳細步驟)

    小編我將用CSDN記錄軟件開發(fā)求學之路上親身所得與所學的心得與知識,有興趣的小伙伴可以關(guān)注一下! 也許一個人獨行,可以走的很快,但是一群人結(jié)伴而行,才能走的更遠!讓我們在成長的道路上互相學習,讓我們共同進步,歡迎關(guān)注! 目錄 一、安裝jdk 1:問題 1.1Openj

    2024年02月03日
    瀏覽(18)
  • springboot+vue真實項目部署詳細步驟

    springboot+vue真實項目部署詳細步驟

    下面是實際項目部署完整詳細步驟,僅供參考。 首先需要以下文件: 后端包(關(guān)聯(lián)交易需要: toplink-admin.jar后臺管理包 和 toplink-rule-server.jar規(guī)則引擎服務(wù)包 )Maven-clean-package 后端配置文件,比如application.ym等,用于后面把配置文件提取到和jar包同一路徑進行修改 sql文件 前端

    2024年02月14日
    瀏覽(66)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包