亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot Service和Dao的編寫詳解

 更新時間:2020年11月23日 09:14:15   作者:瑞 新  
這篇文章主要介紹了SpringBoot Service和Dao的編寫詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文主要介紹了SpringBoot Service和Dao的編寫詳解,分享給大家,具體如下:

效果圖

在這里插入圖片描述

配置環(huán)境

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

數(shù)據(jù)庫中文編碼

在這里插入圖片描述

建表

create table `student` (
	`id` int(11) Not NULL AUTO_INCREMENT COMMENT '主鍵自增id',
	`name` varchar(250) NOT NULL DEFAULT ' ' COMMENT '姓名',
	PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;

在這里插入圖片描述

pom依賴和配置

mybatis 和 mysql

<!--    connect-->
<!--    不同版本mybatis對應(yīng)boot版本不同-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.1</version>
    </dependency>
<!--    mysql版本可以不指定-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

application.properties

# mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rxguo_test?serverTimezone=UTC&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=

java bean

package com.bennyrhys.com.shop.bean;

public class Student {
  Integer id;
  String name;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "Student{" +
        "id=" + id +
        ", name='" + name + '\'' +
        '}';
  }
}

Controller

package com.bennyrhys.com.shop;

import com.bennyrhys.com.shop.bean.Student;
import com.bennyrhys.com.shop.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {
  @Autowired
  StudentService studentService;

  @GetMapping("/student")
  public String getStudentById(@RequestParam Integer id) {
    Student student = studentService.getStudentById(id);
    return student.toString();
  }
}

Service

package com.bennyrhys.com.shop.service;

import com.bennyrhys.com.shop.bean.Student;
import com.bennyrhys.com.shop.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentService {

  @Autowired
  StudentMapper studentMapper;

  public Student getStudentById(Integer id) {
    return studentMapper.getStudentById(id);
  }
}

Mapper接口

package com.bennyrhys.com.shop.mapper;

import com.bennyrhys.com.shop.bean.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;


@Mapper
@Repository
public interface StudentMapper {
  @Select("select * from student where id = #{id}")
  Student getStudentById(Integer id);
}

到此這篇關(guān)于SpringBoot Service和Dao的編寫詳解的文章就介紹到這了,更多相關(guān)SpringBoot Service和Dao編寫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Javafx簡單實現(xiàn)【我的電腦資源管理器】效果

    Javafx簡單實現(xiàn)【我的電腦資源管理器】效果

    這篇文章主要介紹了Javafx簡單實現(xiàn)【我的電腦資源管理器】效果,涉及Javafx操作系統(tǒng)文件模擬資源管理器的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • spring整合cxf框架實例

    spring整合cxf框架實例

    下面小編就為大家?guī)硪黄猻pring整合cxf框架實例。小編覺得挺不錯的?,F(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 簡單驗證碼生成Java版

    簡單驗證碼生成Java版

    這篇文章主要為大家詳細介紹了簡單驗證碼生成Java版,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • myeclipse開發(fā)servlet_動力節(jié)點Java學(xué)院整理

    myeclipse開發(fā)servlet_動力節(jié)點Java學(xué)院整理

    MyEclipse,是在eclipse基礎(chǔ)上加上自己的插件開發(fā)而成的功能強大的企業(yè)級集成開發(fā)環(huán)境,主要用于Java、Java EE以及移動應(yīng)用的開發(fā)。下面這篇文章主要給大家介紹了關(guān)于myeclipse開發(fā)servlet的相關(guān)資料,需要的朋友可以參考下。
    2017-07-07
  • Java利用MultipartFile實現(xiàn)上傳多份文件的代碼

    Java利用MultipartFile實現(xiàn)上傳多份文件的代碼

    這篇文章主要介紹了Java利用MultipartFile實現(xiàn)上傳多份文件的代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • 如何動態(tài)替換Spring容器中的Bean

    如何動態(tài)替換Spring容器中的Bean

    這篇文章主要介紹了如何動態(tài)替換Spring容器中的Bean,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java實現(xiàn)拓?fù)渑判蛩惴ǖ氖纠a

    Java實現(xiàn)拓?fù)渑判蛩惴ǖ氖纠a

    在圖論中,拓?fù)渑判颍═opological Sorting)是一個有向無環(huán)圖(DAG, Directed Acyclic Graph)的所有頂點的線性序列。本文將為大家講講拓?fù)渑判蛩惴ǖ脑砑皩崿F(xiàn),需要的可以參考一下
    2022-07-07
  • Spring?MVC各種參數(shù)進行封裝的方法實例

    Spring?MVC各種參數(shù)進行封裝的方法實例

    這篇文章主要給大家介紹了關(guān)于Spring?MVC各種參數(shù)進行封裝的相關(guān)資料,SpringMVC內(nèi)置多種數(shù)據(jù)類型轉(zhuǎn)換器,可以根據(jù)請求中的參數(shù)與后端控制器方法的參數(shù)的關(guān)系為我們實現(xiàn)簡單的數(shù)據(jù)封裝,需要的朋友可以參考下
    2023-06-06
  • Spring如何解決循環(huán)依賴的問題

    Spring如何解決循環(huán)依賴的問題

    這篇文章主要介紹了Spring是如何解決循環(huán)依賴的問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • idea打開和讀取*properties文件亂碼的解決

    idea打開和讀取*properties文件亂碼的解決

    本文主要介紹了idea打開和讀取*properties文件亂碼的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09

最新評論