亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

1.創(chuàng)建Springboot項(xiàng)目

開發(fā)工具我們采用IntelliJ IDEA,環(huán)境安裝不在敘述,如果對環(huán)境搭建不會(huì)的朋友,可以評論或私聊,本人后期在寫一章環(huán)境搭建,MySQL安裝的文章:

  1. 打開IntelliJ IDEA , new Project 或 File -> New -> Project... ;
  2. 選擇 Spring initializr ,選擇對應(yīng)的Project SDK后 點(diǎn)擊Next ,如果Project SDK沒有可選項(xiàng),請先安裝sdk,
  3.  

如上圖所示 Group 修改為自己的報(bào)名,推薦為域名+項(xiàng)目名,比如** www.td0f7.cn.springboot01* * Artifact 為自己的項(xiàng)目名稱,英文可以下劃線比如spring_boot_demo type 為包管理方式,如果不了解這兩種推薦用默認(rèn)的方式即可,不用修改 Lauguage 為開發(fā)語言,默認(rèn)為JAVA,可自行修改 Packaging 為打包方式,本人習(xí)慣用war所以選擇war方式 Java Version 為開發(fā)語言的版本號,我本地java版本號是8,所以選擇8

SpringBoot+Mysql做登陸接口,拋棄mapper.xml

 

改動(dòng)完成,點(diǎn)擊 Next 出現(xiàn)第二個(gè)界面,在點(diǎn)擊下一步

  1.  

Project Name是項(xiàng)目名稱,修改成自己的即可,默認(rèn)是上個(gè)界面帶過來的,不用修改 Project location 為項(xiàng)目存儲(chǔ)路徑修改為自己的存儲(chǔ)路徑即可 點(diǎn)擊Firish,等待加載完成

2.項(xiàng)目結(jié)構(gòu)介紹

SpringBoot+Mysql做登陸接口,拋棄mapper.xml

 

如圖所示 根目錄下 pom.xml 是包管理,引入一些新的jar包都在這,修改springboot版本號,修改打包方式等等 src main java 是寫代碼的地方 SpringBootDemoApplication 是程序主入口 resources 存儲(chǔ)一些資源 application.properties 是配置文件,配置端口號,mysql,redis等等都在這里 static 是靜態(tài)資源,js css 圖片等 templates 放html界面

其他等教程用到在詳細(xì)解釋

3.引入mysql等包

由于新項(xiàng)目沒有引入任何包,所以要引入數(shù)據(jù)庫的包。 首先打開項(xiàng)目根目錄的 pom.xml 在內(nèi)容 dependencies 節(jié)點(diǎn)下添加

<dependencies>
    	<!--新添加的-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
				

		<!--項(xiàng)目自帶的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-Tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
復(fù)制代碼

lombok為實(shí)體類工具包,可以自動(dòng)添加get set方法 mysql 是鏈接mysql數(shù)據(jù)庫必須添加的包 org.mybatis.spring.boot 是mysql數(shù)據(jù)庫的封裝框架,更簡易的操作數(shù)據(jù)庫 gson 是對象轉(zhuǎn)json字符串的工具類

4.修改數(shù)據(jù)庫配置文件

打開項(xiàng)目根目錄 src -> main -> resources -> application.properties 文件

spring.datasource.url=jdbc:mysql://mysqlip:端口/數(shù)據(jù)庫名?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=數(shù)據(jù)庫賬號
spring.datasource.password=數(shù)據(jù)庫密碼
復(fù)制代碼

spring.datasource.url=jdbc:mysql://127.0.0.1/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=springboot
spring.datasource.password=wangzhong
復(fù)制代碼

mysql默認(rèn)端口是3306,如果沒有修改過端口號,則spring.datasource.url=的端口號可以省略不寫

5.創(chuàng)建mysql數(shù)據(jù)庫user表

