Springboot 自定义标签(插拔即用)保姆级别教程

Springboot 自定义标签(插拔即用)保姆级别教程

先上图片 原来的系统是这样的

@SpringBootApplication   // 启动类注解
@MapperScan(basePackages = "com.example.*.dao")  // 扫描包注解
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

运行的结果是这样的。
在这里插入图片描述

这是一个标准的 Springboot启动类,我想要自定义标签在注解上 那么实现的方法如下:

先定义一个test1类 用于做标签 :

import com.example.demo.dao.test1Demo;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({test1Demo.class})
public @interface test1 {
    String fahuizhi() default ""; // 如果不需要可以不写这个 具体用法后面会讲解
}

这里要注意下 @interface 和 interface 是有区别的 ,如果想用上面的@Target,@Retention,@Documented,@Import 这几个标签 那么就要用@interface声明接口

interface 关键字用于定义一个纯粹的接口。它是一个完全抽象的类,不能被实例化,只能被其他类实现(或称为"实现接口")。它只包含抽象的方法和常量。

@interface 关键字用于定义一个注解(Annotation)。注解是一种特殊类型的接口,用于为程序元素(如类、方法、成员变量等)提供元数据信息。注解本身不会被实例化或实现,而是用于提供附加信息或指示给编译器或其他工具。

简单的来说 interface用于定义纯粹的接口,而@interface用于定义注解。注解是提供元数据信息的特殊类型的接口,主要用于指示或提供额外信息给编译器或其他工具,而不是被实例化或实现。

贴出test1Demo代码

import org.springframework.context.annotation.Bean;

public class test1Demo {

    @Bean
    public String ceshi(){
        System.out.println("自定义");
        return "";
    }
}

好了 准备工作做好了, 那么就开启自定义标签看看效果!!!

@test1
@SpringBootApplication
@MapperScan(basePackages = "com.example.*.dao")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);

	}

这里面多了一个 @test1 的标签,没错 这个就是你刚刚配置的, 那么效果是什么样呢?

在这里插入图片描述
自定义标签已经实现效果了, 在启动过程中输出了自定义三个字

类和方法的使用如下:

package com.example.demo.dao;
import com.example.demo.service.test1;
public class test2 {

    @test1(fahuizhi = "测试值")
    void testMethod(String parameter) {
        // 方法定义
    }
}
package com.example.demo.dao;
import com.example.demo.service.test1;
@test1(fahuizhi = "测试值")
public class test2 {
    //类定义
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot是一个非常流行的Java Web开发框架,而MyBatis则是Java开发中常用的ORM框架。在实际项目中,我们常常需要将两者结合起来使用。本文将为大家提供一个保姆教程,详细讲解SpringBoot如何集成MyBatis。 一、引入相关依赖 在SpringBoot中集成MyBatis,需要在pom.xml文件中引入相关依赖。具体的依赖如下: <!-- MyBatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <!-- 数据库驱动依赖 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency> 二、配置数据源 在集成MyBatis之前,需要先配置数据源。可以在application.yml文件中添加如下配置: # 数据库配置 spring: datasource: url: jdbc:h2:file:./data/test;DB_CLOSE_ON_EXIT=FALSE username: sa password: driver-class-name: org.h2.Driver 这里使用了h2数据库,并指定了特定的路径和用户名、密码。 三、配置Mapper 接下来需要配置Mapper。在创建Mapper之前,需要先创建Java实体类。例如,可以创建一个User实体类: public class User { private Integer id; private String name; private Integer age; /* 省略setter和getter方法 */ } 然后,可以创建对应的Mapper: @Mapper public interface UserMapper { @Select("select * from user where id = #{id}") User selectUserById(Integer id); @Insert("insert into user(name, age) values(#{name}, #{age})") int insertUser(User user); @Delete("delete from user where id = #{id}") int deleteUser(Integer id); @Update("update user set name = #{name}, age = #{age} where id = #{id}") int updateUser(User user); } 在这里我们使用了注解方式对Mapper进行配置。 四、配置MyBatis 在使用MyBatis之前,需要先进行配置。在application.yml文件中添加如下配置: # MyBatis配置 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.example.springbootmybatis.entity 这里指定了Mapper的XML文件位置和Java实体类所在的包路径。 五、在Controller中使用Mapper 最后,可以在Controller中使用Mapper。例如: @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/user/{id}") public User selectUserById(@PathVariable("id") Integer id) { return userMapper.selectUserById(id); } @PostMapping("/user") public int insertUser(@RequestBody User user) { return userMapper.insertUser(user); } @DeleteMapping("/user/{id}") public int deleteUserById(@PathVariable("id") Integer id) { return userMapper.deleteUser(id); } @PutMapping("/user") public int updateUser(@RequestBody User user){ return userMapper.updateUser(user); } } 这里@Autowired注解注入了UserMapper,然后我们可以在方法中调用UserMapper的方法。 通过以上步骤,我们就成功地将MyBatis集成进了SpringBoot。在实际项目中,可以根据需要修改和扩展上述内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值