SpringBoot Service和Dao的編寫詳解
本文主要介紹了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)文章
spring Boot與Mybatis整合優(yōu)化詳解
關(guān)于spring-boot與mybatis整合優(yōu)化方面的介紹,就是Mybatis-Spring-boot-starter的介紹,具體內(nèi)容詳情大家參考下本文2017-07-07
優(yōu)化Java內(nèi)存管理來防止“GC”錯誤的方法詳解
垃圾回收(GC)是 Java 中的一個重要機(jī)制,它可以管理內(nèi)存并回收不再使用的對象所占用的資源,在本文中,我們將探討一些技巧,幫助您避免這一錯誤,確保您的 Java 應(yīng)用程序順利運行,需要的朋友可以參考下2023-11-11
SpringBoot多數(shù)據(jù)源配置方式以及報錯問題的解決
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置方式以及報錯問題的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring Security實現(xiàn)微信公眾號網(wǎng)頁授權(quán)功能
這篇文章主要介紹了Spring Security中實現(xiàn)微信網(wǎng)頁授權(quán),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類型)
這篇文章主要介紹了SpringBoot調(diào)第三方WebService接口的操作代碼(.wsdl與.asmx類型 ),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
springboot @RequiredArgsConstructor的概念與使用方式
這篇文章主要介紹了springboot @RequiredArgsConstructor的概念與使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
Java使用Arrays.asList報UnsupportedOperationException的解決
這篇文章主要介紹了Java使用Arrays.asList報UnsupportedOperationException的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

