java之手搓简单ORM框架--SQL的SELECT
1.手搓简单SQL增删改查框架-查询(id)
1.1创建简单类,并使用泛型类,这里可能使用到之间写的三篇知识的内容,如果不了解的小伙伴可以去
java高级之泛型
java高级之映射
java高级之反射
当然,前提是必须要把数据库相关连接弄好,这里会专门出一篇 java之jdbc
现在咱们继续手搓框架开始叭!
1.2 前期的准备工作
这里已经在【手搓简单SQL增删改查框架-插入】的时候准备完成,如果没有准备的小伙伴可以去瞅瞅
并且这次依然延续用前两篇文章的模板,及dao、userdao、自定义接口、测试类文件
1.3 手搓!!!
这里依然首先要获取通过子类获取父类的泛型反射类
public class CurrencyTest<T> {
private Class CurrencyTest;
public CurrencyTest(){
Class<? extends com.xw.exercise.CurrencyTest> aClass = this.getClass();
ParameterizedType genericSuperclass = (ParameterizedType)aClass.getGenericSuperclass();
Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
CurrencyTest = (Class) actualTypeArguments[0];
}
}
有了反射类剩下一切都好说
public T seleID(Object id) throws Exception{
StringBuffer sele = new StringBuffer("select * from ");
String simpleName = CurrencyTest.getSimpleName();
TableName annotation = (TableName)CurrencyTest.getAnnotation(TableName.class);
if(annotation != null){
simpleName = annotation.value();
}
sele.append(simpleName + " where ");
Field[] declaredFields = CurrencyTest.getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
String name = declaredField.getName();
TableId annotation1 = declaredField.getAnnotation(TableId.class);
if(annotation1 != null){
name = annotation1.value();
sele.append(name + " = " + "'" + id + "'");
}
break;
}
Connection connection = DButils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sele.toString());
ResultSet resultSet = preparedStatement.executeQuery();
//循环读取每一行结果
while (resultSet.next()){
//通过反射new类对象
T t = (T)CurrencyTest.newInstance();
//获取所有属性对象并循环
for (Field declaredField : declaredFields) {
//暴力反射,授权访问私有属性对象
declaredField.setAccessible(true);
//获取属性对象名称
String name = declaredField.getName();
//获取自定义注解
TableId annotation1 = declaredField.getAnnotation(TableId.class);
//判断属性对象名称是否与表名一致,判断id
if(annotation1 != null){
name = annotation1.value();
}
TableFiele annotation2 = declaredField.getAnnotation(TableFiele.class);
//判断属性对象名称是否与表名一致,判断属性对像
if(annotation2 != null){
name = annotation2.value();
}
//获取最终的属性对象
Object v = resultSet.getObject(name);
//将结果装入
declaredField.set(t,v);
}
return t;
}
return null;
}
2.0 测试
public static void main(String[] args) throws Exception {
studentdao studentdao = new studentdao();
TblStudent tblStudent = studentdao.seleID(5);
System.out.println(tblStudent);
}
2.1 测试结果:
TblStudent(id=5, sname=赵文帆, sex=男, cid=1)
当然,依次也可以延伸出查询所有的通用语句
3.0 手搓简单SQL增删改查框架-查询(All)
//通用的查询方法(查询所有)
public List<T> seleAll() throws Exception{
StringBuffer seleall = new StringBuffer("select * from ");
String simpleName = currency.getSimpleName();
TableName annotation = (TableName) currency.getAnnotation(TableName.class);
if(annotation != null){
simpleName = annotation.value();
}
seleall.append(simpleName);
Connection connection = DButils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(seleall.toString());
ResultSet resultSet = preparedStatement.executeQuery();
ArrayList<T> ts = new ArrayList<>();
Field[] declaredFields = currency.getDeclaredFields();
while (resultSet.next()){
T K = (T) currency.newInstance();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
String name = declaredField.getName();
TableId annotation1 = declaredField.getAnnotation(TableId.class);
if(annotation1 != null){
name = annotation1.value();
}
TableFiele annotation2 = declaredField.getAnnotation(TableFiele.class);
if(annotation2 != null){
name = annotation2.value();
}
Object v = resultSet.getObject(name);
declaredField.set(K,v);
}
ts.add(K);
}
return ts;
}
这里无非是返回类型变成了List,加了一个ArrayList,其他基本没有变
3.1 测试
public static void main(String[] args) throws Exception {
studentdao studentdao = new studentdao();
List<TblStudent> tblStudents = studentdao.seleAll();
System.out.println(tblStudents);
}
3.1.1 测试结果:
[TblStudent(id=1, sname=王轩, sex=男, cid=2), TblStudent(id=2, sname=张家豪, sex=男, cid=3), TblStudent(id=3, sname=吴军燕, sex=女, cid=1), TblStudent(id=4, sname=罗丽莉, sex=女, cid=4), TblStudent(id=5, sname=赵文帆, sex=男, cid=1)]
数据库:
-------
以上便是ORM框架中的SQL语句查询,如有漏缺请在下方留言告知,我会及时补充