CREATE TABLE `springboot`.`user` ( `id` INT NOT NULL AUTO_INCREMENT , `user` VARCHAR(30) NOT NULL , `pass` VARCHAR(30) NOT NULL , PRIMARY KEY (`id`), UNIQUE `u_user` (`user`) ENGINE = MEMORY;
復(fù)制代碼

在mysql數(shù)據(jù)庫執(zhí)行這段mysql語句添加user表 id 主鍵自增 user 登錄賬號,不重復(fù) pass 登陸密碼

環(huán)境到這里就算完成了,接下來要寫代碼了

6.編寫代碼實(shí)現(xiàn)登陸接口

首先在 src main java 你的包 下 SpringBootDemoApplication 同級目錄下創(chuàng)建 mapper controller entity 三個(gè)包 mapper 存儲(chǔ)操作數(shù)據(jù)庫的代碼 controller 存儲(chǔ)api的代碼,前臺調(diào)用的api都是這個(gè)下的 entity 存儲(chǔ)數(shù)據(jù)庫對應(yīng)的實(shí)體類

首先是entity包下創(chuàng)建User實(shí)體類文件

import lombok.Data;

//@Data 注釋會(huì)為我們自動(dòng)給該實(shí)體類的屬性添加 get set方法,必須引用了lombok包才可以使用
@Data
public class User {
    private int id;
    private String user;
    private String pass;
}

復(fù)制代碼

創(chuàng)建UserMapper,名稱一般為數(shù)據(jù)庫表名+Mapper,類型為interface

import org.Apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import www.td0f7.cn.springboot01.spring_boot_demo.entity.User;

//必須有這個(gè)注解,否則無法掃描到該mapper
@Mapper
public interface UserMapper {
    /**
     * @Select 代表查詢語句
     * #{user} 代表 取該方法中 user參數(shù)
     * @返回值 登陸是查詢,由于user登陸賬號是唯一的,所有返回類型是一個(gè)User對象而不是集合
     */
    @Select("select * from user where user = #{user} and pass = #{pass}")
    User login(String user, String pass);
}

復(fù)制代碼

創(chuàng)建UserController,給外部提供可調(diào)用的api

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import www.td0f7.cn.springboot01.spring_boot_demo.entity.User;
import www.td0f7.cn.springboot01.spring_boot_demo.mapper.UserMapper;

@RestController//如果返回值為對象,則自動(dòng)轉(zhuǎn)換成json
//@RequestMapping("user"),此注釋代表這個(gè)controller需要+user才可以訪問,如下
//我們啟動(dòng)的網(wǎng)站都是http://localhost:8080,要訪問這個(gè)controller則是http://localhost:8080/user
@RequestMapping("user")
public class UserController {
    @Autowired(required = false)//自動(dòng)掃描mapper文件
    private UserMapper mapper;

    /**
     * 該接口為post方式
     *
     * @param user 參數(shù)user默認(rèn)值為空,不寫defaultValue為null
     * @param pass
     * @return
     */
    @PostMapping("login")
    public String login(@RequestParam(value = "user", defaultValue = "") String user,
                        @RequestParam(value = "pass", defaultValue = "") String pass) {
        if (user.equals("")) return "賬號必傳";
        if (pass.equals("")) return "密碼必傳";
        User user1 = mapper.login(user, pass);
        if (user1 == null) {//沒有查詢到數(shù)據(jù),代表沒有此賬號
            return "賬號密碼不正確!";
        } else {
            return new Gson().toJson(user1);//登陸成功則給對象轉(zhuǎn)json字符串返回
        }
    }
}
復(fù)制代碼

7.運(yùn)行開始測試

數(shù)據(jù)庫添加數(shù)據(jù)

INSERT INTO `user`(`user`, `pass`) VALUES ('wz', '123456')
復(fù)制代碼

以上添加了賬號 wz 密碼 123456 的數(shù)據(jù)

接下來運(yùn)行項(xiàng)目測試

SpringBoot+Mysql做登陸接口,拋棄mapper.xml

 

使用postman測試

SpringBoot+Mysql做登陸接口,拋棄mapper.xml

 


SpringBoot+Mysql做登陸接口,拋棄mapper.xml

 


SpringBoot+Mysql做登陸接口,拋棄mapper.xml

 


SpringBoot+Mysql做登陸接口,拋棄mapper.xml

 

ok,登陸功能完成!!! 注冊功能后期發(fā)布

分享到:
標(biāo)簽:SpringBoot
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定