public class Connection1{
static class student{
public student() {
}//我懒得取消访问检查 所以public
int studentno;
String studentname;
}
static class F<T>{
List<T> list;//定义泛型T
Class cl;//要获取的class对象
public F(Class cl) {
this.cl=cl;
list=new ArrayList<T>();
}
public List<T> select(ResultSet rs){
try {
ResultSetMetaData data=rs.getMetaData();
int count=data.getColumnCount();//列数
Field [] field=cl.getDeclaredFields();//通过反射获取属性
while(rs.next()) {
T obj=(T)cl.getConstructor().newInstance();//反射获取构造器并创建对象
for (int i = 1; i <=count; i++) {
String lab=data.getColumnName(i);
System.out.println(lab);
for(Field ff:field) {
if(lab.equalsIgnoreCase(ff.getName())) {//看列名和属性名是否一致
ff.setAccessible(true);//取消访问检查
ff.set(obj, rs.getObject(i));//设置属性值
break;
}
}
}
list.add(obj);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
static class BassDao{
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception E) {
}
}
public Connection getConn() {
Connection conn=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool?useSSL=false&serverTimezone=UTC","root","123");
}catch(Exception e) {
}
return conn;
}
public ResultSet query(String sql,List<Object> list) {
try {
PreparedStatement ps =getConn().prepareStatement(sql);
if(list!=null&&list.size()!=0) {
for (int i = 0; i < list.size(); i++) {
ps.setObject(i+1, list.get(i));
}
}
return ps.executeQuery();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
public void Test() {
try {
F<student> f=new F<student>(student.class);
List<student> stu=f.select(query("select * from student", null));
for(student stua:stu){
System.out.println(stua.studentname);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
public static void main(String[] args) {
new BassDao().Test();
}
}
上面是原来的 后来学了数据结构 知道了时间复杂度 那么上面就可以用map来映射时间复杂度就为n^2了 不过必须列名要一样
类型也要对 不过我都是idea中直接生成的实体类 希望对看到的人有帮助吧...
public class createEm<T> {
List<T> list;//定义泛型T
Class cl;//要获取的class对象
public createEm(Class cl) {
this.cl=cl;
list=new ArrayList<T>();
}
public List<T> select(ResultSet rs){
try {
ResultSetMetaData data=rs.getMetaData();
int count=data.getColumnCount();//列数
Field[] field=cl.getDeclaredFields();//通过反射获取属性
Map<String, Field> map=new HashMap(field.length);
for (Field temp:field) {
map.put(temp.getName().toUpperCase(),temp);//toUpperCase是防止大小写
}
while(rs.next()) {
T obj=(T)cl.getDeclaredConstructor().newInstance();//反射获取构造器并创建对象
for (int i = 1; i <=count; i++) {
String lab = data.getColumnName(i).trim();
Field field1=map.get(lab.toUpperCase());
if(field1!=null){
field1.setAccessible(true);
try {
field1.set(obj, rs.getObject(i));//设置属性值
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
list.add(obj);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
每一次都要创建map开销比较大 于是我就用 一个静态的map 来存放 上面的map 希望对看到的有用吧...
package util;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class createEm<T> {
List<T> list;//定义泛型T
Class cl;//要获取的class对象
static Map<Class,Map<String,Field>> entrtymap;
static{
entrtymap=new HashMap<>(25);//实体类个数
}
public createEm(Class cl) {
this.cl=cl;
list=new ArrayList<T>();
}
public List<T> select(ResultSet rs){
try {
ResultSetMetaData data=rs.getMetaData();
int count=data.getColumnCount();//列数
Field[] field=cl.getDeclaredFields();//通过反射获取属性
Map<String, Field> map;
if(entrtymap.get(cl)==null){//没有击中就创建对象
map=new HashMap(field.length);
for (Field temp:field)
map.put(temp.getName().toUpperCase(),temp);//toUpperCase是防止大小写
entrtymap.put(cl,map);
}else map=entrtymap.get(cl);
//如果有多次查询不必每一次都构造map
while(rs.next()) {
T obj=(T)cl.getDeclaredConstructor().newInstance();//反射获取构造器并创建对象
for (int i = 1; i <=count; i++) {
String lab = data.getColumnName(i).trim();
Field field1=map.get(lab.toUpperCase());
if(field1!=null){
field1.setAccessible(true);
try {
if(rs.getObject(i)==null) continue;
field1.set(obj, rs.getObject(i));//设置属性值
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}
list.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
return list;
}
}
}