一个简单的泛型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;
}
}
这是我结合网上的一些资料自己编写的,可能会有很多不足欢迎大家指出!