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("服务器错误"); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix