package com.shu.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Column {
String value();
}
package com.shu.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Table {
String value();
}
package com.shu.entity;
import com.shu.annotation.Column;
import com.shu.annotation.Table;
@Table("t_user")
public class User {
@Column("name")
private String name;
@Column("password")
private String password;
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.shu.dao;
import java.lang.reflect.Field;
import com.shu.annotation.Column;
import com.shu.annotation.Table;
public interface IBaseDao<T> {
//最终目标:insert into user (NAME,password) VALUES('cai','123456');
default String save(T entity) throws IllegalArgumentException, IllegalAccessException{
//sql用于存放最终返回的sql语句
StringBuilder sql = new StringBuilder("insert into ");
//tableName用于存放sql语句中表名部分
StringBuilder tableName;
//columnName用于存放sql语句的字段部分
StringBuilder columnName = new StringBuilder("(");
//values用于存放sql语句中的赋值部分
StringBuilder values = new StringBuilder("(");
//获取对象user的class对象
Class clazz = entity.getClass();
//判断该User类是否有@Table注解
boolean isTable = clazz.isAnnotationPresent(Table.class);
if(isTable) {
//获取User类@Table注解的值value,该值我们定义为User表的表名称
Table t = (Table) clazz.getAnnotation(Table.class);
tableName = new StringBuilder(t.value());
//拼接表名
sql.append(tableName+" ");
//获取user对象的属性列表
Field[] fieldList = clazz.getDeclaredFields();
//遍历属性列表,分别拿出属性列表中被@Column注解的属性,并获取属性的值
for(int i=0;i<fieldList.length;i++){
Field f = fieldList[i];
boolean isColumn = f.isAnnotationPresent(Column.class);
if(!isColumn){
continue;
}
Column column = f.getAnnotation(Column.class);
f.setAccessible(true);
Object columnValue = f.get(entity);
if(i==fieldList.length-1){
columnName.append(column.value()+") VALUES ");
values.append("'"+columnValue+"')");
sql.append(columnName);
sql.append(values);
continue;
}
columnName.append(column.value()+", ");
values.append("'"+columnValue+"',");
}
// boolean isColumn = clazz.isAnnotationPresent(annotationClass);
}
return sql.toString();
}
}
package com.shu.dao;
import com.shu.entity.User;
public class UserDao implements IBaseDao<User> {
}
package com.shu.service;
import com.shu.dao.UserDao;
import com.shu.entity.User;
public class UserService {
public static void main(String[] args) {
// TODO Auto-generated constructor stub
UserDao userDao = new UserDao();
User user = new User();
user.setName("cai");
user.setPassword("123456");
try {
System.out.println(userDao.save(user));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有