简单泛型DAO的实现
来源:互联网 发布:大数据细分领域 编辑:程序博客网 时间:2024/06/11 04:57
一个简单的泛型DAO的实现package dao;import java.util.List;public interface SelectSQL { //1.查询所有 public <T>List<T> getUser(T t); //2.根据姓名查询年龄 public <T> int getAge(String name,T t); //3.查询前n条信息 public <T> List<T> getUser(int n,T t); //4.查询有多少条记录 public <T> int getNum(T t); //5.对年龄进行升序排序 public <T> List<T> getAgeASC(T t); //6.对姓名进行升序排序 public <T> List<T> getNameASC(T t); //7.根据性别记行分组 public <T> List<T> getUserGroup(T t); //8.查询年龄在 x ~ y 之间的人 public <T> List<T> getAgeXY(int x,int y,T t); //9.查询年龄在 x ~ y 之间的 男/女 public <T> List<T> getAgeXY(int x,int y,String sex,T t); //10.查询年龄在 x ~ y 之间的 男/女 并按降序排序 public <T> List<T> getAgeXYDesc(int x,int y,String sex,T t); //11.查询当页的对象 public <T> List<T> findByPage(int page,T t); //12.获取总的记录数 public <T> int getCount(T t);}这是随意的12个查询方法;package dao.impl;import java.lang.reflect.Field;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import dao.SelectSQL;import util.ConnectionManager;public class SelectSQLImpl implements SelectSQL{ Connection conn = ConnectionManager.getConnection(); @Override public <T> List<T> getUser(T t) { @SuppressWarnings("rawtypes") Class clazz1 = t.getClass(); List<T> list = new ArrayList<T>(); String clazz = clazz1.getSimpleName(); //得到类的简单名称 String sql = "select * from " + clazz; Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(sql); System.out.println(sql); while(rs.next()){ @SuppressWarnings("unchecked") T t1 = (T) clazz1.newInstance(); //根据反射创建对象 Field[] fs = clazz1.getDeclaredFields(); //得到类中所有属性的集合 for(int i=0; i<fs.length; i++){ //变量属性集合 Field f = fs[i]; f.setAccessible(true); //设置为true时,在反射是可以访问该变量 String type1 = f.getType().toString(); //得到此属性的类型 if(type1.endsWith("String")){ //如果是String类型 f.set(t1, rs.getString(i+1)); //给属性设置 }else if(type1.endsWith("int")){ f.set(t1, rs.getInt(i+1)); }else{ f.set(t1, rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return list; } @Override public <T> int getAge(String name, T t) { @SuppressWarnings("rawtypes") Class clazz = t.getClass(); String clazzName = clazz.getSimpleName(); String sql = "select age from " + clazzName + " where name = '" + name + "'"; Statement st = null; ResultSet rs = null; int age = 0; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ age = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return age; } @Override public <T> List<T> getUser(int n, T t) { List<T> list = new ArrayList<T>(); @SuppressWarnings("rawtypes") Class clazz = t.getClass(); String clazzName = clazz.getSimpleName(); String sql = "select * from " + clazzName + " where id = " + n ; Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(sql); System.out.println("00 " + sql); while(rs.next()){ @SuppressWarnings("unchecked") T t1 = (T) clazz.newInstance(); Field[] fs = clazz.getDeclaredFields(); for(int i=0; i< fs.length; i++){ Field f = fs[i]; f.setAccessible(true); String type = f.getType().toString(); if(type.endsWith("String")){ f.set(t1, rs.getString(i+1)); }else if(type.endsWith("int")){ f.set(t1, rs.getInt(i+1)); }else{ f.set(t1,rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return list; } @Override public <T> int getNum(T t) { @SuppressWarnings("rawtypes") Class clazz = t.getClass(); String simpleName = clazz.getSimpleName(); String sql = "select count(*) from " + simpleName; Statement st = null; ResultSet rs = null; int i = 0; try { st = conn.createStatement(); rs = st.executeQuery(sql); if(rs.next()){ i = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return i; } @Override public <T> List<T> getAgeASC(T t) { List<T> list = new ArrayList<T>(); Class clazz = t.getClass(); String SimpleName = clazz.getSimpleName(); String sql = "select age from " + SimpleName + " order by age "; Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ T t1 = (T) clazz.newInstance(); Field[] fs = clazz.getDeclaredFields(); for(int i=0; i<fs.length;i++){ Field f = fs[i]; f.setAccessible(true); String type = f.getType().toString(); if(type.endsWith("String")){ f.set(t1, rs.getString(i+1)); }else if(type.endsWith("int")){ f.set(t1,rs.getInt(i+1)); }else{ f.set(t1,rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return list; } @Override public <T> List<T> getNameASC(T t) { List<T> list = new ArrayList<T>(); Class clazz = t.getClass(); String SimpleName = clazz.getSimpleName(); String sql = "select * from " + SimpleName + " order by name"; Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ T t1 = (T) clazz.newInstance(); Field[] fs = clazz.getDeclaredFields(); for(int i=0; i<fs.length;i++){ Field f = fs[i]; f.setAccessible(true); String type = f.getType().toString(); if(type.endsWith("String")){ f.set(t1, rs.getString(i+1)); }else if(type.endsWith("int")){ f.set(t1,rs.getInt(i+1)); }else{ f.set(t1,rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return list; } @Override public <T> List<T> getUserGroup(T t) { List<T> list = new ArrayList<T>(); Class clazz = t.getClass(); String SimpleName = clazz.getSimpleName(); String sql = "select * from " + SimpleName + " group by sex"; Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ T t1 = (T) clazz.newInstance(); Field[] fs = clazz.getDeclaredFields(); for(int i=0; i<fs.length;i++){ Field f = fs[i]; f.setAccessible(true); String type = f.getType().toString(); if(type.endsWith("String")){ f.set(t1, rs.getString(i+1)); }else if(type.endsWith("int")){ f.set(t1,rs.getInt(i+1)); }else{ f.set(t1,rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return list; } @Override public <T> List<T> getAgeXY(int x, int y, T t) { List<T> list = new ArrayList<T>(); Class clazz = t.getClass(); String SimpleName = clazz.getSimpleName(); String sql = "select * from " + SimpleName + " where age between " + x + " and " + y; Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ T t1 = (T) clazz.newInstance(); Field[] fs = clazz.getDeclaredFields(); for(int i=0; i<fs.length;i++){ Field f = fs[i]; f.setAccessible(true); String type = f.getType().toString(); if(type.endsWith("String")){ f.set(t1, rs.getString(i+1)); }else if(type.endsWith("int")){ f.set(t1,rs.getInt(i+1)); }else{ f.set(t1,rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return list; } @Override public <T> List<T> getAgeXY(int x, int y, String sex, T t) { List<T> list = new ArrayList<T>(); Class clazz = t.getClass(); String SimpleName = clazz.getSimpleName(); String sql = "select * from " + SimpleName + " where sex = '" +sex + "' and age " + "between 10 and 12" ; Statement st = null; ResultSet rs = null; try { System.out.println(sql); st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ @SuppressWarnings("unchecked") T t1 = (T) clazz.newInstance(); Field[] fs = clazz.getDeclaredFields(); for(int i=0; i<fs.length;i++){ System.out.println(rs.getObject(i+1)); Field f = fs[i]; f.setAccessible(true); String type = f.getType().toString(); if(type.endsWith("String")){ f.set(t1, rs.getString(i+1)); }else if(type.endsWith("int")){ f.set(t1,rs.getInt(i+1)); }else{ f.set(t1,rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return list; } @Override public <T> List<T> getAgeXYDesc(int x, int y, String sex, T t) { List<T> list = new ArrayList<T>(); Class clazz = t.getClass(); String SimpleName = clazz.getSimpleName(); String sql = "select * from " + SimpleName + " where sex = '" +sex + "' and age " + "between 10 and 12 order by desc" ; Statement st = null; ResultSet rs = null; try { System.out.println(sql); st = conn.createStatement(); rs = st.executeQuery(sql); while(rs.next()){ @SuppressWarnings("unchecked") T t1 = (T) clazz.newInstance(); Field[] fs = clazz.getDeclaredFields(); for(int i=0; i<fs.length;i++){ System.out.println(rs.getObject(i+1)); Field f = fs[i]; f.setAccessible(true); String type = f.getType().toString(); if(type.endsWith("String")){ f.set(t1, rs.getString(i+1)); }else if(type.endsWith("int")){ f.set(t1,rs.getInt(i+1)); }else{ f.set(t1,rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return list; } @Override public <T> List<T> findByPage(int page, T t) { @SuppressWarnings("rawtypes") Class clazz1 = t.getClass(); int begin = (page-1)*5; List<T> list = new ArrayList<T>(); String clazz = clazz1.getSimpleName(); String sql = "select * from " + clazz + " limit " + begin + ",5"; Statement st = null; ResultSet rs = null; try { st = conn.createStatement(); rs = st.executeQuery(sql); System.out.println(sql); while(rs.next()){ @SuppressWarnings("unchecked") T t1 = (T) clazz1.newInstance(); Field[] fs = clazz1.getDeclaredFields(); //得到类中所有属性的集合 for(int i=0; i<fs.length; i++){ Field f = fs[i]; f.setAccessible(true); String type1 = f.getType().toString(); //得到此属性的类型 if(type1.endsWith("String")){ f.set(t1, rs.getString(i+1)); //给属性设置 }else if(type1.endsWith("int")){ f.set(t1, rs.getInt(i+1)); }else{ f.set(t1, rs.getObject(i+1)); } } list.add(t1); } } catch (SQLException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return list; } @Override public <T> int getCount(T t) { @SuppressWarnings("rawtypes") Class clazz = t.getClass(); String simpleName = clazz.getSimpleName(); String sql = "select count(*) from " + simpleName; Statement st = null; ResultSet rs = null; int i = 0; try { st = conn.createStatement(); rs = st.executeQuery(sql); if(rs.next()){ i = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return i; }}
这是我结合网上的一些资料自己编写的,可能会有很多不足欢迎大家指出!
阅读全文
0 0
- 简单泛型DAO的实现
- 反射机制实现DAO的泛型
- JdbcTemplate 实现泛型DAO
- Spring JdbcTemplate实现通用的超级dao,比泛型dao更加强大
- [转]Spring JdbcTemplate实现通用的超级dao,比泛型dao更加强大
- 简单的DAO操作
- DAO的简单示例
- 一个简单的DAO
- 关于DAO简单理解实现
- DAO设计模式---实现一个简单的注册(上)
- DAO设计模式---实现一个简单的注册(中)
- DAO设计模式---实现一个简单的注册(下)
- java spring 简单封装出自己的DAO Service实现
- SSM实现简单的CRUD之DAO层
- 使用AOP实现类型安全的泛型DAO
- 基于泛型的通用Dao接口hibernate实现
- dao层的泛型实现(2种方法)
- java 泛型实现Dao 操作的重用
- 牧函 | 十字架和垃圾场
- hdu 6235 Permutation
- JDBC快速保存数据到MySQL
- 原型对象的作用及原型对象的用法
- Hangover
- 简单泛型DAO的实现
- IMX6的LINUX下的IIC驱动(一)
- python编写spark的配置
- list去除重复元素的两种方法
- STM32寄存器列表 RCC时钟相关寄存器
- Python 正则表达式元字符、re 模块
- 前台数据json序列化,从post或get中取py数据类型:QueryDict转字典dict
- selenium-grid AND docker化方案
- 一乐牧师:以十字架定义政教关系