表中字符串类型映射bean-hibernate实例
来源:互联网 发布:mysql 查询记录总数 编辑:程序博客网 时间:2024/06/15 00:28
需求:获取数据库表中某定义好的字段,与实体bean呼应,获取不同bean中属性值;
方式一:
用到的java反射原理(这样更加浪费内存,多用户访问时会慢,不建议使用)
public Object get(Session session, Mark mark,String id){try {Class<?> c = Class.forName(mark.getBeanName());return session.get(c,id);} catch (ClassNotFoundException e) {e.printStackTrace();}return null;}
方式二:
(这里获取connection时出现问题,解决后发现也应用了反射,但是相比方式一,可能效率更好些。这里我没做详细测试,有兴趣的朋友可以测试一下。)
public Map<String, Object> get2(Session session, Mark mark,String id){Map<String,Object> returnMap = new HashMap<String, Object>();List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();try { String tableName = mark.getBeanName();StringBuffer sql = new StringBuffer("select id,title from ");sql.append(tableName).append(" ");sql.append("where id='");sql.append(id).append("' ");//hibernate4.x之后获取connection的方法Connection connection;Method method = session.getClass().getMethod("connection");connection = (Connection) ReflectionUtils.invokeMethod(method, session);PreparedStatement ps = connection.prepareStatement(sql.toString());//结合spring管理,但是hibernate4.x之后每次只能获取一次,第二次null//PreparedStatement ps =session.disconnect().prepareStatement(sql.toString()); ResultSet rs = ps.executeQuery(); while (rs.next()) {Map dataMap=new HashMap();dataMap.put("id", rs.getObject("id"));dataMap.put("title", rs.getObject("title"));dataList.add(dataMap);}} catch (Exception e) {e.printStackTrace();}if(dataList.size()>0){returnMap=dataList.get(0);}return returnMap;}
0 0
- 表中字符串类型映射bean-hibernate实例
- Hibernate中映射枚举类型
- Hibernate中映射枚举类型
- Hibernate映射类型对照表
- Hibernate映射类型对照表
- Hibernate映射类型对照表
- Hibernate映射类型对照表
- hibernate中java.util.Date类型映射
- JNA类型映射实例__结构体中包含字符串数组
- Hibernate实体Bean一对一映射
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)
- hibernate.reveng.xml中修改hibernate映射类型timstamp
- 【转载】Hibernate映射类型对照表
- Hibernate映射类型
- Hibernate映射类型
- Hibernate的映射类型
- Hibernate的映射类型
- hibernate之映射类型
- Mysql zip 安装(windows)
- 解决win下的文件到Ubuntu上显示乱码的问题
- android studio问题汇总
- iOS_Label上文字不同颜色设置
- 电池电量信息获取
- 表中字符串类型映射bean-hibernate实例
- jQuery 基础选择器
- MySQL存储过程详解 mysql 存储过程
- 第四周--单链表应用(判断是否递增)
- test5.18
- 文件下载
- MVC 认识
- Objective-C入门16:内存管理
- string, const char*, char 之间的转化