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

<!--    connect-->
<!--    不同版本mybatis对应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);
}

到此这篇关于SpringBoot Service和Dao的编写详解的文章就介绍到这了,更多相关SpringBoot Service和Dao编写内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Springboot 使用 JSR 303 对 Controller 控制层校验及 Service 服务层 AOP 校验 使用消息资源文件对消息国际化

    导包和配置 导入 JSR 303 的包.hibernate valid 的包 <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.5.Final</version> </dependency> <dependency> <

  • SpringBoot构建Restful service完成Get和Post请求

    一个基本的RESTfule service最进场向外提供的请求Method就是Get和Post. 在Get中,常用的都会在请求上带上参数,或者是路径参数.响应Json. 在Post中,常用的会提交form data或者json data作为参数,响应Json. 1. Get请求,url传参,返回json. 先准备一个请求后,响应的对象. package com.example.demo; public class Echo { private final long id; private fin

  • SpringBoot使用CXF集成WebService的方法

    1.写在前面 WebService 对我来说既熟悉又陌生,已经将近六七年没有看到过他了, 具体的介绍我就不多少了, 想了解的百度百科下说的很详细. 之所以突然研究WebService是接到一个需求要去给 XX 项目做一个适配层,他们原有系统是使用webservice做的,所以-- 那我们就来看看,这一个古老的技术如何和如今最流行的框架SpringBoot进行结合. 2.SpringBoot 集成WebService 2.1 导入依赖 compile('org.springframework.bo

  • idea显示springboot多服务启动界面service操作

    如果是多模块的微服务,idea提供了一个可以多服务启动的界面services,如果你的项目里没看到这个界面: 那么你需要在顶级的maven工程中找到这个配置,然后找到 component name="RunDashboard"这个节点整个替换掉: <component name="RunDashboard"> <option name="configurationTypes"> <set> <option

  • SpringBoot集成shiro,MyRealm中无法@Autowired注入Service的问题

    网上说了很多诸如是Spring加载顺序,shiroFilter在Spring自动装配bean之前的问题,其实也有可能忽略如下低级错误. 在ShiroConfiguration中要使用@Bean在ApplicationContext注入MyRealm,不能直接new对象. 道理和Controller中调用Service一样,都要是SpringBean,不能自己new. 错误方式: @Bean(name = "securityManager") public SecurityManager

  • 详解SpringBoot 解决拦截器注入Service为空问题

    一.自定义拦截器实现 HandlerInterceptor 接口 /** * * Created by zhh on 2018/04/20. */ public class MyInterceptor implements HandlerInterceptor { @Autowired private NetworkProxyInfoService networkProxyInfoService; @Override public void afterCompletion(HttpServlet

  • springboot整合cxf发布webservice以及调用的方法

    webservice性能不高,但是现在好多公司还是在用,恰好今天在开发的时候对接项目组需要使用到webservice下面来说下简单的案例应用 首先老规矩:引入jar包 <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.11</version> &

  • 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 C

  • Springboot集成mybatis与jsp过程详解

    目录 什么是Spring Boot? springboot特点 springboot快速搭建项目 新建项目springboot_mybatis_jsp 项目配置 配置项目目录 配置工作目录(working directory) 配置pom.xml 配置application.properties 编写代码 建表t_user 编写User.java 编写UserMapper.xml 编写UserService.java.UserServiceImpl.java 编写Controller 什么是Sp

  • SpringBoot导入导出数据实现方法详解

    今天给大家带来的是一个 SpringBoot导入导出数据 首先我们先创建项目 注意:创建SpringBoot项目时一定要联网不然会报错 项目创建好后我们首先对 application.yml 进行编译 server:  port: 8081# mysqlspring:  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql://127.0.0.1:3306/dvd?characterEncodi

  • SpringBoot整合junit与Mybatis流程详解

    目录 SpringBoot整合junit 环境准备 编写测试类 SpringBoot整合mybatis 回顾Spring整合Mybatis SpringBoot整合mybatis 创建模块 定义实体类 定义dao接口 定义测试类 编写配置 测试 使用Druid数据源 SpringBoot整合junit 回顾 Spring 整合 junit @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringC

  • IntelliJ Idea SpringBoot 数据库增删改查实例详解

    SpringBoot 是 SpringMVC 的升级,对于编码.配置.部署和监控,更加简单 微服务 微服务是一个新兴的软件架构,就是把一个大型的单个应用程序和服务拆分为数十个的支持微服务.一个微服务的策略可以让工作变得更为简便,它可扩展单个组件而不是整个的应用程序堆栈,从而满足服务等级协议. Spring 为 微服务提供了一整套的组件-SpringClound , SpirngBoot 就是该基础. 第一个SpringBoot程序 这里使用的开发软件是IntelliJ Idea,和Eclipse

  • Mockito 结合 Springboot 进行应用测试的方法详解

    Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring Boot可以跟BDD(Behavier Driven Development)工具.Cucumber和Spock协同工作,对应用程序进行测试. 在web应用程序中,我们主要是对Service层做单元测试,以前单元测试都是使用 junit4 ,对Controller层做集成测试或者接口测试,对Controller层的测试一般有两种方法:(1)发送htt

  • SpringBoot实现整合微信支付方法详解

    目录 1.准备工作 1.1 数据库表 1.2 实体类 1.3 导入依赖 1.4 配置文件 1.5 创建读取微信支付相关信息的工具类 1.6 其他工具类 2.生成订单 2.1 远程调用用户模块和课程模块 2.2 远程调用方法的实现 2.3 根据课程id和用户id生成订单 3.查询订单信息 3.1 controller层 3.2 service层 4.生成微信支付的二维码 4.1 controller层 4.2 service层 5.查询订单支付状态 5.1 controller层 5.2 serv

  • Springboot整合Netty实现RPC服务器详解流程

    目录 一.什么是RPC? 二.实现RPC需要解决那些问题? 1. 约定通信协议格式 RPC请求 RPC响应 2. 序列化方式 3. TCP粘包.拆包 4. 网络通信框架的选择 三.RPC服务端 四.RPC客户端 总结 一.什么是RPC? RPC(Remote Procedure Call)远程过程调用,是一种进程间的通信方式,其可以做到像调用本地方法那样调用位于远程的计算机的服务.其实现的原理过程如下: 本地的进程通过接口进行本地方法调用. RPC客户端将调用的接口名.接口方法.方法参数等信息利

  • SpringBoot 使用Mybatis分页插件实现详解

    这篇文章主要介绍了SpringBoot 使用Mybatis分页插件实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导入分页插件包和jpa包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </depende

  • 使用SpringBoot整合ssm项目的实例详解

    SpringBoot是什么? Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程. Spring Boot 现在已经成为 Java 开发领域的一颗璀璨明珠,它本身是包容万象的,可以跟各种技术集成.成为 SpringBoot 全家桶,成为一把万能钥匙. SpringBoot的特点 1.创建独立的 Spring 应用程序 2.嵌入的 Tomcat ,无需部署 WAR 文件 3.简化 Maven 配置 4.自动配置 Spr

随机推荐