SpringBoot简单快速入门操作
项目类分为: dao层 server层 controller层 Mapper → Server→ controller
mapper层(必须要用interface创建)
创建后,首先要在方法前加@Mapper 标明为Mapper类 告知Springboot,之后Springboot就能识别此类;
其次Mapper层主要写实现方法,可以理解为底层实现代码; 因此不需要@Autowired这种“自动引进”;
类似于:
@Select
@Insert
@Delete
@Update
这些替代了繁琐的JDBC代码,如果想实现增删改查的功能,直接在方法上添加如上参数,编写sql语句即可;
和preparestatement不同的是,替代?功能的是#{id}
其他:


Mapper层:
package com.example.demo.co; import java.util.List; import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AliasFor; @Mapper //告知SpringBoot这是mapper层
public interface DeptMapper {
@Select("select * from dept")
List<Dept>selectALL(); @Insert("insert into dept (id,name,location)values(#{id},#{name},#{location})")
void insert(String string,String name,String location); @Select("select * from dept where id= #{id}") Dept userSelect(int id); // @Update("update `dept` set id ,name,location values(#{id},#{name},#{location})")
//
// Dept updateFromDept(int id,String name,String location); @Delete("delete from dept where id = #{id}") void deleteFromdept(int id);
}
Servce层:
创建后,首先要在方法前加@Service 标明为Service类 告知Springboot,之后Springboot就能识别此类;
@AutoWired 其实在启动spring IoC时,容器自动装载了一个AutowiredAnnotationBeanPostProcessor后置处理器,当容器扫描到@Autowied、@Resource或@Inject时,就会在IoC容器自动查找需要的bean,并装配给该对象的属性
注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。
1.interfaceService层接口
package com.example.demo;
import java.util.List; import org.springframework.stereotype.Service; import com.example.demo.gs.Move; @Service
public interface MoveService {
List<Move>selectFromService();
}
2.interface-Service-implement实现类
package com.example.demo.gs; import java.util.List; import javax.management.loading.PrivateClassLoader; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.example.demo.MoveMapper;
import com.example.demo.MoveService; @Service
public class MoveServiceImp implements MoveService {
@Autowired //注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 一般在contorller层使用
private MoveMapper mapper; //直接定义一个变量名就可以了; @Override
public List<Move> selectFromService() { return this.mapper.selectFromMes(); //this 直接引用作用域之外的变量
} }
Contorller层:
创建后,首先要在方法前加@RestController 标明为Controller类 告知Springboot,之后Springboot就能识别此类;
@GetMapping("/select") servlet(“/select”)用法一样;
package com.example.demo.co; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class DeptController {
@Autowired
private DeptService service;
@GetMapping("/select")
List<Dept>selectSerlet(){
return this.service.selectFromService(); }
@GetMapping("/delete")
void deleteFromDept(int id) {
this.service.deletefromService(id); }
}
其他:
package com.example.demo; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserController {
@Autowired
private UserService service; @GetMapping("/hello")
public String hello() {
return "Hello World";
} @GetMapping("/users")
public List<User> list() {
return this.service.list();
} @GetMapping("/users/load")
public User load1(@RequestParam("un") String username) {
return this.service.load(username);
} @GetMapping("/users/{username}.html")
public User load2(@PathVariable String username) {
return this.service.load(username);
} @PostMapping("/users")
public void save1(User user) { // 自动接收application/x-www-form-urlencoded格式的参数
System.out.println(user);
} @PostMapping("/users/json")
public ResponseEntity<String> save2(@RequestBody User user) { // @RequestBody的作用是告诉Spring: 请求体是JSON格式,请帮我反序列化成user对象
System.out.println(user);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器错误");
} }
SpringBoot简单快速入门操作的更多相关文章
- 【原创】SpringBoot & SpringCloud 快速入门学习笔记(完整示例)
[原创]SpringBoot & SpringCloud 快速入门学习笔记(完整示例) 1月前在系统的学习SpringBoot和SpringCloud,同时整理了快速入门示例,方便能针对每个知 ...
- SpringBoot开发快速入门
SpringBoot开发快速入门 目录 一.Spring Boot 入门 1.Spring Boot 简介 2.微服务 3.环境准备 1.maven设置: 2.IDEA设置 4.Spring Boot ...
- 01.JDBC操作数据库-快速入门操作
/** * 简单入门操作 * 注:先将mysql-connector-java-5.1.36.jar 构建 Build Path环境当中去 * @param args * @throws Except ...
- SpringBoot介绍,快速入门小例子,目录结构,不同的启动方式,SpringBoot常用注解
SpringBoot介绍 引言 为了使用ssm框架去开发,准备ssm框架的模板配置 为了Spring整合第三方框架,单独的去编写xml文件 导致ssm项目后期xml文件特别多,维护xml文件的成本也是 ...
- 【SpringBoot】快速入门
博客主页:准Java全栈开发工程师 00年出生,即将进入职场闯荡,目标赚钱,可能会有人觉得我格局小.觉得俗,但不得不承认这个世界已经不再是以一条线来分割的平面,而是围绕财富旋转的球面,成为有钱人不是为 ...
- Shiro权限框架简单快速入门
声明本文只适合初学者,本人也是刚接触而已,经过一段时间的研究小有收获,特来分享下希望和大家互相交流学习. 首先配置我们的web.xml代码如下: <filter> <filter-n ...
- JQuery快速入门-操作元素的属性和样式
我们在学习JavaScript时,详细介绍了DOM对象.从DOM树可以得知,对DOM的操作,主要包括:元素的属性.内容.值.CSS. 一.元素属性的操作 在 jQuery 中,可以对元素的属性执行获取 ...
- SpringBoot的快速入门
快速创建一个SpringBoot项目(两种方式:STS版本,IntelliJ IDEA) 1.STS方式:什么是STS?是Spring团队推荐使用的开发工具 所谓的sts就是eclipse升级版 继承 ...
- php-laravel4.0框架 简单快速入门
前提必须已经安装好了laravel4.0版本. 写入权限: 安装完 Laravel ,你还需要为web服务器设置 app/storage 目录的写入权限. 目录结构: 安装完框架后,你需要熟悉一下该项 ...
随机推荐
- 项目导入 Vue Router 4 依赖包流程
下载 Vue Router 4 的依赖包: npm install vue-router@4 新建 router.ts 文件,导入 createRouter 以及 createWebHashHisto ...
- GoogleTest环境配置以及应用
1 GoogleTest源码编译: GoogleTest代码仓库URL: https://github.com/google/googletest.git 下载源代码: git clone --bra ...
- 《吐血整理》进阶系列教程-拿捏Fiddler抓包教程(14)-Fiddler断点(breakpoints)实战,篡改或伪造数据
1.简介 上一篇主要就讲解和分享Fiddler断点的理论和操作,今天宏哥就用具体例子,将上一篇中的理论知识实践一下.而且在实际测试过程中,有时候需要修改请求或响应数据,或者直接模拟服务器响应,此时可以 ...
- Spring源码-Bean生命周期总览
- 算法模板:C++的高精度
代码是抄别人的:https://blog.csdn.net/code4101/article/details/38705155. 这篇博客只是用来查看保存,非原创. #include<iostr ...
- XYX错误集
(频数递减) # 数据范围:没开Long Long (*inf^2) # while 打成了 if ,if 打成了 while(*inf^2) # 换根DP:两个dfs调用错误 (*inf) # ZK ...
- HDU4372 Count the Buildings (+题解:斯特林数)
题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...
- .NET 纯原生实现 Cron 定时任务执行,未依赖第三方组件 (Timer 优化版)
在上个月写过一篇 .NET 纯原生实现 Cron 定时任务执行,未依赖第三方组件 的文章,当时 CronSchedule 的实现是使用了,每个服务都独立进入到一个 while 循环中,进行定期扫描是否 ...
- 大家都能看得懂的源码之 ahooks useVirtualList 封装虚拟滚动列表
本文是深入浅出 ahooks 源码系列文章的第十八篇,该系列已整理成文档-地址.觉得还不错,给个 star 支持一下哈,Thanks. 简介 提供虚拟化列表能力的 Hook,用于解决展示海量数据渲染时 ...
- Dapr 证书过期了怎么办? 别慌,有救!
一.背景 Dapr 默认证书有效时间是1年,证书过期后就不能执行相关控制面和数据面的交互了,如下图: 二.查看证书有效时间 通过dapr mtls expiry 看到期时间,具体参见命令https:/ ...