手撕orm之selectAll()和selectById()

思路:拼接sql,将sql执行结果赋值到相应的字段上。设置Field属性的值:属性对象。set(反射类对象,相应字段的值(Object类型)  )。可以抽取将执行结果赋值给相应字段的部分的代码成一个方法,参数为结果集ResultSet和反射类。

public T selectById(Object id) throws Exception{
        StringBuffer sql=new StringBuffer("select * from ");
        //获取表名
        String tableName=clazz.getSimpleName();
        TableName annotation = clazz.getAnnotation(TableName.class);
        if(annotation!=null){
            tableName=annotation.value();
        }
        sql.append(tableName+" where ");
        //获取主键列名
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field:declaredFields){
            TableId tableId = field.getAnnotation(TableId.class);
            if(tableId!=null){
                sql.append(tableId.value()+"="+id);
                break;
            }
        }

        //执行sql语句
        Connection conn = DbUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql.toString());
        ResultSet rs = ps.executeQuery();
        //封装数据到实体类
        while(rs.next()){
            T t=clazz.newInstance();
            for (Field field:declaredFields){
                field.setAccessible(true);
                TableField tableField = field.getAnnotation(TableField.class);
                TableId tableId = field.getAnnotation(TableId.class);
                //获取属性名
                String name = field.getName();
                if(tableId!=null){
                   name=tableId.value();
                }
                if(tableField!=null){
                    name=tableField.value();
                }
                //获取数据库中指定列的值
                Object v = rs.getObject(name);
                //为指定对象的属性赋值
                field.set(t,v);
            }
            return t;
        }
        return null;

    }

selectAll()

public List<T> selectAll()throws Exception{
        List<T> list=new ArrayList<>();
        StringBuffer sql=new StringBuffer("select * from ");
        //获取表名
        String tableName=clazz.getSimpleName();
        TableName annotation = clazz.getAnnotation(TableName.class);
        if(annotation!=null){
            tableName=annotation.value();
        }
        sql.append(tableName);
        //执行sql语句
        Connection conn = DbUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql.toString());
        ResultSet rs = ps.executeQuery();
        Field[] declaredFields = clazz.getDeclaredFields();
        //封装数据
        while (rs.next()){
            T t=clazz.newInstance();
            //为实体类的属性赋值
            for (Field field:declaredFields){
                field.setAccessible(true);
                TableField tableField = field.getAnnotation(TableField.class);
                TableId tableId = field.getAnnotation(TableId.class);
                //获取属性名
                String name = field.getName();
                if(tableId!=null){
                    name=tableId.value();
                }
                if(tableField!=null){
                    name=tableField.value();
                }
                //获取数据库中指定列的值
                Object v = rs.getObject(name);
                //为指定对象的属性赋值
                field.set(t,v);
            }
            list.add(t);
        }
        return list;
    }

删除之类操作需要全选功能,方便选择 public class MainActivity extends Activity { private ListView lv; private MyAdapter mAdapter; private ArrayList list; private Button bt_selectall; // private Button bt_cancel; // private Button bt_deselectall; private int checkNum; // 记录选的条目数量 private TextView tv_show;// 用于显示选的条目数量 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 实例化各个控件 */ lv = (ListView) findViewById(R.id.lv); bt_selectall = (Button) findViewById(R.id.bt_selectall); // bt_cancel = (Button) findViewById(R.id.bt_cancelselectall); // bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show = (TextView) findViewById(R.id.tv); list = new ArrayList(); // 为Adapter准备数据 initDate(); // 实例化自定义的MyAdapter mAdapter = new MyAdapter(list, this); // 绑定Adapter lv.setAdapter(mAdapter); // 全选按钮的回调接口 bt_selectall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的长度,将MyAdapter的map值全部设为true for (int i = 0; i < list.size(); i++) { MyAdapter.getIsSelected().put(i, true); } // 数量设为list的长度 checkNum = list.size(); // 刷新listview和TextView的显示 dataChanged(); } }); // 反选按钮的回调接口 // bt_cancel.setOnClickListener(new OnClickListener() { // @Override // public void onClick(View v) { // // 遍历list的长度,将已选的设为未选,未选的设为已选 // for (int i = 0; i < list.siz
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值