SpringBoot Service和Dao的编写详解

网友投稿 748 2023-03-09

SpringBoot Service和Dao的编写详解

SpringBoot Service和Dao的编写详解

本文主要介绍了SpringBoot Service和Dao的编写详解,分享给大家,具体如下:

效果图

配置环境

创建数据

数据库中文编码

建表

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

org.mybatis.spring.boot

mybatis-spring-boot-starter</artifactId>

2.1.1

mysql

mysql-connector-java

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);

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于binarywang封装的微信工具包生成二维码
下一篇:微信小程序开发一个多少钱(微信小程序怎么制作自己的程序)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~