Java注解项目实战即模拟Hibenernate生成sql语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java注解项目实战即模拟Hibenernate生成sql语句相关的知识,希望对你有一定的参考价值。
整理了近期学习java注解的部分代码 ,借助java注解模拟Hibenernate ORM模型建立对象与sql语句的映射
Table 注解的创建
package com.imooc.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.lang.model.element.Element; //用作类的注解,生命周期为运行时 一个值 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Table { String value(); }
字段 注解的创建
package com.imooc.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /* * 作用域为字段 运行时使用 * */ @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Column { String value(); }
用户表(类)
package com.imooc.test; @Table("user") public class Filter { @Column("id") private int id; @Column("user_name") private String userName; @Column("user_name") private String nickName; @Column("age") private int age; @Column("city") private String city; @Column("email") private String email; @Column("mobile") private String mobile; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } }
测试类的创建
package com.imooc.test; @Table("Coder ") public class Coder { @Column("id") private int id; @Column("name") private String userName; @Column("age") private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
sql语句的生成过程
package com.imooc.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Test { public static void main(String[] args) { // 代码如何与数据库做映射 Filter f1 = new Filter(); f1.setId(10); Filter f2 = new Filter(); f2.setUserName("lucy"); Filter f3 = new Filter(); f3.setEmail("[email protected],[email protected],[email protected]"); Coder c1 = new Coder(); c1.setUserName("ruanjianlin"); String sql1 = query(f1); String sql2 = query(f2); String sql3 = query(f3); String sql4 = query(c1); System.out.println(sql1); System.out.println(sql2); System.out.println(sql3); System.out.println(sql4); } private static String query(Object f) { // TODO Auto-generated method stub StringBuilder sb = new StringBuilder(); // 1.获取Class通过类的类类型得到类的详细信息 Class c = f.getClass(); // Java Package.isAnnotationPresent()方法用法实例教程。方法返回true,如果指定类型的注释存在于此元素上, // 否则返回false // 2.获取Table的名字 boolean exists = c.isAnnotationPresent(Table.class); if (!exists) { return null; } Table t = (Table) c.getAnnotation(Table.class); String tableName = t.value(); sb.append("select* from ").append(tableName).append(" where 1=1 "); // 3遍历所有字段 Field[] fArray = c.getDeclaredFields(); for (Field field : fArray) { // 4拿到每个字段对应的sql // 4.1字段名 boolean fExists = field.isAnnotationPresent(Column.class);// 是否存在 // 判断是否存在字段 if (!fExists) { continue; } Column column = field.getAnnotation(Column.class); String columnName = column.value(); Object fieldValue = null; String filedName = field.getName(); // 4.2拿到字段值(先获取字段的get方法) String getMethodName = "get" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1); try { Method getMethod = c.getMethod(getMethodName);// 取得该值 // 反射渠道得到字段值 fieldValue = getMethod.invoke(f); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (fieldValue == null || (fieldValue instanceof Integer && (Integer) fieldValue == 0)) { continue; } //4.3拼装sql sb.append("and ").append(columnName); if (fieldValue instanceof String) { if (((String) fieldValue).contains(",")) { String[] values = ((String) fieldValue).split(","); sb.append(" in("); for (String v : values) { sb.append("‘").append(v).append("‘,"); } sb.deleteCharAt(sb.length() - 1); sb.append(")"); } else { sb.append("= ‘").append(fieldValue).append("‘"); } } else { sb.append("=").append(fieldValue.toString()); } } return sb.toString(); } }
本文出自 “ruanjianlin” 博客,请务必保留此出处http://ruanjianlin.blog.51cto.com/11146685/1900584
以上是关于Java注解项目实战即模拟Hibenernate生成sql语句的主要内容,如果未能解决你的问题,请参考以下文章
Java SSM 项目实战 day08 方法级别的权限操作 服务器端的权限控制(JSR-250注解)(支持表达式的注解)(@Secured)以及页面端的权限控制
Java SSM 项目实战 day08 方法级别的权限操作 服务器端的权限控制(JSR-250注解)(支持表达式的注解)(@Secured)以及页面端的权限控制