溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Service與Dao如何在SpringBoot中使用

發(fā)布時間:2020-11-23 15:44:49 來源:億速云 閱讀:273 作者:Leah 欄目:開發(fā)技術(shù)

Service與Dao如何在SpringBoot中使用 ?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

效果圖

Service與Dao如何在SpringBoot中使用

配置環(huán)境

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

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

Service與Dao如何在SpringBoot中使用

建表

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;

Service與Dao如何在SpringBoot中使用

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&#63;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);
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI