首页 > 其他分享 >springboot学习:建立springboot项目及相关注意事项

springboot学习:建立springboot项目及相关注意事项

时间:2024-01-27 11:11:40浏览次数:24  
标签:mapper return springboot boot id 学习 Result 注意事项 public

一、建立maven项目后引入依赖:
以下没有版本号的依赖表示在springboot父依赖中已锁定相应的版本号

必需依赖:

1.springboot父依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.5</version>
</parent>

2.web依赖启动器

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

3.mysql驱动

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

4.mybatis的启动器

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.3.0</version>
    </dependency>

5.junit依赖启动器

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

非必需依赖:

1.lombok

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

通过在类上添加注释的方式为其建立get,set方法,构造器等,以及开启构建者模式
@Data
@NoArgsConstructor //无参构造器
@AllArgsConstructor //全参构造器
@Builder //构建者设计模式
2.解决配置文件绑定到bean上的提示问题

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

二、建立项目结构
1.建立如下图所示的项目结构与相应的引导类(StudentService为个人项目业务端接口,可忽略)
项目结构:

引导类(类名通常为你的项目名加上Application):

@SpringBootApplication
@MapperScan(basePackages = "(你的组织名所对应的包).boot.mapper")
public class SpringbootmybatisdemoApplication {//所有的java代码都要放在这个类所在的包下面(将包下所有的组件类加入springboot管理),它取代了配置类的组件扫描功能,test类也要在相对应的位置
    public static void main(String[] args) {
        SpringApplication.run(SpringbootmybatisdemoApplication.class,args);
    }
}

domain包存放数据库表所对应的实体类,mapper包存放持久层mapper接口,service包存放业务层接口及实现类,entity包存放统一返回类型的实体类,utils包存放工具类,web包存放控制器与统一异常处理类。
2.在引导类的包相对应的测试包在加入测试代码,比如加入mapper接口测试类:

3.在你的java包资源文件中加入static(前端资源(html,css,javascript,音频图片等)),mapper包为mapper接口映射文件,application.yml是配置文件(取代ssm中的配置类)

配置文件的编写

#tomcat配置(默认8080,非必需)
server:
  port: 80


#数据库驱动配置(必需)
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: 2474600586liu


#日志配置(公司项目中必需)
logging:
  level:
    组织包名: debug


#mybatis配置(必需)
mybatis:
  configuration:
    map-underscore-to-camel-case: true       #开启驼峰映射
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl    #开启数据库操作日志
  type-aliases-package: 组织包名.boot.domain.Student      #开别名(效果为可以将映射文件中Student的全类名替换为Student,可以选择不要这行)
  mapper-locations: classpath:mapper/*StudentMapper.xml   #映射文件路径

三、注意事项
1.在持久层mapper接口上添加,可以防止出现引用代理对象爆红

@Mapper
@Repository

2.在业务层实现类上添加@Transactional,在只有读取数据库操作的方法上添加@Transactional(readOnly = true)可以提高效率

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    @Transactional(readOnly = true)
    public Result findAll() {
        List<Student> all = studentMapper.findAll();
        return Result.success(all);
    }

    @Override
    public Result insert(Student student) {
        int inert = studentMapper.inert(student);
        if(inert>0){
            return Result.success();
        }
        else{
            return Result.error("添加失败");
        }
    }
}

3.业务层尽量restful的代码风格,即相应的请求类型处理相应的操作,比如get请求对应查询,post请求对应插入,put请求对应修改,delete请求对应删除

@RestController
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result findAll(){
        Result all = studentService.findAll();
        log.info("findall");
        return all;
    }
    @PostMapping
    public Result insert(@RequestBody Student student){
        log.info("insert");
        return studentService.insert(student);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(name = "id") Long id){
        log.info("delete");
        return studentService.delete(id);
    }
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("update");
        return studentService.update(student);
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable(name = "id") Long id){
        log.info("findbyid");
        return studentService.findById(id);
    }
}

4.在static粘贴引入前端时,需要使用maven的清理命令(clean)清理项目的target目录和通过构建过程生成的其他临时文件,否则可能会报错

标签:mapper,return,springboot,boot,id,学习,Result,注意事项,public
From: https://www.cnblogs.com/swliujavajourney/p/17990781

相关文章

  • [office] Excel转dbf技巧及其注意事项概述
    1.DBF文件只会保存工作表中命名区域或当前区域中的数据:当以dBASE(DB2、DB3或DB4)格式保存Excel工作表、且该工作表中包含一个名为“Database”的区域时,只有命名区域中的数据会保存到dBASE文件中。如果区域命名之后又添加了新记录,则必须重新定义包括新记录的“Database”区域后,才能......
  • 【重要】部署系统的注意事项实践指南
    在部署系统时,有几个重要的注意事项需要考虑:确定部署需求和目标:在开始部署之前,明确系统的需求和目标非常重要。仔细分析所需的功能和性能,并确保系统能满足这些要求。进行系统测试:在正式部署系统之前,进行全面的系统测试是必不可少的。确保系统在各种场景下的正常运行,并修复任何出部署......
  • SpringBoot启动项目报错:java.lang.UnsatisfiedLinkError: D:\files\software\jdk-1
    目录问题描述解决方法:问题描述在运行向的时候出现报错:java.lang.UnsatisfiedLinkError:D:\files\software\jdk-15.0.1\jdk-17.0.3.1\bin\tcnative-1.dll:Can'tloadIA32-bit.dllonaAMD64-bitplatform atjava.base/jdk.internal.loader.NativeLibraries.load(Native......
  • SpringBoot简易教程
     SpringBoot简易教程(01):SpringBoot基础入门SpringBoot简易教程(02):SpringBoot配置文件详解SpringBoot简易教程(03):SpringBoot整合ssmSpringBoot简易教程(04):SpringBoot单元测试SpringBoot简易教程(05):SpringBoot开发RestfulAPI及使用jmeter测试SpringBoot简易教程(06):swagger测试Rest......
  • SpringBoot中使用LocalDateTime踩坑记录
    目录前言近日心血来潮想做一个开源项目,目标是做一款可以适配多端、功能完备的模板工程,包含后台管理系统和前台系统,开发者基于此项目进行裁剪和扩展来完成自己的功能开发。本项目基于Java21和SpringBoot3开发,序列化工具使用的是默认的Jackson,使用SpringDataRedis操作Redis缓......
  • 数位 dp 学习笔记(灵神模板)
      我谔谔,数位dp几年了还不会,学的都是些奇奇怪怪的写法,导致每次比赛遇到数位dp的题要么不会,要么写半天。灵神的数位dp模板其实很早就有看过,不过当时不是很理解递归的含义于是放弃了,最近重新来回来看发现还是递归好写,不仅简短而且基本都是一个框架,这就可以大大减少思考量,基......
  • Kubernetes 推荐学习资料 课程 视频
    以下是一些推荐的Kubernetes学习资料、课程和视频:学习资料:Kubernetes官方文档:https://kubernetes.io/docs/home/《Kubernetes操作指南》(KubernetesUp&Running)一书,由KelseyHightower、BrendanBurns、JoeBeda著。《KubernetesinAction》一书,由MarkoLuksa著。《Kuberne......
  • Vim学习
    至今还不理解是怎么发明出Vim这种东西的,首先就是经常使用windows的话就会觉Vim简直就是反人类的操作,但是它到现在还没有被淘汰,甚至于一直被应用在Linux或者Mac系统上,可见还是有它的独到之处,所以写一些随笔来记录Vim的一些常用命令,因为根本就是记不住,太多了,可能等我熟练用用以后会......
  • 【THM】Intro to Malware Analysis(恶意软件分析简介)-学习
    本文相关的TryHackMe实验房间链接:https://tryhackme.com/room/intromalwareanalysis本文相关内容:简单介绍一下遇到可疑的恶意软件应该怎么处理。简介当我们在担任SOC(安全运营中心)分析师时,有时会遇到一些可疑的内容(文件或流量),并且我们必须确定这些可疑内容是否为恶意的,为此......
  • 如何学习算法:什么时完全二叉树?完全二叉树有什么特点?
    完全二叉树我们知道树是一种非线性数据结构。它对儿童数量没有限制。二叉树有一个限制,因为树的任何节点最多有两个子节点:左子节点和右子节点。什么是完全二叉树?完全二叉树是一种特殊类型的二叉树,其中树的所有级别都被完全填充,除了最低级别的节点从尽可能左侧填充之外。完全二叉树的......