package imooc.test;
@Table("BHL_MANAGER_USER")
public class Filter {
@Column("USER_ID")
private Integer userId;
@Column("USER_NAME")
private String userName;
@Column("USER_PWD")
private String userPwd;
@Column("USER_REALNAME")
private String userRname;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserRname() {
return userRname;
}
public void setUserRname(String userRname) {
this.userRname = userRname;
}
}
===================
package imooc.test;
@Table("BHL_ROLE")
public class Filter2 {
@Column("ROLE_ID")
private Integer roleId;
@Column("ROLE_NAME")
private String roleName;
@Column("ORDERNO")
private String orderNo;
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
}
===================
package imooc.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
===================
package 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 imooc.test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//模拟hibernate的映射
public class Test {
public static void main(String[] args) {
Filter f1 = new Filter();
f1.setUserId(7);
f1.setUserName("test5");
Filter f2 = new Filter();
f2.setUserName("bhl_test");
Filter f3 = new Filter();
f3.setUserRname("bhl,test1,test2");
String sql1 = query(f1);
System.out.println(sql1);
executeQuery(sql1,1);
System.out.println();
String sql2 = query(f2);
System.out.println(sql2);
executeQuery(sql2,1);
System.out.println();
String sql3 = query(f3);
System.out.println(sql3);
executeQuery(sql3,1);
System.out.println();
Filter2 f4 = new Filter2();
f4.setRoleName("档案管理员");
String sq4 = query(f4);
System.out.println(sq4);
executeQuery(sq4,2);
}
private static void executeQuery(String sql,int type) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.9:1521:orcl", "bcloud", "bcloud");
PreparedStatement findPst = conn.prepareStatement(sql);
ResultSet rs = findPst.executeQuery();
while (rs.next()) {
if (type==1) {
int userId = rs.getInt(1);
String userName = rs.getString(2);
String userPwd = rs.getString(3);
String realName = rs.getString(4);
System.out.println("userId:"+userId+" userName:"+userName+" userPwd:"+userPwd+" realName:"+realName);
}else {
int roleId = rs.getInt(1);
String roleName = rs.getString(2);
int orderNo = rs.getInt(3);
System.out.println("roleId:"+roleId+" roleName:"+roleName+" orderNo:"+orderNo);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//最终目的建立属性到数据库表字段的映射,拼装sql语句
public static String query( Object f){
StringBuilder sb = new StringBuilder();
//1.获取到class
Class c = f.getClass();
//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 拿到字段名(属性对应数据库表中的字段,即注解的值)//4.2 拿到字段值(数据库表中的)
//4.3 拼装sql
boolean fexist = field.isAnnotationPresent(Column.class);
if (!fexist) {
continue;
}
Column column = field.getAnnotation(Column.class);//获得注解对象
String columnName = column.value(); //获得注解对象的值//取得属性的get方法
String fieldName = field.getName();
String getMethodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
//filedValue类型不唯一,所以定义成Object
Object fieldValue = null;
try {
Method getMethod = c.getMethod(getMethodName);
fieldValue = getMethod.invoke(f);
} catch (Exception e) {
e.printStackTrace();
}
//拼装sql,如果fieldValue是null或者是Integer类型且值为0时
if (fieldValue==null||(fieldValue instanceof Integer && (Integer)fieldValue==0)) {
continue;
}
sb.append(" and ").append(columnName);
if (fieldValue instanceof String) {
if (((String) fieldValue).contains(",")) {
sb.append(" in(");
String[] values = ((String) fieldValue).split(",");
for (String v : values) {
sb.append("'").append(v).append("'").append(",");
}
sb.deleteCharAt(sb.length()-1).append(")");//将最末尾的,删掉
}else {
sb.append(" = ").append("'").append(fieldValue).append("'");
}
}else {
sb.append(" = ").append(fieldValue);
}
}
return sb.toString();
}
}