欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目快速搭建詳細(xì)步驟

 更新時(shí)間:2022年08月16日 14:57:33   作者:Zzz_me  
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程,下面這篇文章主要給大家介紹了一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目快速搭建詳細(xì)步驟,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

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

第一步新建項(xiàng)目

1.選擇Spring Initializr

2.點(diǎn)擊下一步

3.修改jdk的版本,再點(diǎn)擊下一步

4.選中Spring Web,再下一步

5.給項(xiàng)目文件命名,再點(diǎn)擊完成

這樣子就會(huì)生成一個(gè)項(xiàng)目,如下圖所示

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

第二步導(dǎo)入依賴

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

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

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

		<!--springboot+mybatis的依賴-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
		<!--MySQL數(shù)據(jù)庫(kù)驅(qū)動(dòng)-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
		<!--druid數(shù)據(jù)庫(kù)連接池依賴-->
        <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

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

配置項(xiàng)目需要修改的端口號(hào)、datasource、mybatis。

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

    #Spring Boot 默認(rèn)是不注入這些屬性值的,需要自己綁定
    #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)計(jì)攔截的filters,stat:監(jiān)控統(tǒng)計(jì)、log4j:日志記錄、wall:防御sql注入
    #如果允許時(shí)報(bào)錯(cuò)  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #則導(dǎo)入 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自動(dòng)掃描到指定義的pojo包下
  type-aliases-package: com.me.test.pojo
  #指定位置掃描Mapper接口對(duì)應(yīng)的XML文件 classpath:xml文件位置
  mapper-locations: classpath:mapper/*.xml

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

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

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

spl語(yǔ)句代碼

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');

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

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

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

創(chuàng)建pojo類

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

創(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();
}

創(chuàng)建對(duì)于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>

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

<?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>

圖中爆紅不用管這個(gè)是因?yàn)槲遗渲昧艘粋€(gè)插件的原因,實(shí)際在運(yùn)行時(shí)不影響效果!

創(chuàng)建service層

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

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中配置一個(gè)fastjson依賴這是阿里巴巴開(kāi)源的,用來(lái)轉(zhuǎn)換成JSON和類的格式的。

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

我使用了RestFull風(fēng)格去實(shí)現(xiàn)路徑的請(qǐng)求

代碼

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

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

    //使用了RestFull風(fēng)格
    @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請(qǐng)求
    //@RequestBody 表示接收請(qǐng)求是JSON格式的數(shù)據(jù)
    @PostMapping
    @ResponseBody
    public String add(@RequestBody UserInfo userInfo){
        userInfoService.add(userInfo);
        return "添加OK";
    }

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

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

第五步測(cè)試請(qǐng)求

本人測(cè)試使用的工具是Postman

Postman下載路徑:https://app.getpostman.com/app/download/win64

查詢測(cè)試

查詢沒(méi)問(wèn)題

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

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

修改測(cè)試

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

刪除測(cè)試

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

源碼路徑:https://gitee.com/mehao123/meTest

總結(jié)

到此這篇關(guān)于SpringBoot項(xiàng)目快速搭建的文章就介紹到這了,更多相關(guān)SpringBoot項(xiàng)目搭建步驟內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序

    mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序

    這篇文章主要介紹了mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 老生常談Java中instanceof關(guān)鍵字的理解

    老生常談Java中instanceof關(guān)鍵字的理解

    java 中的instanceof 運(yùn)算符是用來(lái)在運(yùn)行時(shí)指出對(duì)象是否是特定類的一個(gè)實(shí)例。這篇文章主要介紹了老生常談Java中instanceof關(guān)鍵字的理解,需要的朋友可以參考下
    2018-10-10
  • Java并發(fā)編程線程間通訊實(shí)現(xiàn)過(guò)程詳解

    Java并發(fā)編程線程間通訊實(shí)現(xiàn)過(guò)程詳解

    這篇文章主要介紹了Java并發(fā)編程線程間通訊實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 詳解java IO流之緩沖流的使用

    詳解java IO流之緩沖流的使用

    本文主要介紹了java的IO流中的緩沖流的使用,緩沖流分為字節(jié)和字符緩沖流。分享了有關(guān)它們的實(shí)例代碼,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Java中IO和NIO的區(qū)別詳細(xì)解析

    Java中IO和NIO的區(qū)別詳細(xì)解析

    這篇文章主要介紹了Java中IO和NIO的區(qū)別詳細(xì)解析,IO和NIO有相同的作用和目的,但實(shí)現(xiàn)方式不同,NIO主要用到的是塊,所以NIO的效率要比IO快不少,需要的朋友可以參考下
    2023-11-11
  • Java實(shí)現(xiàn)快速生成詞云圖的示例代碼

    Java實(shí)現(xiàn)快速生成詞云圖的示例代碼

    詞云(Word?Cloud),又稱文字云、標(biāo)簽云(Tag?Cloud)、關(guān)鍵詞云(Keyword?Cloud),是對(duì)文本信息中一定數(shù)量的關(guān)鍵詞出現(xiàn)的頻率高低情況的一種可視化展現(xiàn)方式。本文將用Java代碼實(shí)現(xiàn)快速生成詞云圖,需要的可以參考一下
    2023-02-02
  • Android內(nèi)存泄漏實(shí)戰(zhàn)解析

    Android內(nèi)存泄漏實(shí)戰(zhàn)解析

    Java是垃圾回收語(yǔ)言的一種。這篇文章主要介紹了Android內(nèi)存泄漏 的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • spring boot metrics監(jiān)控指標(biāo)使用教程

    spring boot metrics監(jiān)控指標(biāo)使用教程

    這篇文章主要為大家介紹了針對(duì)應(yīng)用監(jiān)控指標(biāo)暴露spring boot metrics監(jiān)控指標(biāo)的使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • Java協(xié)議字節(jié)操作工具類詳情

    Java協(xié)議字節(jié)操作工具類詳情

    這篇文章主要介紹了Java協(xié)議字節(jié)操作工具類詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • SpringCloud之loadbalancer負(fù)載均衡組件實(shí)戰(zhàn)詳解

    SpringCloud之loadbalancer負(fù)載均衡組件實(shí)戰(zhàn)詳解

    LoadBalancer是Spring Cloud官方提供的負(fù)載均衡組件,可用于替代Ribbon,這篇文章主要介紹了SpringCloud之loadbalancer負(fù)載均衡組件,需要的朋友可以參考下
    2023-06-06

最新評(píng)論