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

SpringBoot簡單的SpringBoot后端實(shí)例

 更新時(shí)間:2024年03月05日 10:09:12   作者:喵喵@香菜  
這篇文章主要介紹了SpringBoot簡單的SpringBoot后端實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

下面案例作為自己學(xué)習(xí)SpringBoot的筆記,是一個(gè)簡單SpringBoot后端代碼;

包含 entity , dao ,controller

一、開發(fā)準(zhǔn)備

1)Jdk1.7及以上(官方建議使用最新版本的jdk,spring boot對最新版本的jdk也是支持最好的。) jdk安裝省略。

2)Maven安裝:使用maven能夠快速的添加jar包,提高工作效率。

3)IDE:spring tool suit (STS),eclipse,idea等都可以,下面是用sts演示;

二、創(chuàng)建項(xiàng)目

注:以下程序示例是在安裝maven和jdk1.8下進(jìn)行的

創(chuàng)建過程如圖:

第一步:創(chuàng)建項(xiàng)目(這里有jdk的版本和spring boot的版本)

第二步:添加依賴(AOP,MYSQL,JDBC,JPA)

第三步:

創(chuàng)建完成后為:

SpringBootDemoApplication這個(gè)類為包含一個(gè)main方法,啟動(dòng)項(xiàng)目的入口;

Application.properties 這個(gè)是spring boot的配置文件

Pom.xml 這個(gè)是放項(xiàng)目依賴的地方。

項(xiàng)目結(jié)構(gòu)如上,下面來看看內(nèi)部:

Pom:

下面開始編寫示例代碼:

首先就應(yīng)該是Spring Boot連數(shù)據(jù)庫的配置,SpringBoot為我們做了很多自動(dòng)配置,我們只需要配置數(shù)據(jù)地址,用戶名和密碼即可,找到application.properties文件配置如下:

spring.datasource.username=root
spring.datasource.password=你的數(shù)據(jù)庫密碼
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/dbgirl

#JPA 配置
spring.jpa.properties.hibernate.hbm2ddl.auto=update
#Spring mvc 配置前綴
spring.mvc.view.prefix=classpath:/templates/
# … 后綴
spring.mvc.view.suffix=*.html 

創(chuàng)建一個(gè)bean:

package com.b505.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
     * @Name:
     * @Description:  user 實(shí)體類
     * @Author: 追到烏云的盡頭找太陽
     * @Version: V1.00 (版本號(hào))
     * @Create Date: 2017年5月5日下午9:29:18
     * @Parameters: 
     * @Return: 
     */
@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "age")
    private int age;
    @Column(name = "hobby")
    private String hobby;
    //省略set get方法
}

編寫Dao層,Dao層十分簡單,直接繼承Spring data Jpa可以獲取十八中默認(rèn)方法,代碼如下:

package com.b505.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.b505.bean.User;

public interface UserDao extends JpaRepository<User, Integer> {

}

編寫controller(此處直接調(diào)用Dao層,沒有經(jīng)過service)

代碼如下:

package com.b505.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.b505.bean.User;
import com.b505.dao.UserDao;

/**
     * @Name:
     * @Description: user控制類
     * @Author: 追到烏云的盡頭找太陽
     * @Version: V1.00 (版本號(hào))
     * @Create Date: 2017年5月5日下午9:30:14
     * @Parameters: 
     * @Return: 
     */
@RestController
public class UserController {

    @Autowired
    private UserDao userDao;
    //刪除
    @RequestMapping(value ="/delete" ,method = RequestMethod.POST)
    public  void weekdaylDelete(@RequestParam("id") Integer id){
        System.out.println("刪除執(zhí)行");
        userDao.delete(id);
    }
    //添加
    @RequestMapping(value ="/add" ,method = RequestMethod.POST)
    public void weekdayAdd(@RequestParam("name") String name,
            @RequestParam("age") Integer age,@RequestParam("hobby") String hobby
            ){
        User user = new User();
        user.setName(name);
        user.setAge(age);
        user.setHobby(hobby);
        userDao.save(user);
    }
    //查詢所有
    @RequestMapping(value ="/getall" ,method = RequestMethod.GET)
    public List<User> girlList(){
        System.out.println("查詢所有執(zhí)行");
        return  userDao.findAll();
    }
}

至此就已經(jīng)返回json數(shù)據(jù)了,運(yùn)行一下程序進(jìn)行測試

運(yùn)行方法:找到含有mian 方法的類 運(yùn)行 run Java application;

出現(xiàn)類似如圖所示,說明程序編譯通過。

直接在后臺(tái)數(shù)據(jù)庫添加數(shù)據(jù)(也可以是用postman)因?yàn)闉g覽器默認(rèn)是使用get方法:

下面我們在谷歌瀏覽器中訪問:

在地址欄輸入 http://127.0.0.1:8080/getall

測試結(jié)果:

在controller中的add和delete方法測試結(jié)果

測試頁面代碼:

<!DOCTYPE html>
<html lang="en" ng-app="myApp" ng-controller="myController">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/angular.min.js"></script>
    <link href="css/directive_bind.css" rel="external nofollow"  rel="stylesheet" type="text/css" />
    <script src="js/service_http.js"></script>
