当前位置: 首页 > news >正文

广元网站建设优化/seo优化要做什么

广元网站建设优化,seo优化要做什么,有网站做淘宝客,网站建设作用为什么80%的码农都做不了架构师?>>> 最近做项目中有接触到反射的使用简单的做了一个注释 首先的想法是根据类中不为空的值生成sql 首先是三个注解 主键注解 package comments;import java.lang.annotation.Documented; import java.lang.annotation.Ele…

为什么80%的码农都做不了架构师?>>>   hot3.png

最近做项目中有接触到反射的使用简单的做了一个注释

首先的想法是根据类中不为空的值生成sql

 

首先是三个注解

主键注解

package comments;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;/*** 主键* @author  Administrator*/
@Target(ElementType.FIELD)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
@Inherited 
public @interface  Key {
}
package comments;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;/*** 如果不和数据关联则设置此注解* @author  Administrator**/
@Target(ElementType.FIELD)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
@Inherited 
public @interface  notRecord {
}
package comments;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;/*** 设置表名* @author  Administrator**/
@Target(ElementType.TYPE)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
@Inherited 
public @interface  Table {public String name();
}

然后是自定义异常类

package org;/*** 设置自定义异常* @author  Administrator**/
public class NumException extends Exception {private String name;public NumException(String name){this.name=name;}public String toString(){return name;}
}

实体类

package org;import comments.Key;
import comments.Table;
import comments.notRecord;@Table(name = "student")
public class Student {@Key private String id;private String name;@notRecordprivate String sex;private int age;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;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

处理实体类生成sql的类。

package org;import java.lang.reflect.Field;import comments.Key;
import comments.Table;
import comments.notRecord;public class Processing {/*** 通过实体类生成 insert into sql语句* @param cl* @return * @throws IllegalArgumentException* @throws IllegalAccessException* @throws NumException*/public String save(Object cl) throws IllegalArgumentException, IllegalAccessException, NumException{String sql="insert into ";if(cl!=null){Field[] fiels=cl.getClass().getDeclaredFields();//获得反射对象集合boolean t=cl.getClass().isAnnotationPresent(Table.class);//获得类是否有注解if(t){Table tab=cl.getClass().getAnnotation(Table.class);sql+=tab.name();//获得表名String name ="";//记录字段名String value ="";//记录值名称boolean bl=false;//记录主键是否为空for(Field fl:fiels){//循环组装fl.setAccessible(true);//开启支私有变量的访问权限Object tobj=fl.get(cl);if(tobj!=null){if(fl.isAnnotationPresent(Key.class)){//判断是否存在主键bl=true;}if(!fl.isAnnotationPresent(notRecord.class)){name+=fl.getName()+",";value+="'"+tobj.toString()+"',";}}}if(bl){if(name.length()>0)name=name.substring(0,name.length()-1);if(value.length()>0)value=value.substring(0,value.length()-1);sql+="("+name+") values("+value+")";}elsethrow new NumException("未找到类主键 主键不能为空");}elsethrow new NumException("传入对象不是实体类");}elsethrow new NumException("传入对象不能为空");//抛出异常return sql;}/*** 传入对象更新* @param obj* @return * @throws IllegalArgumentException* @throws IllegalAccessException* @throws NumException*/public String update(Object obj) throws IllegalArgumentException, IllegalAccessException, NumException{String sql="update ";if(obj!=null){Field[] fiels=obj.getClass().getDeclaredFields();//获得反射对象集合boolean t=obj.getClass().isAnnotationPresent(Table.class);//获得类是否有注解if(t){Table tab=obj.getClass().getAnnotation(Table.class);sql+=tab.name()+" set ";//获得表名String wh ="";//记录字段名String k="";boolean bl=false;//记录主键是否为空for(Field fl:fiels){//循环组装fl.setAccessible(true);//开启支私有变量的访问权限Object tobj=fl.get(obj);if(tobj!=null){if(fl.isAnnotationPresent(Key.class)){//判断是否存在主键bl=true;k=fl.getName()+"='"+tobj.toString()+"' where  ";}else{if(!fl.isAnnotationPresent(notRecord.class)){wh+=fl.getName()+"='"+tobj.toString()+"',";}}}}if(bl){if(wh.length()>0)wh=wh.substring(0,wh.length()-1);if(k.length()>0)k=k.substring(0,k.length()-1);sql+=k+wh;}elsethrow new NumException("未找到类主键 主键不能为空");}elsethrow new NumException("传入对象不是实体类");}elsethrow new NumException("传入对象不能为空");//抛出异常return sql;}
}

最后是测试类

package org;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import comments.Table;
import comments.Key;public class temp {public static void main(String[] aa) throws IllegalArgumentException, IllegalAccessException, NumException{Student stu=new Student();stu.setId("ccc");stu.setName("姓名");stu.setAge(18);stu.setSex("男");//stu=null;System.out.println(new Processing().save(stu));System.out.println(new Processing().update(stu));}
}

这里如果要套用着DAO用就好了。我只是简单的实现一下。欢迎拍砖。

输出结果

insert into student(id,name,age) values('ccc','姓名','18')
update student set id='ccc' where name='姓名',age='18'

 

转载于:https://my.oschina.net/mifans/blog/756084

http://www.jmfq.cn/news/5004199.html

相关文章:

  • 食品企业网站建设策划方案书/品牌营销推广要怎么做
  • 怎样做网站的关键词/seo网站平台
  • 自己做的网站图片打开慢/网络营销学院
  • 网站开发 大学专业/营销型网站建设
  • 动态网页用什么语言编写/北京优化网站推广
  • 企业建立网站的目的/百度指数搜索热度排行
  • 西充移动网站建设/网页设计制作网站html代码大全
  • 网络推广网站/网站优化方式有哪些
  • php网站开发百度百科/优化 英语
  • 如何快速制作一个网站/长沙谷歌seo收费
  • 枣庄网站制作公司/seo com
  • 帮老板做网站/网络营销的营销策略
  • 医院网站建设招标/哪些网站可以免费推广
  • 用动物做网站名称/seo的内容有哪些
  • 如何给网站做第三方流量监测/厦门关键词排名推广
  • 平面设计师常用的素材网站/合肥网站seo整站优化
  • 交互型网站难做吗/学电脑培训班多少一个月
  • 做企业网站怎么收费的/日本今日新闻头条
  • 江苏润通市政建设工程有限公司网站/新乡网站seo
  • 工信部icp备案是什么意思/windows优化大师是电脑自带的吗
  • 做网站的公司高创/域名解析ip地址
  • 服务器的做网站空间/西安网站到首页排名
  • 旅游网站建设外现状/怎么打开网站
  • 伏羲方舟网站建设/好搜网
  • 做网站公司会场主持台词/网站seo置顶 乐云践新专家
  • 资源专业网站优化排名/百度知识营销
  • 不忘初心网站建设/产品推广策略
  • 通灵人预言2023年疫情/seo网站推广有哪些
  • 去哪个网站找做贷款的靠谱/网页设计模板素材图片
  • 聊城做网站lcbywl/产品软文代写