java 自定义注解的应用

  1. 需求描述:通过实体类动态构建查询的sql语句
  2. IDEA新建springboot,引入web模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yf.cn</groupId>
    <artifactId>spring-aop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-aop</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  1. 在主包com.yf.cn下新建anno包,并新建Entity注解
    3.1Entity
package com.yf.cn.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @Target 定义注解的作用域,类型为数组可以包含多个
 * TYPE:作用域类
 * FIELD:作用域属性
 * METHOD:作用域方法
 *
 */
@Target({ElementType.TYPE,ElementType.METHOD})
/**
 * @Retention 被该注解注释的内容保留多久
 * source:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;被编译器忽略
 *
 * class:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期
 *
 * runtime:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface Entity {
    public String value() default "";
}


  1. 在主包com.yf.cn下新建pojo包,并新建Student和City实体类,并添加注解定义表名
    4.1Student:
package com.yf.cn.pojo;

import com.yf.cn.anno.Entity;

@Entity("st_student")
public class Student {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

4.2City:
package com.yf.cn.pojo;

import com.yf.cn.anno.Entity;

@Entity("sys_city")
public class City {
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

  1. 在主包com.yf.cn下新建util包,并创建QueryUtil类,定义创建构建sql语句的方法
    5.1QueryUtil:
package com.yf.cn.util;

import com.yf.cn.anno.Entity;
import com.yf.cn.pojo.Student;

public class QueryUtil {

    private static String sqlStr = "select * from ";

    public static String getQuerySql(Object obj){
       Class clazz =  obj.getClass();

        String entityName = "";
       //判断是否添加了指定注解@Entity
        System.out.println(clazz.isAnnotationPresent(Entity.class));
        if(clazz.isAnnotationPresent(Entity.class)){
            Entity entity = (Entity) clazz.getAnnotation(Entity.class);
            entityName = entity.value();
            System.out.println(entityName);
        }
        return sqlStr+entityName;
    }

}

  1. 在主包com.yf.cn下新建test包并创建TestQuery测试类
package com.yf.cn.test;

import com.yf.cn.app.Appconfig;
import com.yf.cn.pojo.City;
import com.yf.cn.pojo.Student;
import com.yf.cn.repository.IndexRepository;
import com.yf.cn.util.QueryUtil;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestQuery {
    public static void main(String[] args) {
        Student student = new Student();
        City city = new City();
        System.out.println(QueryUtil.getQuerySql(city));
    }
}

执行如上测试方法;传City对象时,输出的sql为:
在这里插入图片描述
传Student对象时,输出的sql为:

在这里插入图片描述
至此完成了java自定义的注解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值