</head>
<body>
<!--表格-->
<div class="box">
<div class="portlet-title">
    <div class="caption">
        <img class="icon-reorder" src="images/record.png"></img> 人員信息管理 </div>
</div>
<table class="table table-bordered" ng-style="myStyle">
    <!-- ng-style 綁定myStyle屬性,獲取表格的css樣式-->
    <tr>
        <td>姓名</td>
        <td>年齡</td>
        <td>愛好</td>
        <td></td>
    </tr>
    <tr ng-repeat="day in days" >
        <td ng-class-even="'even'"> {{day.name}} </td>
        <td>
         {{day.age}}
        </td>
        <td>
            {{day.hobby}}
        </td>
        <td>
         <button class="delete" ng-click="removeDay(day.id)"><img src="images/Delete (1).png" />刪除 </button>
        </td>
    </tr>
</table>
    <button ng-click="add()">添加人員信息</button>
    <div  id="table1" ng-show='menuState.show'>
        <div class="portlet-title1">
            <div class="caption">
                <img class="icon-reorder" src="images/record.png"></img> 添加信息 </div>
        </div>
        <div class="content" >
            <span>姓名:</span> <input ng-model="name" type="text" placeholder="請輸入姓名" />
        <br>
            <span>年齡:</span> <input ng-model="age" type="text" placeholder="請輸入年齡" />
        <br>
            <span>愛好:</span> <input ng-model="hobby" class="hobby" type="text" placeholder="請輸入愛好" />
        </div>
         <input class="submit" ng-click="submit(name,age,hobby)" type="button" value="提交" />

    </div>
</div>
</body>
</html>

用的是angularJs ,相關(guān)js依賴自行添加;

在瀏覽器中輸入 : http://localhost:8080

映射自行配置,在后面的博客中我寫

點(diǎn)擊添加按鈕:

添加后結(jié)果為:

點(diǎn)擊刪除小明:

總結(jié)

至此簡單的后臺(tái)已經(jīng)搭建完畢,SpringBoot能夠?qū)㈩愔苯愚D(zhuǎn)換為json數(shù)據(jù),這只是SpringBoot眾多功能中的一個(gè);后面我會(huì)將這兩次博客的源碼地址放上!

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談maven 多環(huán)境打包發(fā)布的兩種方式

    淺談maven 多環(huán)境打包發(fā)布的兩種方式

    這篇文章主要介紹了淺談maven 多環(huán)境打包發(fā)布的兩種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • Spring Security LDAP實(shí)現(xiàn)身份驗(yàn)證的項(xiàng)目實(shí)踐

    Spring Security LDAP實(shí)現(xiàn)身份驗(yàn)證的項(xiàng)目實(shí)踐

    在本文中,我們涵蓋了“使用 Spring Boot 的 Spring Security LDAP 身份驗(yàn)證示例”的所有理論和示例部分,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • Java中List分片方式詳細(xì)解析

    Java中List分片方式詳細(xì)解析

    這篇文章主要介紹了Java中List分片方式詳細(xì)解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • SpringBoot瘦身打包部署的實(shí)現(xiàn)

    SpringBoot瘦身打包部署的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot瘦身打包部署的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴}

    淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴}

    這篇文章主要介紹了淺談mybatis返回單一對象或?qū)ο罅斜淼膯栴},具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring bean對象實(shí)例化實(shí)現(xiàn)過程圖解

    Spring bean對象實(shí)例化實(shí)現(xiàn)過程圖解

    這篇文章主要介紹了Spring bean對象實(shí)例化實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼

    SpringBoot使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼

    Spring Boot提供了一種方便的方式來實(shí)現(xiàn)定時(shí)任務(wù),即使用Spring的@Scheduled注解,通過在方法上添加@Scheduled注解,我們可以指定方法在何時(shí)執(zhí)行,本文我們就給大家介紹一下SpringBoot如何使用Scheduling實(shí)現(xiàn)定時(shí)任務(wù),需要的朋友可以參考下
    2023-08-08
  • Centos7安裝JDK1.8詳細(xì)過程實(shí)戰(zhàn)記錄

    Centos7安裝JDK1.8詳細(xì)過程實(shí)戰(zhàn)記錄

    這篇文章主要給大家介紹了關(guān)于Centos7安裝JDK1.8的相關(guān)資料,文中通過圖文以及實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-09-09
  • SpringBoot使用AES對JSON數(shù)據(jù)加密和解密的實(shí)現(xiàn)方法

    SpringBoot使用AES對JSON數(shù)據(jù)加密和解密的實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot使用AES對JSON數(shù)據(jù)加密和解密的實(shí)現(xiàn)方法,文章通過代碼示例介紹的非常詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-08-08
  • IntelliJ IDEA 中g(shù)it的使用圖文教程

    IntelliJ IDEA 中g(shù)it的使用圖文教程

    本文通過圖文并茂的形式給大家介紹了IntelliJ IDEA 中g(shù)it的使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2018-02-02

最新評(píng)論