hibernate之简单模拟hibernate的save方法

hibernate大多内部使用字节码方式得到对象,我们这里用反射替代。
实体

package shujujiegou;

/**
 * Created by lcc on 2017/7/14.
 */
public class Teacher {
    public Teacher(){
    }

    public Teacher(int id, String name, String title) {
        this.id = id;
        this.name = name;
        this.title = title;
    }

    private int id;
    private String name;
    private String title;

    public int getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

hibernate的session模拟

/**
 * Created by lcc on 2017/7/14.
 */
public class SessionMoni {

    //我们假设从配置文件里读取到了表名。
    private String tableName = "tb_teacher";
    //我们假设这里的map是表里列与Teacher字段一一对应,也是从配置文件里读取
    private Map<String, String> cfs = new HashMap<String, String>();
    private String[] methodName;

    public SessionMoni() {
        cfs.put("tb_id", "id");
        cfs.put("tb_name", "name");
        cfs.put("tb_title", "title");
        methodName = new String[cfs.size()];
    }

    public void save(Teacher t) {
        Connection connection = getConn();
        String sql = createSql();
        System.out.println(sql);
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) connection.prepareStatement(sql);
            for (int i = 0; i < methodName.length; i++) {
                Method method = t.getClass().getMethod(methodName[i]);
                Class type = method.getReturnType();
                String typeName = type.getSimpleName();
                if (typeName.equals("int")) {
                    Integer value = (Integer) method.invoke(t);
                    System.out.println(typeName + " " + value);
                    pstmt.setInt(i + 1, value);
                } else if (typeName.equals("String")) {
                    String value = (String) method.invoke(t);
                    pstmt.setString(i + 1, value);
                    System.out.println(typeName + " " + value);
                }
            }
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private String createSql() {
        String str1 = "";
        int index = 0;
        for (String s : cfs.keySet()) {
            str1 = s + "," + str1;
            String sm = cfs.get(s);
            sm = sm.toUpperCase().charAt(0) + sm.substring(1);
            methodName[index] = "get" + sm;
            index++;
        }
        str1 = str1.substring(0, str1.length() - 1);
        String str2 = "";
        for (int i = 0; i < cfs.size(); i++) {
            str2 = "?," + str2;
        }
        str2 = str2.substring(0, str2.length() - 1);
        String sql = "insert into " + tableName + "(" + str1 + ")" + "values (" + str2 + ")";
        return sql;
    }

    /**
     * @return
     */
    private static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/samp_db";
        String username = "root";
        String password = "";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

}

调用

package shujujiegou;

/**
 * Created by lcc on 2017/7/14.
 */
public class TeacherTest {
    public static void main(String[] args) {
        Teacher t = new Teacher();
        t.setId(1);
        t.setName("小名");
        t.setTitle("中级");
        SessionMoni session = new SessionMoni();
        session.save(t);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值